- 阅读权限
- 255
- 威望
- 1 级
- 论坛币
- 49392 个
- 通用积分
- 51.6904
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57815 点
- 帖子
- 4006
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
学术权威
还不是VIP/贵宾
TA的文库 其他... R资源总汇
Panel Data Analysis
Experimental Design
- 威望
- 1 级
- 论坛币
- 49392 个
- 通用积分
- 51.6904
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57815 点
- 帖子
- 4006
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
| 开心 2017-10-21 10:25:33 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
|
经管之家送您一份
应届毕业生专属福利!
求职就业群
感谢您参与论坛问题回答
经管之家送您两个论坛币!
+2 论坛币
- // scalastyle:off println
- package org.apache.spark.examples.ml
- import scala.collection.mutable
- import scala.language.reflectiveCalls
- import scopt.OptionParser
- import org.apache.spark.{SparkConf, SparkContext}
- import org.apache.spark.examples.mllib.AbstractParams
- import org.apache.spark.ml.{Pipeline, PipelineStage}
- import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel}
- import org.apache.spark.ml.feature.StringIndexer
- import org.apache.spark.sql.DataFrame
- /**
- * An example runner for logistic regression with elastic-net (mixing L1/L2) regularization.
- * Run with
- * {{{
- * bin/run-example ml.LogisticRegressionExample [options]
- * }}}
- * A synthetic dataset can be found at `data/mllib/sample_libsvm_data.txt` which can be
- * trained by
- * {{{
- * bin/run-example ml.LogisticRegressionExample --regParam 0.3 --elasticNetParam 0.8 \
- * data/mllib/sample_libsvm_data.txt
- * }}}
- * If you use it as a template to create your own app, please use `spark-submit` to submit your app.
- */
- object LogisticRegressionExample {
- case class Params(
- input: String = null,
- testInput: String = "",
- dataFormat: String = "libsvm",
- regParam: Double = 0.0,
- elasticNetParam: Double = 0.0,
- maxIter: Int = 100,
- fitIntercept: Boolean = true,
- tol: Double = 1E-6,
- fracTest: Double = 0.2) extends AbstractParams[Params]
- def main(args: Array[String]) {
- val defaultParams = Params()
- val parser = new OptionParser[Params]("LogisticRegressionExample") {
- head("LogisticRegressionExample: an example Logistic Regression with Elastic-Net app.")
- opt[Double]("regParam")
- .text(s"regularization parameter, default: ${defaultParams.regParam}")
- .action((x, c) => c.copy(regParam = x))
- opt[Double]("elasticNetParam")
- .text(s"ElasticNet mixing parameter. For alpha = 0, the penalty is an L2 penalty. " +
- s"For alpha = 1, it is an L1 penalty. For 0 < alpha < 1, the penalty is a combination of " +
- s"L1 and L2, default: ${defaultParams.elasticNetParam}")
- .action((x, c) => c.copy(elasticNetParam = x))
- opt[Int]("maxIter")
- .text(s"maximum number of iterations, default: ${defaultParams.maxIter}")
- .action((x, c) => c.copy(maxIter = x))
- opt[Boolean]("fitIntercept")
- .text(s"whether to fit an intercept term, default: ${defaultParams.fitIntercept}")
- .action((x, c) => c.copy(fitIntercept = x))
- opt[Double]("tol")
- .text(s"the convergence tolerance of iterations, Smaller value will lead " +
- s"to higher accuracy with the cost of more iterations, default: ${defaultParams.tol}")
- .action((x, c) => c.copy(tol = x))
- opt[Double]("fracTest")
- .text(s"fraction of data to hold out for testing. If given option testInput, " +
- s"this option is ignored. default: ${defaultParams.fracTest}")
- .action((x, c) => c.copy(fracTest = x))
- opt[String]("testInput")
- .text(s"input path to test dataset. If given, option fracTest is ignored." +
- s" default: ${defaultParams.testInput}")
- .action((x, c) => c.copy(testInput = x))
- opt[String]("dataFormat")
- .text("data format: libsvm (default), dense (deprecated in Spark v1.1)")
- .action((x, c) => c.copy(dataFormat = x))
- arg[String]("<input>")
- .text("input path to labeled examples")
- .required()
- .action((x, c) => c.copy(input = x))
- checkConfig { params =>
- if (params.fracTest < 0 || params.fracTest >= 1) {
- failure(s"fracTest ${params.fracTest} value incorrect; should be in [0,1).")
- } else {
- success
- }
- }
- }
- parser.parse(args, defaultParams).map { params =>
- run(params)
- }.getOrElse {
- sys.exit(1)
- }
- }
- def run(params: Params) {
- val conf = new SparkConf().setAppName(s"LogisticRegressionExample with $params")
- val sc = new SparkContext(conf)
- println(s"LogisticRegressionExample with parameters:\n$params")
- // Load training and test data and cache it.
- val (training: DataFrame, test: DataFrame) = DecisionTreeExample.loadDatasets(sc, params.input,
- params.dataFormat, params.testInput, "classification", params.fracTest)
- // Set up Pipeline
- val stages = new mutable.ArrayBuffer[PipelineStage]()
- val labelIndexer = new StringIndexer()
- .setInputCol("label")
- .setOutputCol("indexedLabel")
- stages += labelIndexer
- val lor = new LogisticRegression()
- .setFeaturesCol("features")
- .setLabelCol("indexedLabel")
- .setRegParam(params.regParam)
- .setElasticNetParam(params.elasticNetParam)
- .setMaxIter(params.maxIter)
- .setTol(params.tol)
- .setFitIntercept(params.fitIntercept)
- stages += lor
- val pipeline = new Pipeline().setStages(stages.toArray)
- // Fit the Pipeline
- val startTime = System.nanoTime()
- val pipelineModel = pipeline.fit(training)
- val elapsedTime = (System.nanoTime() - startTime) / 1e9
- println(s"Training time: $elapsedTime seconds")
- val lorModel = pipelineModel.stages.last.asInstanceOf[LogisticRegressionModel]
- // Print the weights and intercept for logistic regression.
- println(s"Weights: ${lorModel.coefficients} Intercept: ${lorModel.intercept}")
- println("Training data results:")
- DecisionTreeExample.evaluateClassificationModel(pipelineModel, training, "indexedLabel")
- println("Test data results:")
- DecisionTreeExample.evaluateClassificationModel(pipelineModel, test, "indexedLabel")
- sc.stop()
- }
- }
- // scalastyle:on println
复制代码
扫码加我 拉你入群
请注明:姓名-公司-职位
以便审核进群资格,未注明则拒绝
|
|
|