【发布时间】:2017-03-12 18:48:41
【问题描述】:
我是 Spark 的新手(使用 PySpark)。我尝试从here (link) 运行决策树教程。我执行代码:
from pyspark.ml import Pipeline
from pyspark.ml.classification import DecisionTreeClassifier
from pyspark.ml.feature import StringIndexer, VectorIndexer
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.mllib.util import MLUtils
# Load and parse the data file, converting it to a DataFrame.
data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt").toDF()
labelIndexer = StringIndexer(inputCol="label", outputCol="indexedLabel").fit(data)
# Now this line fails
featureIndexer =\
VectorIndexer(inputCol="features", outputCol="indexedFeatures", maxCategories=4).fit(data)
我收到错误消息:
IllegalArgumentException: u'requirement failed: 列特征必须是 org.apache.spark.ml.linalg.VectorUDT@3bfc3ba7 类型,但实际上是 org.apache.spark.mllib.linalg.VectorUDT@f71b0bce。'
在网上搜索此错误时,我找到了一个答案:
使用
from pyspark.ml.linalg import Vectors, VectorUDT
而不是from pyspark.mllib.linalg import Vectors, VectorUDT
这很奇怪,因为我没有使用它。此外,将此导入添加到我的代码中并没有解决任何问题,我仍然遇到同样的错误。
我不太清楚如何调试这种情况。在查看原始数据时,我看到:
data.show()
+--------------------+-----+
| features|label|
+--------------------+-----+
|(692,[127,128,129...| 0.0|
|(692,[158,159,160...| 1.0|
|(692,[124,125,126...| 1.0|
|(692,[152,153,154...| 1.0|
这看起来像一个列表,以'('开头。
我不知道如何解决这个问题,甚至调试。
【问题讨论】:
-
你用的是哪个版本的spark?
-
我使用的是 Spark 2.0.0
标签: apache-spark dataframe pyspark apache-spark-sql decision-tree