1. 程式人生 > >python中sklearn的樸素貝葉斯方法(sklearn.naive_bayes.GaussianNB)的簡單使用

python中sklearn的樸素貝葉斯方法(sklearn.naive_bayes.GaussianNB)的簡單使用

#測試資料
import numpy as np
features_train = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
labels_train = np.array([1, 1, 1, 2, 2, 2])
#引入高斯樸素貝葉斯
from sklearn.naive_bayes import GaussianNB
#例項化
clf = GaussianNB()
#訓練資料 fit相當於train
clf.fit(features_train, labels_train) 
#輸出單個預測結果
features_test = np.array([-0.8,-1])
labels_test = np.array([1])
pred = clf.predict(features_test)
print(pred)
#準確度評估 評估正確/總數
#方法1
accuracy = clf.score(features_test, labels_test)
#方法2
from sklearn.metrics import accuracy_score
accuracy2 = accuracy_score(pred,labels_test)

更多參考:http://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html