我在尝试使用DecisionTreeClassifier.train()时一直提示如下错误,说当前对象不是classification包的子类,因此不能调用他的protected属性方法,但是官方文档是public类型的,因此不知道哪里出错了
Error:(218, 41) method train in class DecisionTreeClassifier cannot be accessed in org.apache.spark.ml.classification.DecisionTreeClassifier
Access to protected method train not permitted because
enclosing object FeatureSelection in package core is not a subclass of
class DecisionTreeClassifier in package classification where target is defined
val dt = decisionTreeClassifier.train(trainRdd)
环境为scala 2.10.6 spark2.10:1.6.1 jdk1.8,相关代码如下:
import org.apache.spark.ml.classification.DecisionTreeClassifier
object FeatureSelection {
val selectfeature=ArrayBuffer[String]()
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 decisionTreeClassifier = new DecisionTreeClassifier()
.setMaxBins(maxBins)
.setImpurity(impurity)
.setFeaturesCol("features")
.setLabelCol("label")
.setMaxDepth(maxDepth)
val dt = decisionTreeClassifier.train(trainRdd, categoricalFeaturesInfo, numClasses)
}