import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
if __name__ == '__main__':
dataset = load_iris()
X = dataset.data
y = dataset.target
# 将数组或矩阵拆分为 随机训练 和 测试子集
Xd_train, Xd_test, y_train, y_test = train_test_split(X, y, random_state=14)
# 调用贝叶斯算法
clf = GaussianNB()
# fit 根据 X, y 拟合高斯朴素贝叶斯
clf = clf.fit(Xd_train, y_train)
# 预测(测试数据)
y_predicted = clf.predict(Xd_test)
# 预测准确率
accuracy = np.mean(y_predicted == y_test) * 100
print("y_test ", y_test)
print("y_predicted", y_predicted)
print("accuracy:", accuracy)