我在尝试使用DecisionTreeClassifier.train()时,出现了以下错误报告:
错误:(218, 41) 在org.apache.spark.ml.classification.DecisionTreeClassifier类中无法访问train方法,因为封闭对象FeatureSelection在core包中不是classification包中定义目标的DecisionTreeClassifier类的子类,无法访问受保护的方法train val dt = decisionTreeClassifier.train(trainRdd)
报告指出我的对象FeatureSelection
不是classification包的子类,因此无法调用该包的受保护方法。但实际上,官方文档中train()是一个公共类型的函数。
环境:Scala 2.10.6 Spark 2.10:1.6.1 jdk 1.8
附上代码如下:
import org.apache.spark.ml.classification.DecisionTreeClassifierobject FeatureSelection { def selectFeatureGreedyDTNoLimit(){ val selectfeature=ArrayBuffer[String]() val selectsize=selectfeature.size val tempfeature=selectfeature++ArrayBuffer(line) val vectorDF = new VectorAssembler() .setInputCols(tempfeature.toArray) .setOutputCol("features") .transform(tempdf) .select("label", "features") val Array(trainRdd, testRdd) = vectorDF .rdd .map(row => LabeledPoint(Common.any2Double(row.get(0)).get, row.getAs[Vector](1))) .randomSplit(Array(0.5, 0.5), 0L) val numClasses = 2 val categoricalFeaturesInfo = Map[Int, Int]() val dt = decisionTreeClassifier.train(trainRdd, categoricalFeaturesInfo, numClasses) }}
希望有人能帮助我解决这个问题。
回答:
您应该使用方法fit
。
train
是一个内部函数,这就是它被保护的原因。