How to implement naive Bayesian algorithm in spark mllib
This article mainly introduces how to implement naive Bayesian algorithm in spark mllib, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
Data Source Description
Labels for each row in the first column, features in the other columns
The runtime code is as follows package spark.logisticRegressionimport org.apache.spark.mllib.classification.NaiveBayesimport org.apache.spark.mllib.linalg.Vectorsimport org.apache.spark.mllib.util.MLUtilsimport org. apache.spark. {SparkConf, SparkContext}/** * Classification-Naive Bayes Simple Example * Posteriori Probability = Prior Probability x Adjustment Factor * Created by eric on 16-7-18. */object Bayes { val conf = new SparkConf() //Create environment variables .setMaster("local") //Set localization processing .setAppName("Bayes") //setName val sc = new SparkContext(conf) def main(args: Array[String]) { val data = MLUtils.loadLabeledPoints(sc, "./ src/main/spark/logisticRegression/bayes.txt") val model = NaiveBayes.train(data, 1.0) model.labels.foreach(println)//print label(labels are label categories) model.pi.foreach(println)//Print prior probabilities (pi stores prior probabilities for each label) //0.0 //1.0 //2.0 //-1.0986122886681098 //-1.0986122886681098 //-1.0986122886681098 val test = Vectors.dense(0, 0, 10)//new prediction data val result = model.predict(test)//predict result println(result)//2.0 }}
bayes.txt
0,1 0 00,2 0 01,0 1 01,0 2 02,0 0 12,0 0 2 The results are as shown in the figure
Thank you for reading this article carefully. I hope that the article "How to Implement Naive Bayesian Algorithm in spark mllib" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!