Spark MLlib线性回归代码实现及结果展示

BitTigerio 2018-03-08

代码实现:
importorg.apache.spark.sql.SparkSession
importorg.apache.spark.sql.DataFrame
importorg.apache.spark.ml.feature.VectorAssembler
importorg.apache.spark.ml.regression.LinearRegression
/**
* Created by zhen on 2018/3/10.
*/
objectLinearRegression {

defmain(args: Array[String]) {
//设置环境
valspark = SparkSession.builder ().appName ("SendBroadcast").master ("local[2]").getOrCreate()
valsc = spark.sparkContext
valsqlContext = spark.sqlContext
//准备训练集合

valraw_data = sc.textFile("src/sparkMLlib/man.txt")
valmap_data = raw_data.map{x=>
valmid = x.replaceAll(","," ,")
valsplit_list = mid.substring(0,mid.length-1).split(",")
for(x <- 0 until split_list.length){
if(split_list(x).trim.equals("")) split_list(x) = "0.0" elsesplit_list(x) = split_list(x).trim
}
(split_list(1).toDouble,split_list(2).toDouble,split_list(3).toDouble,split_list(4).toDouble,
split_list(5).toDouble,split_list(6).toDouble,split_list(7).toDouble,split_list(8).toDouble,
split_list(9).toDouble,split_list(10).toDouble,split_list(11).toDouble)
}
valmid = map_data.sample(false,0.6,0)//随机取样,训练模型
valdf = sqlContext.createDataFrame(mid)
valcolArray = Array("c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11")
valdata = df.toDF("c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11")
valassembler = newVectorAssembler().setInputCols(colArray).setOutputCol("features")
valvecDF = assembler.transform(data)
//准备预测集合
valmap_data_for_predict = map_data
valdf_for_predict = sqlContext.createDataFrame(map_data_for_predict)
valdata_for_predict = df_for_predict.toDF("c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11")
valcolArray_for_predict = Array("c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11")
valassembler_for_predict = newVectorAssembler().setInputCols(colArray_for_predict).setOutputCol("features")
valvecDF_for_predict: DataFrame = assembler_for_predict.transform(data_for_predict)
// 建立模型,进行预测
// 设置线性回归参数
vallr1 = newLinearRegression()
vallr2 = lr1.setFeaturesCol("features").setLabelCol("c5").setFitIntercept(true)
// RegParam:正则化
vallr3 = lr2.setMaxIter(10).setRegParam(0.3).setElasticNetParam(0.8)
// 将训练集合代入模型进行训练
vallrModel = lr3.fit(vecDF)
// 输出模型全部参数
lrModel.extractParamMap()
//coefficients 系数 intercept 截距
println(s"Coefficients: ${lrModel.coefficients}Intercept: ${lrModel.intercept}")
// 模型进行评价
valtrainingSummary = lrModel.summary
trainingSummary.residuals.show()
println(s"均方根差: ${trainingSummary.rootMeanSquaredError}")//RMSE:均方根差
println(s"判定系数: ${trainingSummary.r2}")//r2:判定系数,也称为拟合优度,越接近1越好
valpredictions = lrModel.transform(vecDF_for_predict)
valpredict_result = predictions.selectExpr("features","c5", "round(prediction,1) as prediction")
predict_result.rdd.saveAsTextFile("src/sparkMLlib/manResult")
sc.stop()
}
}

性能评估:

均方根差: 0.2968176690349843
判定系数: 0.9715059814474793

结果:

[[4.61,1.51,5.91,4.18,3.91,0.0,7.83,0.0,4.81,4.71,3.44,0.0,3.61,3.76],1.51,1.7]
[[3.1,3.64,1.6,2.57,3.16,0.0,5.6,0.0,1.84,2.77,0.0,2.4,0.0,2.53],3.64,3.4]
[[3.15,4.24,2.89,1.94,3.81,0.0,6.12,0.0,0.0,0.0,2.23,0.0,2.51,3.98],4.24,3.9]
[[2.13,3.81,3.5,3.29,3.47,0.0,0.0,0.0,2.16,2.06,1.65,0.0,3.37,3.93],3.81,3.6]
[[3.6,4.36,2.89,3.46,3.66,0.0,7.17,0.0,2.86,2.58,0.0,2.73,2.73,3.94],4.36,4.0]
[[2.65,3.58,3.9,3.63,2.71,0.0,5.91,0.0,3.63,3.08,2.33,0.0,1.79,2.54],3.58,3.4]
[(14,[0,1,2,3,4,6,8,9],[2.13,2.7,2.26,1.78,2.82,7.15,2.69,2.46]),2.7,2.6]
[[2.31,2.42,4.0,3.27,3.69,0.0,5.87,0.0,0.0,0.0,1.32,0.0,1.32,2.09],2.42,2.4]
[(14,[0,1,2,3,6,10,12,13],[3.4,4.12,3.04,2.76,9.55,1.44,3.61,3.95]),4.12,3.8]

相关推荐