Bernoulli Naive Bayes algorithm examples

 

from sklearn.naive_bayes import BernoulliNB   #贝努利贝叶斯  适用于符合二项分布的数据集
import numpy as np

X = np.array([[0, 1, 0, 1], 
              [1, 1, 1, 0], 
              [0, 1, 1, 0], 
              [0, 0, 0, 1], 
              [0, 1, 1, 0], 
              [0, 1, 0, 1], 
              [1, 0, 0, 1]])                        #7天天气的四个影响因素(刮风、闷热、多云、预报) 
y = np.array( [0, 1, 1, 0, 1, 0, 0])                #7天的结果

#对不同分类计算每个特征为1的数量
counts = {}
for label in np.unique(y):
    counts[label] = X[y == label].sum(axis=0)
print("feature counts:\n{}".format(counts))

clf = BernoulliNB()
clf.fit(X, y)
Next_Day = [[0, 0, 1, 0]]                              #假设明天的四个因素 
pre = clf.predict(Next_Day)                            #预测明天天气
a=clf.predict_proba(Next_Day)                          #预测的概率
if pre == [1]:
    print("明天下雨!概率为{}".format(a[0][1]))
else:
print("明天晴天哦")
本文参考《深入浅出python机器学习》一书内容,仅供学习参考,若有侵权,请联系删除。

Author: ChenBD

Published 142 original articles · won praise 213 · views 10000 +

Guess you like

Origin blog.csdn.net/s0302017/article/details/104336823