【spark系列5】回归之LinearRegressionWithSGD

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 05:55   15   0

其它步骤请参考上一篇博客,转载请注明地址。


scala程序

import org.apache.spark.mllib.regression.LinearRegressionWithSGD
import org.apache.spark.mllib.regression.LabeledPoint

object ObLinearRegressionWithSGD{
    def run() {
        // Load and parse the data file
        val data = sc.textFile("D:/schoolar_tool/spark-0.9.1/mllib/data/ridge-data/lpsa.data")
        val parsedData = data.map { line =>
          val parts = line.split(',')
          LabeledPoint(parts(0).toDouble, parts(1).split(' ').map(x => x.toDouble).toArray)
        }

        // Building the model
        val numIterations = 20
        val model = LinearRegressionWithSGD.train(parsedData, numIterations)

        // Evaluate model on training examples and compute training error
        val valuesAndPreds = parsedData.map { point =>
          val prediction = model.predict(point.features)
          (point.label, prediction)
        }
        val MSE = valuesAndPreds.map{ case(v, p) => math.pow((v - p), 2)}.reduce(_ + _)/valuesAndPreds.count
        println("training Mean Squared Error = " + MSE)
    }
}


运行

ObLinearRegressionWithSGD.run

效果



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP