# 高斯模型apiimport numpy
asnp
from
sklearn.*****_bayes import gaussiannb
from
sklearn.datasets import load_digits
from
sklearn.model_selection import train_test_split
data =load_digits()
feature =data.data
target =data.target
x_train, x_test, y_train, y_test =train_test_split(feature, target,
test_size=0.2, random_state=2020
)nb =gaussiannb()
nb.fit(x_train, y_train)
score =nb.score(x_test, y_test)
print(score)
# 測試集**值print(nb.predict(x_test[5].reshape(1,-1)))
# 測試集真實值
print(y_test[5])
# **每乙個的概率
print(nb.predict_log_proba(x_test[5].reshape(1,-1)))
# 多項式樸素貝葉斯api(新聞分類案例)import sklearn.datasets
asdatasets
from
sklearn.feature_extraction.text import tfidfvectorizer
from
sklearn.model_selection import train_test_split
from
sklearn.*****_bayes import multinomialnb
mnv = multinomialnb(alpha=1.0, fit_prior=true, class_prior=none)
news = datasets.fetch_20newsgroups(subset="
all"
)# print(news)
feature =news.data # 返回的是列表,列表中是文章
target =news.target # 返回不同的類別
x_train,x_test, y_train,y_test = train_test_split(feature, target, test_size=0.2
)# 對數進行特徵抽取
tf =tfidfvectorizer()
x_train =tf.fit_transform(x_train)
x_test =tf.transform(x_test)
mlt = multinomialnb(alpha=1
)mlt.fit(x_test,y_test)
y_predict =mlt.predict(x_test)
print(
"**結果:
",y_predict)
print(
"真實結果:
",y_test)
print(
"準確率:
",mlt.score(x_test,y_test))
# 伯努利模型(主要使用在二分類中,多分類建議使用多項式樸素貝葉斯)import sklearn.datasets
asdatasets
from
sklearn.feature_extraction.text import tfidfvectorizer
from
sklearn.model_selection import train_test_split
from
sklearn.*****_bayes import bernoullinb
news = datasets.fetch_20newsgroups(subset="
all"
)feature =news.data # 返回的是列表,列表中是文章
target =news.target # 返回不同的類別
x_train,x_test, y_train,y_test = train_test_split(feature, target, test_size=0.25
)# 對數進行特徵抽取
tf =tfidfvectorizer()
x_train =tf.fit_transform(x_train)
x_test =tf.transform(x_test)
mlt = bernoullinb(alpha=1
)mlt.fit(x_test,y_test)
y_predict =mlt.predict(x_test)
print(
"**結果:
",y_predict)
print(
"真實結果:
",y_test)
print(
"準確率:
",mlt.score(x_test,y_test))
樸素貝葉斯
樸素貝葉斯演算法是一種基於概率統計的分類方法,它主要利用貝葉斯公式對樣本事件求概率,通過概率進行分類。以下先對貝葉斯公式做個了解。對於事件a b,若p b 0,則事件a在事件b發生的條件下發生的概率為 p a b p a b p b 將條件概率稍作轉化即可得到貝葉斯公式如下 p a b p b a ...
樸素貝葉斯
1.準備資料 從文字中構建詞向量 2.訓練演算法 從詞向量計算概率 3.測試演算法 儲存為 bayes.py 檔案 參考 coding utf 8 from numpy import 文字轉化為詞向量 def loaddataset postinglist my dog has flea probl...
樸素貝葉斯
機器學習是將資料轉化為決策面的過程 scikit learn縮寫為sklearn 訓練乙個分類器,學習之後 其處理的準確性 def nbaccuracy features train,labels train,features test,labels test from sklearn.bayes ...