KNN算法概述:https://www.cnblogs.com/listenfwind/p/10311496.html
KNN算法实践:https://www.cnblogs.com/listenfwind/p/10685192.html
随机生成样本数为500,有两个属性,五个分类的数据集。
#导入数据集生成器
from sklearn.datasets import make_blobs
#导入KNN分类器
from sklearn.neighbors import KNeighborsClassifier
#导入画图工具
import matplotlib.pyplot as plt
#导入数据集拆分工具
from sklearn.model_selection import train_test_split
import numpy as np
#生成样本数为500,有两个属性,五个分类的数据集
data=make_blobs(n_samples=500,centers=5,n_features=2)
#X保存数据属性,保存数据的分类
X,y=data
#创建一个分类器
clf=KNeighborsClassifier()
#训练数据
clf.fit(X,y)。#以下代码用于画图
x_min,x_max=X[:,0].min()-1,X[:,0].max()+1
y_min,y_max=X[:,1].min()-1,X[:,1].max()+1
xx,yy=np.meshgrid(np.arange(x_min,x_max,0.2),np.arange(y_min,y_max,0.2))z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
z=z.reshape(xx.shape)
plt.pcolormesh(xx, yy,z)plt.scatter(X[:,0],X[:,1],cmap=plt.cm.spring,c=y,edgecolor='k')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()
print(clf.score(X, y))
KNN分类效果如图所示,有相同颜色的区域中的点归为一类
KNN算法用于回归分析
#导入make_regression数据生成器
from sklearn.datasets import make_regression
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsRegressor
import numpy as np
X,y=make_regression(n_features=1,n_informative=1,noise=50)plt.scatter(X,y,c='red',edgecolor='yellow',linewidth=3)reg=KNeighborsRegressor(n_neighbors=5)reg.fit(X, y)
z=np.linspace(-3,3,200).reshape(-1,1)
plt.plot(z,reg.predict(z),c='red',linewidth=3)
plt.show()
print(reg.score(X, y))
KNN算法实战——酒的分类
from sklearn.datasets import load_wine
#从sklearn的datasets模块载入数据集
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
wine_dataset=load_wine()
X_train,X_test,y_train,y_test=train_test_split(wine_dataset['data'], wine_dataset['target'])
KNN=KNeighborsClassifier(n_neighbors=3)
KNN.fit(X_train,y_train)
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]])
tt=KNN.predict(X_new)
print(wine_dataset['target_names'][tt])
print(KNN.score(X_test, y_test))