spark mllib机器学习之五 LinearRegressionWithSGD
来源:互联网 发布:餐饮数据分析报告 编辑:程序博客网 时间:2024/04/28 03:07
数据格式:
8,1 5 2
125,90 30 5
0,0 0 0
92,22 50 20
999,333 333 333
50,12 26 12
60,25 25 10
8,1 5 2
867,537 97 233
672,55 216 401
1672,521 576 575
2229,699 799 731
1279,695 434 150
package com.agm.lgstc
import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.regression.LinearRegressionModel
import org.apache.spark.mllib.regression.LinearRegressionWithSGD
import org.apache.spark.mllib.linalg.Vectors
import java.io._
import org.apache.log4j.{Level, Logger}
object funpredict {
def main(args: Array[String]) {
val rand = new util.Random
var one = rand.nextInt(900)
var two = rand.nextInt(900)
var three = rand.nextInt(900)
var add = one+two+three
for(i<-0 to 150){
one = rand.nextInt(900)
two = rand.nextInt(900)
three = rand.nextInt(900)
add = one+two+three
// println(add+","+one+" "+two+" "+three)
}
Logger.getLogger("org").setLevel(Level.ERROR)
val path = new File(".").getCanonicalPath()
System.getProperties().put("hadoop.home.dir", path);
new File("./bin").mkdirs();
new File("./bin/winutils.exe").createNewFile();
val conf: SparkConf = new SparkConf().setAppName("LinearRegression").setMaster("local")
val sc = new SparkContext(conf)
// Load and parse the data
val data = sc.textFile("F:\\testData\\spark\\funpredict.txt")
val parsedData = data.map { line =>
val parts = line.split(',')
LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))
}.cache()
// parsedData.foreach(println)
// Building the model
val numIterations = 200
val stepSize = 0.00001
val model = LinearRegressionWithSGD.train(parsedData, numIterations,stepSize)
// var lr = new LinearRegressionWithSGD().setIntercept(true)
// val model = lr.run(parsedData)
//获取特征权重,及干扰特征
println("weights:%s, intercept:%s".format(model.weights,model.intercept))
//println(model.toPMML())
// Evaluate model on training examples and compute training error
val valuesAndPreds = parsedData.map { point =>
val prediction = model.predict(point.features)
(point.label, prediction)
}
println(model.predict(Vectors.dense(50,50,78)))
//计算 均方误差
val MSE = valuesAndPreds.map{case(v, p) => math.pow((v - p), 2)}.mean()
println("training Mean Squared Error = " + MSE)
// Save and load model
// model.save(sc, "myModelPath")
// val sameModel = LinearRegressionModel.load(sc, "myModelPath")
}
}
0 0
- spark mllib机器学习之五 LinearRegressionWithSGD
- Spark MLlib之机器学习(一)
- Spark MLlib之机器学习(二)
- Spark MLlib之机器学习(三)
- spark mllib机器学习之二 DecisionTree
- spark mllib机器学习之三 FPGrowth
- spark mllib机器学习之四 kmeans
- spark mllib机器学习之六 ALS
- spark mllib机器学习之七 TFIDF
- spark之MLlib机器学习-Kmeans
- spark之MLlib机器学习-线性回归
- Spark MLlib机器学习之朴素贝叶斯小试牛刀
- Spark学习之基于MLlib的机器学习
- Spark机器学习库mllib之协同过滤
- spark MLlib、ML机器学习之Logistic回归
- 【spark系列5】回归之LinearRegressionWithSGD
- Spark 0.9.1 MLLib 机器学习库
- Spark 0.9.1 MLLib 机器学习库
- Python三目运算符and or和if else以及注意事项
- 九度OJ--1047
- zabbix linuxagent搭建
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- 数据类型提升
- spark mllib机器学习之五 LinearRegressionWithSGD
- 来深圳一个多月的JAVA面试日记
- 虚方法(virtual)和抽象方法(abstract)的区别
- ++i与--i的区别
- 大部分面试碰到几率比较高的遍历dir的题目
- Handler使用中遇到的内存泄漏问题
- s3c2451gpio函数
- 关于html5 video currentTime 在chrome浏览器下设定失败问题解决
- tensorflow中的一些重要函数