@@ -24,24 +24,50 @@ import org.apache.spark.mllib.regression.LinearRegressionModel
2424import org .apache .spark .mllib .regression .LinearRegressionWithSGD
2525import org .apache .spark .rdd .RDD
2626
27+ import scopt .OptionParser
28+
2729object LinearRegression {
2830
31+ case class Params (
32+ dataPath : String = null ,
33+ numIterations : Int = 100 ,
34+ stepSize : Double = 0.00000001
35+ )
36+
2937 def main (args : Array [String ]): Unit = {
30- var inputPath = " "
38+ val defaultParams = Params ()
3139
32- if (args.length == 1 ) {
33- inputPath = args(0 )
40+ val parser = new OptionParser [Params ](" Linear" ){
41+ head(" Linear Regression: an example of linear regression with SGD optimizer" )
42+ opt[Int ](" numIterations" )
43+ .text(s " numIterations, default: ${defaultParams.numIterations}" )
44+ .action((x,c) => c.copy(numIterations = x))
45+ opt[Double ](" stepSize" )
46+ .text(s " stepSize, default: ${defaultParams.stepSize}" )
47+ .action((x,c) => c.copy(stepSize = x))
48+ arg[String ](" <dataPath>" )
49+ .required()
50+ .text(" Input path for data" )
51+ .action((x,c) => c.copy(dataPath = x))
3452 }
35-
36- val conf = new SparkConf ().setAppName(" LinearRegressionWithSGD" )
53+ parser.parse(args, defaultParams) match {
54+ case Some (params) => run(params)
55+ case _ => sys.exit(1 )
56+ }
57+ }
58+
59+ def run (params : Params ): Unit = {
60+ val conf = new SparkConf ().setAppName(s " LinearRegressionWithSGD with $params" )
3761 val sc = new SparkContext (conf)
62+
63+ val dataPath = params.dataPath
64+ val numIterations = params.numIterations
65+ val stepSize = params.stepSize
3866
39- // Load training data in LIBSVM format.
40- val data : RDD [LabeledPoint ] = sc.objectFile(inputPath )
67+ // Load training data in LabeledPoint format.
68+ val data : RDD [LabeledPoint ] = sc.objectFile(dataPath )
4169
4270 // Building the model
43- val numIterations = 100
44- val stepSize = 0.00000001
4571 val model = LinearRegressionWithSGD .train(data, numIterations, stepSize)
4672
4773 // Evaluate model on training examples and compute training error
0 commit comments