前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住给大家分享一下。点击跳转到网站:https://www.captainai.net/dongkelun
前言
本文是讲如何使用spark ml进行线性回归,不涉及线性回归的原理。
1、数据格式
1.1 普通标签格式
1.1.1 格式为:
1 | 标签,特征值1 特征值2 特征值3... |
1 | 1,1.9 |
1.1.2 spark 读取
1、Rdd
旧版(mllib)的线性回归要求传入的参数类型为RDD[LabeledPoint]1
2
3
4
5
6
7
8
9
10
11
12import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
val data_path = "files/ml/linear_regression_data1.txt"
val data = sc.textFile(data_path)
val training = data.map { line =>
val arr = line.split(',')
LabeledPoint(arr(0).toDouble, Vectors.dense(arr(1).split(' ').map(_.toDouble)))
}.cache()
training.foreach(println)
结果:1
2
3
4
5
6
7(1.0,[1.9])
(2.0,[3.1])
(3.0,[4.0])
(3.5,[4.45])
(4.0,[5.02])
(9.0,[9.97])
(-2.0,[-0.98])
一共有两列,第一列可以通过.label获得(类型为Double),第二列可以通过.features获得(类型为Vector[Double])
2、 DataFrame
新版(ml)的线性回归要求传入的参数类型为Dataset[_]1
2
3
4
5
6
7
8
9
10
11import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.sql.Row
import spark.implicits._
val data_path = "files/ml/linear_regression_data1.txt"
val data = spark.read.text(data_path)
val training = data.map {
case Row(line: String) =>
var arr = line.split(',')
(arr(0).toDouble, Vectors.dense(arr(1).split(' ').map(_.toDouble)))
}.toDF("label", "features")
training.show()
结果:1
2
3
4
5
6
7
8
9
10
11+-----+--------+
|label|features|
+-----+--------+
| 1.0| [1.9]|
| 2.0| [3.1]|
| 3.0| [4.0]|
| 3.5| [4.45]|
| 4.0| [5.02]|
| 9.0| [9.97]|
| -2.0| [-0.98]|
+-----+--------+
其中列名”label”, “features”固定,不能改为其他列名。
1.2 LIBSVM格式
1.2.1 格式为:
1 | label index1:value1 index2:value2 ... |
其中每一行的index必须为升序
为了便于理解,造几条多维数据:1
2
3
4
5
6
71 1:1.9 2:2 4:2 100:3 101:6
2 1:3.1 2:2 4:2 100:3 101:6
3 1:4 2:2 4:2 100:3 101:6
3.5 1:4.45 2:2 4:2 100:3 101:6
4 1:5.02 2:2 4:2 100:3 101:6
9 1:9.97 4:2 100:3 101:6
-2 1:-0.98 2:2 4:2 100:3 201:6
1.2.2 spark 读取
1、Rdd1
2
3
4
5
6import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.mllib.util.MLUtils
val data_path = "files/ml/linear_regression_data2.txt"
val training = MLUtils.loadLibSVMFile(sc, data_path)
training.foreach(println)
结果:1
2
3
4
5
6
7(1.0,(201,[0,1,3,99,100],[1.9,2.0,2.0,3.0,6.0]))
(2.0,(201,[0,1,3,99,100],[3.1,2.0,2.0,3.0,6.0]))
(3.0,(201,[0,1,3,99,100],[4.0,2.0,2.0,3.0,6.0]))
(3.5,(201,[0,1,3,99,100],[4.45,2.0,2.0,3.0,6.0]))
(4.0,(201,[0,1,3,99,100],[5.02,2.0,2.0,3.0,6.0]))
(9.0,(201,[0,3,99,100],[9.97,2.0,3.0,6.0]))
(-2.0,(201,[0,1,3,99,200],[-0.98,2.0,2.0,3.0,6.0]))
返回类型为RDD[LabeledPoint],其中第一列为label,第二列vector的第一个值为max(index),第二个index-1组成的数组,第三个为value组成的数组。
2、DataFrame1
2
3
4val data_path = "files/ml/linear_regression_data2.txt"
val data = spark.read.text(data_path)
val training = spark.read.format("libsvm").load(data_path)
training.show(false)
结果:1
2
3
4
5
6
7
8
9
10
11+-----+--------------------------------------------+
|label|features |
+-----+--------------------------------------------+
|1.0 |(201,[0,1,3,99,100],[1.9,2.0,2.0,3.0,6.0]) |
|2.0 |(201,[0,1,3,99,100],[3.1,2.0,2.0,3.0,6.0]) |
|3.0 |(201,[0,1,3,99,100],[4.0,2.0,2.0,3.0,6.0]) |
|3.5 |(201,[0,1,3,99,100],[4.45,2.0,2.0,3.0,6.0]) |
|4.0 |(201,[0,1,3,99,100],[5.02,2.0,2.0,3.0,6.0]) |
|9.0 |(201,[0,3,99,100],[9.97,2.0,3.0,6.0]) |
|-2.0 |(201,[0,1,3,99,200],[-0.98,2.0,2.0,3.0,6.0])|
+-----+--------------------------------------------+
2、线性回归代码
2.1 数据
用libsvm格式的数据:1
2
3
4
5
6
71 1:1.9
2 1:3.1
3 1:4
3.5 1:4.45
4 1:5.02
9 1:9.97
-2 1:-0.98
2.2 旧版代码
1 | package com.dkl.leanring.spark.ml |
其中注释的第30行代码为书上的写法,但这样写intercept一直为0,也就是只适用于y=a*x的形式,不适用于y=ax+b,改为31、32替代即可。
结果:1
2
3
4
5
6
7
8
9
10
11[0.992894785953067]
-0.9446037936869749
prediction label
0.9418962996238525 1.0
2.133370042767533 2.0
3.0269753501252934 3.0
3.473778003804174 3.5
4.039728031797421 4.0
8.954557222265104 9.0
-1.9176406839209805 -2.0
Test RMSE = 0.06866615969192089.
即a=0.992894785953067,b=-0.9446037936869749,y=0.992894785953067*x-0.9446037936869749
2.3 新版代码
1 | package com.dkl.leanring.spark.ml |
结果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28Coefficients: [0.9072296333951224] Intercept: -0.630360819004294
numIterations: 3
objectiveHistory: [0.5,0.41543560544030766,0.08269406021049913]
+--------------------+
| residuals|
+--------------------+
| -0.0933754844464385|
|-0.18205104452058585|
|0.001442285423804...|
| 0.09318895039599973|
| 0.07606805936077965|
| 0.5852813740549223|
| -0.4805541402684861|
+--------------------+
RMSE: 0.2999573166705823
r2: 0.9906296595124621
+-----+---------------+------------------+
|label| features| prediction|
+-----+---------------+------------------+
| 1.0| (1,[0],[1.9])|1.0933754844464385|
| 2.0| (1,[0],[3.1])| 2.182051044520586|
| 3.0| (1,[0],[4.0])|2.9985577145761955|
| 3.5| (1,[0],[4.45])|3.4068110496040003|
| 4.0| (1,[0],[5.02])|3.9239319406392204|
| 9.0| (1,[0],[9.97])| 8.414718625945078|
| -2.0|(1,[0],[-0.98])|-1.519445859731514|
+-----+---------------+------------------+