楼主: ReneeBK
1682 0

[Scala Study]Linear Regression using Scala [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4897份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49635 个
通用积分
55.6937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2015-11-16 00:35:57 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. // scalastyle:off println
  2. package org.apache.spark.examples.ml

  3. import scala.collection.mutable
  4. import scala.language.reflectiveCalls

  5. import scopt.OptionParser

  6. import org.apache.spark.{SparkConf, SparkContext}
  7. import org.apache.spark.examples.mllib.AbstractParams
  8. import org.apache.spark.ml.{Pipeline, PipelineStage}
  9. import org.apache.spark.ml.regression.{LinearRegression, LinearRegressionModel}
  10. import org.apache.spark.sql.DataFrame

  11. /**
  12. * An example runner for linear regression with elastic-net (mixing L1/L2) regularization.
  13. * Run with
  14. * {{{
  15. * bin/run-example ml.LinearRegressionExample [options]
  16. * }}}
  17. * A synthetic dataset can be found at `data/mllib/sample_linear_regression_data.txt` which can be
  18. * trained by
  19. * {{{
  20. * bin/run-example ml.LinearRegressionExample --regParam 0.15 --elasticNetParam 1.0 \
  21. *   data/mllib/sample_linear_regression_data.txt
  22. * }}}
  23. * If you use it as a template to create your own app, please use `spark-submit` to submit your app.
  24. */
  25. object LinearRegressionExample {

  26.   case class Params(
  27.       input: String = null,
  28.       testInput: String = "",
  29.       dataFormat: String = "libsvm",
  30.       regParam: Double = 0.0,
  31.       elasticNetParam: Double = 0.0,
  32.       maxIter: Int = 100,
  33.       tol: Double = 1E-6,
  34.       fracTest: Double = 0.2) extends AbstractParams[Params]

  35.   def main(args: Array[String]) {
  36.     val defaultParams = Params()

  37.     val parser = new OptionParser[Params]("LinearRegressionExample") {
  38.       head("LinearRegressionExample: an example Linear Regression with Elastic-Net app.")
  39.       opt[Double]("regParam")
  40.         .text(s"regularization parameter, default: ${defaultParams.regParam}")
  41.         .action((x, c) => c.copy(regParam = x))
  42.       opt[Double]("elasticNetParam")
  43.         .text(s"ElasticNet mixing parameter. For alpha = 0, the penalty is an L2 penalty. " +
  44.         s"For alpha = 1, it is an L1 penalty. For 0 < alpha < 1, the penalty is a combination of " +
  45.         s"L1 and L2, default: ${defaultParams.elasticNetParam}")
  46.         .action((x, c) => c.copy(elasticNetParam = x))
  47.       opt[Int]("maxIter")
  48.         .text(s"maximum number of iterations, default: ${defaultParams.maxIter}")
  49.         .action((x, c) => c.copy(maxIter = x))
  50.       opt[Double]("tol")
  51.         .text(s"the convergence tolerance of iterations, Smaller value will lead " +
  52.         s"to higher accuracy with the cost of more iterations, default: ${defaultParams.tol}")
  53.         .action((x, c) => c.copy(tol = x))
  54.       opt[Double]("fracTest")
  55.         .text(s"fraction of data to hold out for testing.  If given option testInput, " +
  56.         s"this option is ignored. default: ${defaultParams.fracTest}")
  57.         .action((x, c) => c.copy(fracTest = x))
  58.       opt[String]("testInput")
  59.         .text(s"input path to test dataset.  If given, option fracTest is ignored." +
  60.         s" default: ${defaultParams.testInput}")
  61.         .action((x, c) => c.copy(testInput = x))
  62.       opt[String]("dataFormat")
  63.         .text("data format: libsvm (default), dense (deprecated in Spark v1.1)")
  64.         .action((x, c) => c.copy(dataFormat = x))
  65.       arg[String]("<input>")
  66.         .text("input path to labeled examples")
  67.         .required()
  68.         .action((x, c) => c.copy(input = x))
  69.       checkConfig { params =>
  70.         if (params.fracTest < 0 || params.fracTest >= 1) {
  71.           failure(s"fracTest ${params.fracTest} value incorrect; should be in [0,1).")
  72.         } else {
  73.           success
  74.         }
  75.       }
  76.     }

  77.     parser.parse(args, defaultParams).map { params =>
  78.       run(params)
  79.     }.getOrElse {
  80.       sys.exit(1)
  81.     }
  82.   }

  83.   def run(params: Params) {
  84.     val conf = new SparkConf().setAppName(s"LinearRegressionExample with $params")
  85.     val sc = new SparkContext(conf)

  86.     println(s"LinearRegressionExample with parameters:\n$params")

  87.     // Load training and test data and cache it.
  88.     val (training: DataFrame, test: DataFrame) = DecisionTreeExample.loadDatasets(sc, params.input,
  89.       params.dataFormat, params.testInput, "regression", params.fracTest)

  90.     val lir = new LinearRegression()
  91.       .setFeaturesCol("features")
  92.       .setLabelCol("label")
  93.       .setRegParam(params.regParam)
  94.       .setElasticNetParam(params.elasticNetParam)
  95.       .setMaxIter(params.maxIter)
  96.       .setTol(params.tol)

  97.     // Train the model
  98.     val startTime = System.nanoTime()
  99.     val lirModel = lir.fit(training)
  100.     val elapsedTime = (System.nanoTime() - startTime) / 1e9
  101.     println(s"Training time: $elapsedTime seconds")

  102.     // Print the weights and intercept for linear regression.
  103.     println(s"Weights: ${lirModel.coefficients} Intercept: ${lirModel.intercept}")

  104.     println("Training data results:")
  105.     DecisionTreeExample.evaluateRegressionModel(lirModel, training, "label")
  106.     println("Test data results:")
  107.     DecisionTreeExample.evaluateRegressionModel(lirModel, test, "label")

  108.     sc.stop()
  109.   }
  110. }
  111. // scalastyle:on println
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:regression regressio regress Linear SCALA example import

本帖被以下文库推荐

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-29 05:26