【问题标题】:why I got different result when I use weka,python and spark about linear regression?为什么当我使用 weka、python 和 spark 进行线性回归时得到不同的结果?
【发布时间】:2016-12-10 16:44:26
【问题描述】:

我用weka和python的线性回归预测了价格的数据。得到的结果和附件一样。linear-regression in weka linear-regression in python

我之前预测的数据如下:

英尺、床、浴室、车库、年龄、价格

1048,2,1,1,30,104900

1052,2,2,1,20,128750

1057,2,1,1,32,102900

1060,2,2,1,31,114900

1072,2,2,1,31,119500

1076,2,1,1,24,110500

但是当我使用 spark(1.6) mllib 分析这些数据时,我得到了不同的结果。我已经改变了 interation 和 stepsize 的值。但是结果与weka和python中的结果相差甚远。

我将数据格式化为以下类型。

104900,1048 2 1 1 30

128750,1052 2 2 1 20

102900,1057 2 1 1 32

而代码是这样的:

import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.regression.LinearRegressionModel
import org.apache.spark.mllib.regression.LinearRegressionWithSGD

// Load and parse the data
val data = sc.textFile("/root/data/house.data")
val parsedData = data.map { line =>
  val parts = line.split(',')
  LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))
}.cache()

// Building the model
val numIterations = 2000
val stepSize = 0.00000001
val algorithm = new LinearRegressionWithSGD()
algorithm.setIntercept(true)
algorithm.optimizer.setNumIterations(numIterations)
algorithm.optimizer.setStepSize(stepSize)

val model = algorithm.run(parsedData)

println(model.weights)
println(model.intercept)

【问题讨论】:

  • 我已尝试将数据标准化为以下格式。 (10.49,1.048 2 1 1 3) (12.875,1.052 2 2 1 2) (10.29,1.057 2 1 1 3.2) (11.49,1.06 2 2 1 3.1) (11.95,1.072 2 2 1 3.1) 而且,我试过不同的步长和迭代次数,足以获得稳定的结果。不幸的是,我得到的结果仍然与 weka 和 python 相去甚远。

标签: scikit-learn weka linear-regression apache-spark-mllib


【解决方案1】:

我的第一个猜测是您的stepSize 太小了。 首先,尝试 stepSize 0.1,检查结果是否发生变化。

Weka 和 Python 使用封闭形式的解决方案,因此结果是相同的。在 Spark 中,您使用 StochasticGradientDescent 进行迭代,因此 结果将不相同,但应该接近。

如果结果甚至不接近,请尝试在一些非常简单的数据上检查您的代码。例如,对于像 [(1,1), (2,2), (3,3), (4,4), ...] 这样的数据,你得到截距 0 和系数 1?您是否还检查了所有内容是否已正确解析以及您的 RDD 看起来如何?

【讨论】:

  • 感谢您的回复。我尝试了从(10e-10 到 10e2)的不同 stepSize,但我没有得到合适的答案。然后我尝试用标准化的数据格式化数据,如下格式。 10.49,1.048 2 1 1 3 12.875,1.052 2 2 1 2 10.29,1.057 2 1 1 3.2 11.49,1.06 2 2 1 3.1 11.95,1.072 2 2 1 3.1 还是没有得到合适的结果。
猜你喜欢
  • 2021-09-17
  • 2015-04-24
  • 2020-01-28
  • 2017-01-23
  • 2019-09-23
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多