【发布时间】:2017-04-19 18:30:11
【问题描述】:
我的 spark 版本是 1.6
我的python版本是2.7
我的数据在下面,
x = [300,400,500,500,800,1000,1000,1300]
y = [9500,10300,11000,12000,12400,13400,14500,15300]
+----+-----+
| x| y|
+----+-----+
| 300| 9500|
| 400|10300|
| 500|11000|
| 500|12000|
| 800|12400|
|1000|13400|
|1000|14500|
|1300|15300|
+----+-----+
我的密码错误,
from pyspark.mllib.linalg import Vectors
from pyspark.sql import SQLContext
from pyspark.ml.regression import LinearRegression
sqlContext = SQLContext(sc)
#my data
x = [300,400,500,500,800,1000,1000,1300]
y = [9500,10300,11000,12000,12400,13400,14500,15300]
df = pd.DataFrame({'x':x, 'y':y})
df_spark=sqlCtx.createDataFrame(df)
lr = LinearRegression(maxIter=50, regParam=0.0, solver="normal", weightCol="weight")
model = lr.fit(df)
我想像这个例子一样运行:
>>> from pyspark.mllib.linalg import Vectors
>>> df = sqlContext.createDataFrame([
... (1.0, 2.0, Vectors.dense(1.0)),
... (0.0, 2.0, Vectors.sparse(1, [], []))], ["label", "weight", "features"])
>>> lr = LinearRegression(maxIter=5, regParam=0.0, solver="normal", weightCol="weight")
>>> model = lr.fit(df)
我可以弄清楚如何将我的数据转换为示例数据类型。
+-----+------+---------+
|label|weight| features|
+-----+------+---------+
| 1.0| 2.0| [1.0]|
| 0.0| 2.0|(1,[],[])|
+-----+------+---------+
我们将不胜感激。
谢谢您的帮助。
【问题讨论】:
-
你需要
VectorAssembler,看这里:stackoverflow.com/a/39505883/4964651 -
谢谢,让我试试。如果我成功了,我会回来并提供我的代码。
标签: python apache-spark pyspark