14
我想火花数据帧转换使用以下代码来添加:AttributeError的:“数据帧”对象没有属性“地图”
from pyspark.mllib.clustering import KMeans
spark_df = sqlContext.createDataFrame(pandas_df)
rdd = spark_df.map(lambda data: Vectors.dense([float(c) for c in data]))
model = KMeans.train(rdd, 2, maxIterations=10, runs=30, initializationMode="random")
详细的错误信息是:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-a19a1763d3ac> in <module>()
1 from pyspark.mllib.clustering import KMeans
2 spark_df = sqlContext.createDataFrame(pandas_df)
----> 3 rdd = spark_df.map(lambda data: Vectors.dense([float(c) for c in data]))
4 model = KMeans.train(rdd, 2, maxIterations=10, runs=30, initializationMode="random")
/home/edamame/spark/spark-2.0.0-bin-hadoop2.6/python/pyspark/sql/dataframe.pyc in __getattr__(self, name)
842 if name not in self.columns:
843 raise AttributeError(
--> 844 "'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
845 jc = self._jdf.apply(name)
846 return Column(jc)
AttributeError: 'DataFrame' object has no attribute 'map'
有人知道我在这里做错了吗?谢谢!
请记住,MLLIB是围绕RDD构建的,而ML通常是围绕数据框构建的。由于你似乎在使用Spark 2.0,我建议你从ML中查找KMeans:https://spark.apache.org/docs/latest/ml-clustering.html – Jeff
@JeffL:我检查了ml,我注意到输入必须是数据集,而不是数据框。所以我们需要做另一层转换来将数据框转换为数据集才能使用ml? – Edamame
我不再100%清楚这个区别,尽管在Python中我相信它几乎没有实际意义。事实上,如果您浏览github代码,则在1.6.1中,各种数据框方法位于数据框模块中,而在2.0中,这些相同方法位于数据集模块中,并且没有数据框模块。所以我不认为你会面对数据框和数据集之间的任何转换问题,至少在Python API中。 – Jeff