我是新手,尝试使用MLlib-文档示例中的NaiveBayes。我尝试导入NaiveBayes,但出现以下错误,提示它中没有训练方法。我不确定该如何进行?如果您有任何输入,这将有所帮助。

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.classification.NaiveBayes


object NaiveBayes {

def main(args: Array[String]){

val conf = new SparkConf().setMaster("local[1]").setAppName("NaiveBayesExample")
val sc = new SparkContext(conf)

val data = sc.textFile("/Users/Desktop/Studies/sample_naive_bayes_data.txt")
val parsedData = data.map { line =>
  val parts = line.split(',')
  LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))
}

// Split data into training (60%) and test (40%).
val splits = parsedData.randomSplit(Array(0.6, 0.4), seed = 11L)
val training = splits(0)
val test = splits(1)

val model = NaiveBayes.train(training, lambda = 1.0)

val predictionAndLabel = test.map(p => (model.predict(p.features), p.label))
val accuracy = 1.0 * predictionAndLabel.filter(x => x._1 == x._2).count() / test.count()

println("Accuracy = " + accuracy * 100 + "%")

}
 }


错误:

 Error:(26, 28) value train is not a member of object NaiveBayes
    val model = NaiveBayes.train(training, lambda = 1.0)
                       ^
 Error:(29, 59) value _1 is not a member of Nothing
   val accuracy = 1.0 * predictionAndLabel.filter(x => x._1 == x._2).count() / test.count()
                                                      ^

最佳答案

在您的程序中,您正在重新定义对象NaiveBayes,以便spark无法访问mllib对象。
object NaiveBayes重命名为object MyNaiveBayes以防止这种情况。

关于apache-spark - 值列不是对象NaiveBayes的成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36962767/

10-12 17:42