基本用法
完整代码:
from sklearn.naive_bayes import BernoulliNB
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split# 生成数据
X, y = make_blobs(n_samples=500, centers=5, random_state=8)
X_train, X_test, y_train, y_test = train_test_split(X, y)# 训练数据
clf = BernoulliNB()
clf.fit(X_train, y_train)# 查看得分
print(clf.score(X_train, y_train))
print(clf.score(X_test, y_test))
输出:
0.5066666666666667
0.52
贝努利贝叶斯可视化
完整代码:
from sklearn.naive_bayes import BernoulliNB
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt# 生成数据
X, y = make_blobs(n_samples=500, centers=5, random_state=8)
X_train, X_test, y_train, y_test = train_test_split(X, y)# 训练数据
clf = BernoulliNB()
clf.fit(X_train, y_train)# 画图
x_min, x_max = X[:,0].min() - 0.5, X[:,0].max() + 0.5
y_min, y_max = X[:,1].min() - 0.5, X[:,1].max() + 0.5
xx, yy = np.meshgrid(np.arange(x_min,x_max,.02), np.arange(y_min,y_max,.02))z = clf.predict(np.c_[(xx.ravel(),yy.ravel())]).reshape(xx.shape)plt.scatter(X_train[:,0], X_train[:,1], c=y_train, cmap=plt.cm.cool, edgecolor="k")
plt.scatter(X_test[:,0], X_test[:,1], c=y_test, cmap=plt.cm.cool, marker="*", edgecolor="k")
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
plt.title("Classifier: BernoulliNB")
plt.show()
输出: