Pyspark,决策树(Spark 2.0.0)

鲁斯兰

我是新来的火花(使用pyspark)。我尝试从此处(链接)运行决策树教程我执行代码:

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失败:列要素必须为org.apache.spark.ml.linalg.VectorUDT@3bfc3ba7类型,但实际上为org.apache.spark.mllib.linalg.VectorUDT@f71b0bce。

谷歌搜索此错误时,我找到了一个回答,说:

use from pyspark.ml.linalg import Vectors, VectorUDT 
instead of 
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 1.5.2。spark 2.0.0上的示例(请参见下面对spark 2.0示例的引用)。

spark.ml和spark.mllib之间的区别

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

可在此处找到更多详细信息:http : //spark.apache.org/docs/latest/ml-guide.html

使用Spark 2.0,请尝试使用Spark 2.0.0示例https://spark.apache.org/docs/2.0.0/mllib-decision-tree.html

from pyspark.mllib.tree import DecisionTree, DecisionTreeModel
from pyspark.mllib.util import MLUtils

# Load and parse the data file into an RDD of LabeledPoint.
data = MLUtils.loadLibSVMFile(sc, 'data/mllib/sample_libsvm_data.txt')
# Split the data into training and test sets (30% held out for testing)
(trainingData, testData) = data.randomSplit([0.7, 0.3])

# Train a DecisionTree model.
#  Empty categoricalFeaturesInfo indicates all features are continuous.
model = DecisionTree.trainClassifier(trainingData, numClasses=2, categoricalFeaturesInfo={},
                                     impurity='gini', maxDepth=5, maxBins=32)

# Evaluate model on test instances and compute test error
predictions = model.predict(testData.map(lambda x: x.features))
labelsAndPredictions = testData.map(lambda lp: lp.label).zip(predictions)
testErr = labelsAndPredictions.filter(lambda (v, p): v != p).count() / float(testData.count())
print('Test Error = ' + str(testErr))
print('Learned classification tree model:')
print(model.toDebugString())

# Save and load model
model.save(sc, "target/tmp/myDecisionTreeClassificationModel")
sameModel = DecisionTreeModel.load(sc, "target/tmp/myDecisionTreeClassificationModel")

在Spark存储库中的“ examples / src / main / python / mllib / decision_tree_classification_example.py”中找到完整的示例代码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在决策树中使用交叉熵而不是0/1损失

我希望输出像[[0,0,0,0,],[0,1,0,0],[0,2,0,0],[0,3,0,0]]

使用H2O实施决策树

在R中将(0,1,0,0,1,1,1)转换为(0,0,0,1,0,1,2)

0 < 2 = ...假?

如何使用a [0]-a [0] a [1]-a [0] a [1] a [2]输出数组

2 ^ 0 * 2和(2 ^ 0)* 2之间的区别?

CSS - 框阴影 - 什么是 0+0+0+0+2in#color

如何创建从 (0,0,0) 到 (2,2,2) 的元组,条目最多为 2?

在以 2 开头的行前添加 0

索引 = 2 不在 [0, 1)

积分2不应返回0

用于决策树的spark数据分区

numpy-为什么Z [(0,2)]是视图而Z [(0,2),(0)]是副本?

显示输入的最大%2 == 0数字的C程序,直到您输入数字%2!= 0 &&%7 == 0

该代码的含义([1、2、3] || 0)[0]

scipy的randint(0,2)的ppf(0)是-1.0

使用matplotlib python绘制决策树分类器的2个以上功能

如何可视化Spark(pyspark)中的决策树模型?

“%%2:”和“ number%2 == 0”之间的区别?

tensorflow除以0/0 =:0

是否可以安全地假设atan2(0,0)返回0?

二叉树:0、1或2个子节点的节点频率计数

使用Scala收集方法可帮助将[0,0,0,1,1,1,1,0,0,1,1]的列表转换为[3,4,2,2]

Javascript正则表达式/ ^(19 | 20)([0-9] {2})-([0-9] {2} | 0 [0-9] {1})-([0-9] {2} | 0 [0-9] {1})$ / g未找到日期作为输入值

修剪决策树

决策树深度

Android - 获取数字的索引 #0 、 #1 、 #2

除以2的数字将返回0