3 代码实现贝努力朴素贝叶斯
4 代码实现高斯朴素贝叶斯
5 代码实现多项式朴素贝叶斯

## 2 朴素贝叶斯应用

id刮北风闷热多云预报有雨真下雨？
101010
211101
301101
400010
501101
601010
710010

## 3 代码实现贝努力朴素贝叶斯

```from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.naive_bayes import BernoulliNB
def naviebayes():
# 读取数据
# 取出数据当中的特征值和目标值
y = data['真下雨？'] #目标值
x = data[['刮北风', '闷热','多云','天气预报有雨']] #特征值
# 进行数据分割
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
#贝努力朴素贝叶斯
clf = BernoulliNB()
clf.fit(x_train, y_train)
y_predict = clf.predict(x_test)
print(x_test)
print(y_predict)
print("预测的概率为",clf.score(x_test, y_test))
print("--------------------")
Next_Day = [[0, 0, 1, 0]]
pre = clf.predict(Next_Day)
pre2 = clf.predict_proba(Next_Day)
print("预测结果为：", pre)
# 输出模型预测的分类概率
print("预测的概率为：", pre2)
if __name__ == '__main__':
naviebayes()```

## 4 代码实现高斯朴素贝叶斯

```from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.naive_bayes import GaussianNB
def naviebayes():
# 读取数据
# 取出数据当中的特征值和目标值
y = data['真下雨？'] #目标值
x = data[['刮北风', '闷热','多云','天气预报有雨']] #特征值
# 进行数据分割
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
#高斯朴素贝叶斯
gnb = GaussianNB()
gnb.fit(x_train, y_train)
y_predict = gnb.predict(x_test)
print(x_test)
print(y_predict)
print("预测的概率为",gnb.score(x_test, y_test))
print("--------------------")
Next_Day = [[0, 0, 1, 0]]
pre = gnb.predict(Next_Day)
pre2 = gnb.predict_proba(Next_Day)
print("预测结果为：", pre)
# 输出模型预测的分类概率
print("预测的概率为：", pre2)
if __name__ == '__main__':
naviebayes()```

## 5 代码实现多项式朴素贝叶斯

```from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
import pandas as pd
def naviebayes():
# 读取数据
# 取出数据当中的特征值和目标值
y = data['真下雨？'] #目标值
x = data[['刮北风', '闷热','多云','天气预报有雨']] #特征值
# 进行数据分割
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
# 多项式朴素贝叶斯
mlt = MultinomialNB(alpha=1.0)
mlt.fit(x_train, y_train)
y_predict = mlt.predict(x_test)
print(x_test)
print(y_predict)
print("准确率为：", mlt.score(x_test, y_test))
print("--------------------")
Next_Day = [[0, 0, 1, 0]]
pre = mlt.predict(Next_Day)
pre2 = mlt.predict_proba(Next_Day)
print("预测结果为：", pre)
# 输出模型预测的分类概率
print("预测的概率为：", pre2)
if __name__ == '__main__':
naviebayes()```