1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import numpy as np import pandas as pd from sklearn.datasets import make_moons import matplotlib.pyplot as plt from sklearn.naive_bayes import BernoulliNB from sklearn.naive_bayes import GaussianNB from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import cross_val_score as cr n_samples = 600 x,y=make_moons(n_samples=n_samples, noise=0.1,random_state=8) x=x+2;
clf= GaussianNB() clf.fit(x,y)
xmin,xmax=x[:,0].min()-1,x[:,0].max()+1 ymin,ymax=x[:,1].min()-1,x[:,1].max()+1
xx,yy=np.meshgrid(np.arange(xmin,xmax,0.02),np.arange(ymin,ymax,0.02))
xf=np.c_[xx.ravel(),yy.ravel()]; z=clf.predict(xf) z=z.reshape(xx.shape) plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1) plt.scatter(x[:,0],x[:,1],c=y)
print(cr(clf,x,y,cv=5,scoring="accuracy"))
plt.show()
|