【发布时间】:2016-10-09 10:30:46
【问题描述】:
在尝试使用 Python 使用 Spark mllib 的 LinearRegressionWithSGD 进行线性回归时,我得到了非常糟糕的结果。
我研究了类似的问题,如下所示:
- Spark - MLlib linear regression intercept and weight NaN
- https://stackoverflow.com/questions/34940225/spark-mllib-python-linear-regression-with-sgd-not-getting-accurate-weights-for-s
- Spark MlLib linear regression (Linear least squares) giving random results
我很清楚,关键是调整参数恰到好处。
我也知道随机梯度下降不一定会找到最佳解决方案(就像交替最小二乘法那样),因为它有可能陷入局部最小值。但至少我希望找到一个好的模型。
这是我的设置,我选择使用 Journal of Statistics education 中的this example 和对应的dataset。我从这篇论文(以及在 JMP 中复制结果)知道,如果我只使用数值字段,我应该得到类似于以下等式的结果(R^2 约为 44%,RMSE 约为 7400):
价格 = 7323 - 0.171 里程 + 3200 气缸 - 1463 门 + 6206 巡航 - 2024 声音 + 3327 皮革
由于我不知道如何设置参数正好,所以我运行了以下蛮力方法:
from collections import Iterable
from pyspark import SparkContext
from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.regression import LinearRegressionWithSGD
from pyspark.mllib.evaluation import RegressionMetrics
def f(n):
return float(n)
if __name__ == "__main__":
sc = SparkContext(appName="LinearRegressionExample")
# CSV file format:
# 0 1 2 3 4 5 6 7 8 9 10 11
# Price, Mileage, Make, Model, Trim, Type, Cylinder, Liter, Doors, Cruise, Sound, Leather
raw_data = sc.textFile('file:///home/ccastroh/training/pyspark/kuiper.csv')
# Grabbing numerical values only (for now)
data = raw_data \
.map(lambda x : x.split(',')) \
.map(lambda x : [f(x[0]), f(x[1]), f(x[6]), f(x[8]), f(x[9]), f(x[10]), f(x[11])])
points = data.map(lambda x : LabeledPoint(x[0], x[1:])).cache()
print "Num, Iterations, Step, MiniBatch, RegParam, RegType, Intercept?, Validation?, " + \
"RMSE, R2, EXPLAINED VARIANCE, INTERCEPT, WEIGHTS..."
i = 0
for ite in [10, 100, 1000]:
for stp in [1, 1e-01, 1e-02, 1e-03, 1e-04, 1e-05, 1e-06, 1e-07, 1e-08, 1e-09, 1e-10]:
for mini in [0.2, 0.4, 0.6, 0.8, 1.0]:
for regP in [0.0, 0.1, 0.01, 0.001]:
for regT in [None, 'l1', 'l2']:
for intr in [True]:
for vald in [False, True]:
i += 1
message = str(i) + \
"," + str(ite) + \
"," + str(stp) + \
"," + str(mini) + \
"," + str(regP) + \
"," + str(regT) + \
"," + str(intr) + \
"," + str(vald)
model = LinearRegressionWithSGD.train(points, iterations=ite, step=stp, \
miniBatchFraction=mini, regParam=regP, regType=regT, intercept=intr, \
validateData=vald)
predictions_observations = points \
.map(lambda p : (float(model.predict(p.features)), p.label)).cache()
metrics = RegressionMetrics(predictions_observations)
message += "," + str(metrics.rootMeanSquaredError) \
+ "," + str(metrics.r2) \
+ "," + str(metrics.explainedVariance)
message += "," + str(model.intercept)
for weight in model.weights:
message += "," + str(weight)
print message
sc.stop()
如您所见,我基本上运行了 3960 种不同的变体。在这些中,我没有得到任何与论文或 JMP 中的公式完全相似的东西。以下是一些亮点:
- 在很多运行中,我的截距和权重都得到了 NaN
- 我得到的最高 R^2 是 -0.89。我什至不知道你会得到一个负的 R^2。结果是负值表示选择的模型fits worse than a horizontal line。
- 我得到的最低 RMSE 是 13600,比预期的 7400 差很多。
我也尝试了normalizing the values,以便在 [0,1] 范围内,但这也没有帮助
有没有人知道如何获得一半体面的线性回归模型?我错过了什么吗?
【问题讨论】:
标签: python apache-spark pyspark linear-regression apache-spark-mllib