从numpy矩阵创建Spark数据帧

Jan*_*ila 13 numpy apache-spark apache-spark-sql pyspark apache-spark-mllib

这是我第一次使用PySpark(Spark 2),我正在尝试为Logit模型创建一个玩具数据帧.我成功运行了教程,并希望将自己的数据传递给它.

我试过这个:

%pyspark
import numpy as np
from pyspark.ml.linalg import Vectors, VectorUDT
from pyspark.mllib.regression import LabeledPoint

df = np.concatenate([np.random.randint(0,2, size=(1000)), np.random.randn(1000), 3*np.random.randn(1000)+2, 6*np.random.randn(1000)-2]).reshape(1000,-1)
df = map(lambda x: LabeledPoint(x[0], Vectors.dense(x[1:])), df)

mydf = spark.createDataFrame(df,["label", "features"])
Run Code Online (Sandbox Code Playgroud)

但我无法摆脱:

TypeError: Cannot convert type <class 'pyspark.ml.linalg.DenseVector'> into Vector
Run Code Online (Sandbox Code Playgroud)

我正在使用ML库作为向量,输入是一个双数组,所以请问有什么问题?根据文档应该没问题.

非常感谢.

Jef*_*dez 7

从Numpy到Pandas到Spark:

spark.createDataFrame(pd.DataFrame(np.random.rand(4,4),columns=list('abcd'))).show()

输出: +-------------------+-------------------+------------------+-------------------+ | a| b| c| d| +-------------------+-------------------+------------------+-------------------+ | 0.8026427193838694|0.16867056812634307|0.2284873209015007|0.17141853164400833| | 0.2559088794287595| 0.3896957084615589|0.3806810025185623| 0.9362280141470332| |0.41313827425060257| 0.8087580640179158|0.5547653674054028| 0.5386190454838264| | 0.2948395900484454| 0.4085807623354264|0.6814694724946697|0.32031773805256325| +-------------------+-------------------+------------------+-------------------+

  • 这是最好的答案,至少它对我来说就像一个魅力! (2认同)

des*_*aut 5

您正在混合ML和MLlib的功能,这些功能不一定兼容。LabeledPoint使用时不需要spark-ml

sc.version
# u'2.1.1'

import numpy as np
from pyspark.ml.linalg import Vectors

df = np.concatenate([np.random.randint(0,2, size=(1000)), np.random.randn(1000), 3*np.random.randn(1000)+2, 6*np.random.randn(1000)-2]).reshape(1000,-1)
dff = map(lambda x: (int(x[0]), Vectors.dense(x[1:])), df)

mydf = spark.createDataFrame(dff,schema=["label", "features"])

mydf.show(5)
# +-----+-------------+ 
# |label|     features| 
# +-----+-------------+ 
# |    1|[0.0,0.0,0.0]| 
# |    0|[0.0,1.0,1.0]| 
# |    0|[0.0,1.0,0.0]| 
# |    1|[0.0,0.0,1.0]| 
# |    0|[0.0,1.0,0.0]|
# +-----+-------------+
Run Code Online (Sandbox Code Playgroud)

PS:从Spark 2.0开始,spark.mllib软件包中基于RDD的API已进入维护模式。Spark的主要机器学习API现在是spark.ml软件包中基于DataFrame的API。[参考]