1 from sklearn.datasets import load_diabetes 2 X,y=load_diabetes().data,load_diabetes().target 3 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8) 4 lr=LinearRegression().fit(X_train,y_train) 5 print("the coefficient:{}".format(lr.coef_)) 6 print('the intercept:{}'.format(lr.intercept_)) 7 print("the score of this model:{:.3f}".format(lr.score(X_test,y_test)))
1 import matplotlib.pyplot as plt 2 plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.spring,edgecolors='k') 3 plt.show()
1 #伯努利贝叶斯分类器 2 from sklearn.naive_bayes import BernoulliNB 3 bnb=BernoulliNB() 4 bnb.fit(X_train,y_train) 5 print("the score of this model:{}".format(bnb.score(X_test,y_test)))
1 import numpy as np 2 x_min=X[:,0].min()-0.5 3 x_max=X[:,0].max()+0.5 4 y_min=X[:,1].min()-0.5 5 y_max=X[:,1].max()+0.5 6 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02), 7 np.arange(y_min,y_max,.02)) 8 z=bnb.predict(np.c_[(xx.ravel(),yy.ravel())]) 9 z=z.reshape(xx.shape) 10 plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1) 11 plt.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap=plt.cm.spring,edgecolors='k') 12 plt.scatter(X_test[:,0],X_test[:,1],c=y_test,cmap=plt.cm.spring,edgecolors='k',marker='*') 13 plt.xlim(xx.min(),xx.max()) 14 plt.ylim(yy.min(),yy.max()) 15 plt.title("Classifier:BernoulliNB") 16 plt.show()
1 #高斯贝叶斯分类器 2 from sklearn.naive_bayes import GaussianNB 3 gnb=GaussianNB() 4 gnb.fit(X_train,y_train) 5 print("the score of this model:{}".format(gnb.score(X_test,y_test)))
1 import numpy as np 2 x_min=X[:,0].min()-0.5 3 x_max=X[:,0].max()+0.5 4 y_min=X[:,1].min()-0.5 5 y_max=X[:,1].max()+0.5 6 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02), 7 np.arange(y_min,y_max,.02)) 8 z=gnb.predict(np.c_[(xx.ravel(),yy.ravel())]) 9 z=z.reshape(xx.shape) 10 plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1) 11 plt.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap=plt.cm.spring,edgecolors='k') 12 plt.scatter(X_test[:,0],X_test[:,1],c=y_test,cmap=plt.cm.spring,edgecolors='k',marker='*') 13 plt.xlim(xx.min(),xx.max()) 14 plt.ylim(yy.min(),yy.max()) 15 plt.title("Classifier:GaussianNB") 16 plt.show()
1 #最大最小预处理,处理非负数据 2 from sklearn.preprocessing import MinMaxScaler 3 scaler=MinMaxScaler() 4 scaler.fit(X_train) 5 X_train_scaled=scaler.transform(X_train) 6 X_test_scaled=scaler.transform(X_test) 7 #多项式朴素贝叶斯 8 from sklearn.naive_bayes import MultinomialNB 9 mnb=MultinomialNB() 10 mnb.fit(X_train_scaled,y_train) 11 print("the score of this model:{}".format(mnb.score(X_test_scaled,y_test)))
1 import numpy as np 2 x_min=X[:,0].min()-0.5 3 x_max=X[:,0].max()+0.5 4 y_min=X[:,1].min()-0.5 5 y_max=X[:,1].max()+0.5 6 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02), 7 np.arange(y_min,y_max,.02)) 8 z=mnb.predict(np.c_[(xx.ravel(),yy.ravel())]) 9 z=z.reshape(xx.shape) 10 plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1) 11 plt.scatter(X_train[:,0],X_train[:,1],c=y_train,cmap=plt.cm.spring,edgecolors='k') 12 plt.scatter(X_test[:,0],X_test[:,1],c=y_test,cmap=plt.cm.spring,edgecolors='k',marker='*') 13 plt.xlim(xx.min(),xx.max()) 14 plt.ylim(yy.min(),yy.max()) 15 plt.title("Classifier:MultinomialNB") 16 plt.show()
以上三个分类器均是二维可视化的。