KNN-classifier:
1 from sklearn.datasets import make_blobs 2 #用于生成聚类算法的测试数据 3 from sklearn.neighbors import KNeighborsClassifier 4 from sklearn.model_selection import train_test_split 5 import matplotlib.pyplot as plt 6 data=make_blobs(n_samples=200,centers=2,random_state=8) 7 X,y=data 8 plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.spring,edgecolors='k') 9 #print(X) 10 #print(y) 11 plt.show()
1 import numpy as np 2 clf=KNeighborsClassifier() 3 clf.fit(X,y) 4 print(clf) 5 x_min=X[:,0].min()-1 6 x_max=X[:,0].max()+1 7 y_min=X[:,1].min()-1 8 y_max=X[:,1].max()+1 9 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02), 10 np.arange(y_min,y_max,.02)) 11 Z=clf.predict(np.c_[xx.ravel(),yy.ravel()]) 12 Z=Z.reshape(xx.shape) 13 plt.pcolormesh(xx,yy,Z,cmap=plt.cm.Pastel1) 14 plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.spring,edgecolors='k') 15 plt.xlim(xx.min(),xx.max()) 16 plt.ylim(yy.min(),yy.max()) 17 plt.title("Classifier:KNN") 18 plt.suptitle("myfirstML") 19 20 plt.scatter(6.75,4.82,marker='*',c='red',s=200) 21 plt.show()
1 data2=make_blobs(n_samples=500,centers=5,random_state=8) 2 X2,y2=data2 3 plt.scatter(X2[:,0],X2[:,1],c=y2,cmap=plt.cm.spring,edgecolor='k') 4 plt.show()
1 clf=KNeighborsClassifier() 2 clf.fit(X2,y2) 3 x_min=X2[:,0].min()-1 4 x_max=X2[:,0].max()+1 5 y_min=X2[:,1].min()-1 6 y_max=X2[:,1].max()+1 7 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02), 8 np.arange(y_min,y_max,.02)) 9 Z=clf.predict(np.c_[xx.ravel(),yy.ravel()]) 10 Z=Z.reshape(xx.shape) 11 plt.pcolormesh(xx,yy,Z,cmap=plt.cm.Pastel1) 12 plt.scatter(X2[:,0],X2[:,1],c=y2,cmap=plt.cm.spring,edgecolors='k') 13 plt.xlim(xx.min(),xx.max()) 14 plt.ylim(yy.min(),yy.max()) 15 plt.title("Classifier:KNN") 16 plt.suptitle("mysecondML") 17 plt.show()
KNN-regressor:
1 from sklearn.model_selection import train_test_split 2 import matplotlib.pyplot as plt 3 from sklearn.datasets import make_regression 4 X,y=make_regression(n_features=1,n_informative=1,noise=50,random_state=8) 5 plt.scatter(X,y,c='orange',edgecolors='k') 6 plt.show()
1 from sklearn.neighbors import KNeighborsRegressor 2 import numpy as np 3 reg=KNeighborsRegressor() 4 reg.fit(X,y) 5 z=np.linspace(-3,3,200).reshape(-1,1) 6 plt.scatter(X,y,c='orange',edgecolors='k') 7 plt.plot(z,reg.predict(z),c='k',linewidth=2) 8 plt.title("KNN Regressior") 9 plt.suptitle("mythirdML") 10 print("the score of this model:{:.2f}".format(reg.score(X,y))) 11 plt.show()
1 reg2=KNeighborsRegressor(n_neighbors=2) 2 reg2.fit(X,y) 3 plt.scatter(X,y,c='orange',edgecolors='k') 4 plt.plot(z,reg2.predict(z),c='k',linewidth=2) 5 plt.title("KNN Regressior2") 6 plt.suptitle("myfourthML") 7 print("the score of this model:{:.2f}".format(reg2.score(X,y))) 8 plt.show()
1 from sklearn.datasets import load_wine 2 wine_dataset=load_wine() 3 print("{}".format(wine_dataset.keys())) 4 print("{}".format(wine_dataset['data'].shape)) 5 print("{}".format(wine_dataset['DESCR'])) 6 from sklearn.model_selection import train_test_split 7 X_train,X_test,y_train,y_test=train_test_split( 8 wine_dataset['data'],wine_dataset['target'],random_state=0 9 ) 10 print("{}".format(X_train.shape)) 11 print("{}".format(X_test.shape)) 12 print("{}".format(y_train.shape)) 13 print("{}".format(y_test.shape))
1 from sklearn.neighbors import KNeighborsClassifier 2 knn=KNeighborsClassifier(n_neighbors=1) 3 knn.fit(X_train,y_train) 4 print(knn) 5 print("the score of this model:{:.2f}".format(knn.score(X_test,y_test)))
1 import numpy as np 2 X_new=np.array([[13.2,2.77,2.51,18.5,96.6,1.04,2.55,0.57,1.47,6.2,1.05,3.33,820]]) 3 prediction=knn.predict(X_new) 4 print("the prediction is {}".format(wine_dataset['target_names'][prediction]))