朴素贝叶斯
目录
2017 年 8 月 10 日 19:17 朴素贝叶斯属于贝叶斯算法中的一个分支。sklearn.naive_bayes.GaussianNB 用于分类场景。属于监督学习。 应用分为三个步骤: 实例化算法模块 样本训练 fit 预测 predict (计算准确度,需要提供test数据的实际标签)score 样例:
import numpy as np
X
np . array([[
1 ,
1 ], [
2 ,
1 ], [
3 ,
2 ], [ 1 , 1 ], [ 2 , 1 ], [ 3 , 2 ]])
Y
np . array([ 1 , 1 , 1 , 2 , 2 , 2 ])
from sklearn.naive_bayes import GaussianNB
clf
GaussianNB()
clf . fit(X, Y) GaussianNB()
print clf . predict([[
0.8 ,
1 ]]) [1] 样例二,包含测试准确度: def NBAccuracy(features_train, labels_train, features_test, labels_test): """ compute the accuracy of your Naive Bayes classifier """
import the sklearn module for GaussianNB
from sklearn.naive_bayes import GaussianNB
create classifier
clf = GaussianNB()
fit the classifier on the training features and labels
clf.fit(features_train, labels_train)
use the trained classifier to predict labels for the test features
pred = clf.predict(features_train)
### calculate and return the accuracy on the test data
### this is slightly different than the example,
### where we just print the accuracy
### you might need to import an sklearn module
accuracy = clf.score(features_test, labels_test) return accuracy 计算预测准确度还有一个通用方法:用预测值与实际标签对比: