CodeLibrary/03_pycharm_python_coursework_20240427/NaiveBayesAlgorithm.py
JRNitre 2092240e04 计算机专业前沿作业 Python
Date: 20240427
Design by JRNitre
2024-04-27 20:40:24 +08:00

22 lines
663 B
Python

import numpy as np
from sklearn.naive_bayes import BernoulliNB
x = np.array([[0, 1, 0, 1], [1, 1, 1, 1], [1, 1, 1, 0],
[0, 1, 1, 0], [0, 1, 0, 0], [0, 1, 0, 1],
[1, 1, 0, 1], [1, 0, 0, 1], [1, 1, 0, 1],
[0, 0, 0, 0]])
# 有风-潮湿-多云-闷热
y = np.array([1, 1, 1, 1, 0, 1, 0, 1, 1, 0])
bnb = BernoulliNB()
bnb.fit(x, y)
day_pre = [[1, 0, 1, 0]]
pre = bnb.predict(day_pre)
print("预测结果如下:\n", "*" * 50)
print("结果为: ", pre)
print("*" * 50)
# 进一步查看概率分析
pre_pro = bnb.predict_proba(day_pre)
print("不下雨的概率为: ", pre_pro[0][0], "\n下雨的概率为: ", pre_pro[0][1])