java mllib 算法_朴素贝叶斯算法原理及Spark MLlib实例(Scala/Java/Python)

本文介绍了朴素贝叶斯算法的基本原理,并详细阐述了如何在Apache Spark的MLlib库中使用Scala、Java和Python实现朴素贝叶斯分类。通过加载LIBSVM格式的数据,将数据划分为训练和测试集,训练模型并评估其在测试集上的准确性。
摘要由CSDN通过智能技术生成

朴素贝叶斯

算法介绍:

朴素贝叶斯法是基于贝叶斯定理与特征条件独立假设的分类方法。

朴素贝叶斯的思想基础是这样的:对于给出的待分类项,求解在此项出现的条件下各个类别出现的概率,在没有其它可用信息下,我们会选择条件概率最大的类别作为此待分类项应属的类别。

朴素贝叶斯分类的正式定义如下:

1、设

gif.latex?x%20=%20%5Cleft%5C%7B%20%7B%7Ba_1%7D,%7Ba_2%7D,%20%5Cldots%20,%7Ba_m%7D%7D%20%5Cright%5C%7D 为一个待分类项,而每个a为x的一个特征属性。

2、有类别集合

gif.latex?C%20=%20%5Cleft%5C%7B%20%7B%7By_1%7D,%7By_2%7D,%20%5Cldots%20,%7By_n%7D%7D%20%5Cright%5C%7D 。

3、计算

gif.latex?P%5Cleft(%20%7B%7By_1%7D%5Cleft%7C%20x%20%5Cright.%7D%20%5Cright),P(%7By_2%7D%5Cleft%7C%20x%20%5Cright.),%20%5Cldots%20,P(%7By_n%7D%5Cleft%7C%20x%20%5Cright.) 。

4、如果

gif.latex?P%5Cleft(%20%7B%7By_k%7D%5Cleft%7C%20x%20%5Cright.%7D%20%5Cright)%20=%20max%5C%7B%20P%5Cleft(%20%7B%7By_1%7D%5Cleft%7C%20x%20%5Cright.%7D%20%5Cright),P%5Cleft(%20%7B%7By_2%7D%5Cleft%7C%20x%20%5Cright.%7D%20%5Cright),%20%5Cldots%20,P%5Cleft(%20%7B%7By_n%7D%5Cleft%7C%20x%20%5Cright.%7D%20%5Cright)%5C%7D ,则

gif.latex?x%20%5Cin%20%7By_k%7D 。

那么现在的关键就是如何计算第3步中的各个条件概率。我们可以这么做:

1、找到一个已知分类的待分类项集合,这个集合叫做训练样本集。

2、统计得到在各类别下各个特征属性的条件概率估计。即

gif.latex?P(%7Ba_1%7D%7B%5Cleft%7C%20y%20%5Cright._1%7D),P(%7Ba_2%7D%7B%5Cleft%7C%20y%20%5Cright._1%7D),%20%5Cldots%20,P(%7Ba_m%7D%5Cleft%7C%20%7B%7By_1%7D%7D%20%5Cright.);P(%7Ba_1%7D%7B%5Cleft%7C%20y%20%5Cright._2%7D),P(%7Ba_2%7D%7B%5Cleft%7C%20y%20%5Cright._2%7D),%20%5Cldots%20,P(%7Ba_m%7D%5Cleft%7C%20%7B%7By_2%7D%7D%20%5Cright.);%20%5Cldots%20;P(%7Ba_1%7D%7B%5Cleft%7C%20y%20%5Cright._n%7D),P(%7Ba_2%7D%7B%5Cleft%7C%20y%20%5Cright._n%7D),%20%5Cldots%20,P(%7Ba_m%7D%7B%5Cleft%7C%20y%20%5Cright._n%7D)

3、如果各个特征属性是条件独立的,则根据贝叶斯定理有如下推导:

gif.latex?P%5Cleft(%20%7B%7By_i%7D%5Cleft%7C%20x%20%5Cright.%7D%20%5Cright)%20=%20%5Cfrac%7B%7BP(x%5Cleft%7C%20%7B%7By_i%7D)P(%7By_i%7D)%7D%20%5Cright.%7D%7D%7B%7BP(x)%7D%7D

因为分母对于所有类别为常数,因为我们只要将分子最大化皆可。又因为各特征属性是条件独立的,所以有:

gif.latex?P%5Cleft(%20%7Bx%5Cleft%7C%20%7B%7By_i%7D%7D%20%5Cright.%7D%20%5Cright)P%5Cleft(%20%7B%7By_i%7D%7D%20%5Cright)%20=%20P%5Cleft(%20%7B%7Ba_1%7D%5Cleft%7C%20%7B%7By_i%7D%7D%20%5Cright.%7D%20%5Cright)P(%7Ba_2%7D%5Cleft%7C%20%7B%7By_i%7D%7D%20%5Cright.)%20%5Cldots%20P(%7Ba_m%7D%5Cleft%7C%20%7B%7By_i%7D%7D%20%5Cright.)%20=%20P%5Cleft(%20%7B%7By_i%7D%7D%20%5Cright)%5Cprod%20P%5Cleft(%20%7B%7Ba_j%7D%5Cleft%7C%20%7B%7By_i%7D%7D%20%5Cright.%7D%20%5Cright)

spark.ml现在支持多项朴素贝叶斯和伯努利朴素贝叶斯。

参数:

featuresCol:

类型:字符串型。

含义:特征列名。

labelCol:

类型:字符串型。

含义:标签列名。

modelType:

类型:字符串型。

含义:模型类型(区分大小写)。

predictionCol:

类型:字符串型。

含义:预测结果列名。

probabilityCol:

类型:字符串型。

含义:用以预测类别条件概率的列名。

rawPredictionCol:

类型:字符串型。

含义:原始预测。

smoothing:

类型:双精度型。

含义:平滑参数。

thresholds:

类型:双精度数组型。

含义:多分类预测的阀值,以调整预测结果在各个类别的概率。

示例:

Scala:

import org.apache.spark.ml.classification.NaiveBayes

import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator//Load the data stored in LIBSVM format as a DataFrame.

val data = spark.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt")//Split the data into training and test sets (30% held out for testing)

val Array(trainingData, testData) = data.randomSplit(Array(0.7, 0.3), seed = 1234L)//Train a NaiveBayes model.

val model = newNaiveBayes()

.fit(trainingData)//Select example rows to display.

val predictions =model.transform(testData)

predictions.show()//Select (prediction, true label) and compute test error

val evaluator = newMulticlassClassificationEvaluator()

.setLabelCol("label")

.setPredictionCol("prediction")

.setMetricName("accuracy")

val accuracy=evaluator.evaluate(predictions)

println("Accuracy:" + accuracy)

Java:

import org.apache.spark.ml.classification.NaiveBayes;

import org.apache.spark.ml.classification.NaiveBayesModel;

import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator;

import org.apache.spark.sql.Dataset;

import org.apache.spark.sql.Row;

import org.apache.spark.sql.SparkSession;//Load training data

Dataset dataFrame =spark.read().format("libsvm").load("data/mllib/sample_libsvm_data.txt");//Split the data into train and test

Dataset[] splits = dataFrame.randomSplit(new double[]{0.6, 0.4}, 1234L);

Dataset train = splits[0];

Dataset test = splits[1];//create the trainer and set its parameters

NaiveBayes nb = newNaiveBayes();//train the model

NaiveBayesModel model =nb.fit(train);//compute accuracy on the test set

Dataset result =model.transform(test);

Dataset predictionAndLabels = result.select("prediction", "label");

MulticlassClassificationEvaluator evaluator= newMulticlassClassificationEvaluator()

.setMetricName("accuracy");

System.out.println("Accuracy =" + evaluator.evaluate(predictionAndLabels));

Python:

frompyspark.ml.classification import NaiveBayesfrompyspark.ml.evaluation import MulticlassClassificationEvaluator

# Load training data

data= spark.read.format("libsvm") \

.load("data/mllib/sample_libsvm_data.txt")

# Split the data into train and test

splits= data.randomSplit([0.6, 0.4], 1234)

train= splits[0]

test= splits[1]

# create the trainer andsetits parameters

nb= NaiveBayes(smoothing=1.0, modelType="multinomial")

# train the model

model=nb.fit(train)

# compute accuracy on the testsetresult=model.transform(test)

predictionAndLabels= result.select("prediction", "label")

evaluator= MulticlassClassificationEvaluator(metricName="accuracy")

print("Accuracy:" + str(evaluator.evaluate(predictionAndLabels)))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值