首页 > 其他分享 >朴素贝叶斯

朴素贝叶斯

时间:2023-03-14 12:36:58浏览次数:43  
标签:__ Xd 贝叶斯 train predicted test import 朴素


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)


标签:__,Xd,贝叶斯,train,predicted,test,import,朴素
From: https://blog.51cto.com/u_16006123/6120305

相关文章