Astros
Astros

Reputation: 1

Value train not a member of object NaiveBayes

I am new to spark and trying to use MLlib - NaiveBayes from the documentation example. I tried to import NaiveBayes but I get the below error mentioning it doesnt have train method in it. I am not sure how to proceed with this? If you have any inputs, it would be helpful.

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:

 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()
                                                      ^

Upvotes: 0

Views: 262

Answers (1)

Christian Hirsch
Christian Hirsch

Reputation: 2056

In your program you are re-defining the object NaiveBayes so that spark cannot access the mllib object. Rename object NaiveBayes into object MyNaiveBayes to prevent this.

Upvotes: 2

Related Questions