Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement naive Bayesian algorithm in spark mllib

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report