zoukankan      html  css  js  c++  java
  • 学习进度笔记

    学习进度笔记28

    回归算法

    import org.apache.log4j.{Level, Logger}

    import org.apache.spark.{SparkContext, SparkConf}

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

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

    import org.apache.spark.mllib.linalg.Vectors

    object LinearRegression {

      def main(args:Array[String]): Unit ={

        // 屏蔽不必要的日志显示终端上

        Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)

        Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)

        // 设置运行环境

        val conf = new SparkConf().setAppName("Kmeans").setMaster("local[4]")

        val sc = new SparkContext(conf)

        // Load and parse the data

        val data = sc.textFile("/home/hadoop/upload/class8/lpsa.data")

        val parsedData = data.map { line =>

          val parts = line.split(',')

          LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))

        }

        // Building the model

        val numIterations = 100

        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)

        sc.stop()

      }

    }

  • 相关阅读:
    执行.class文件
    Ant能干什么,编译?打包!
    C的随想
    微服务
    2018年宝鸡市高考复课报告会材料
    用图像解不等式
    高频易错题目01
    2018年宝鸡市二检数学题目解答
    点差法
    和事件的概率求法
  • 原文地址:https://www.cnblogs.com/xueqiuxiang/p/14467008.html
Copyright © 2011-2022 走看看