在sklearn中,KNN(K-Nearest Neighbors)分类器是一种基于实例的学习算法,它主要用于分类任务。
#导入数据集生成器
from sklearn.datasets import make_blobs
# 生成样本数为200,分类为2的数据集
data = make_blobs(n_samples = 200,centers = 2,random_state=8)
X,y = data
#数据可视化
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(X[:,0],X[:,1],c = y, cmap=plt.cm.spring,edgecolors = 'k')
# 导入KNN分类器
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
# KNN分类器实例化
clf = KNeighborsClassifier()
# 模型训练
clf.fit(X, y)
#训练结果可视化
x_min, x_max = X[:,0].min() - 1,X[:,0].max() + 1
y_min, y_max = X[:,0].min() - 1,X[:,0].max() + 1
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()])
Z = Z.reshape(xx.shape)
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Pastel1)
plt.scatter(X[:,0], X[:,1], c=y,cmap=plt.cm.spring,edgecolor='k')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("Classifier:KNN")
#对新数据点进行可视化
plt.scatter(6.75,4.82,marker='*', c='red',s=200)
#生成样本数是500,类别数是5的数据集
data2 = make_blobs (n_samples=500, centers=5, random_state=8)
X2, y2 = data2
plt.scatter(X2[:,0], X2[:,1], c=y2, cmap=plt.cm.spring, edgecolor='k')
plt.show()
clf = KNeighborsClassifier()
clf.fit(X2, y2)
#对拟合结果进行可视化
x_min, x_max = X2[:,0].min()-1, X2[:,0].max()+ 1
y_min, y_max = X2[:,1].min()-1, X2[:,1].max()+ 1
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()])
Z = Z.reshape(xx.shape)
plt.pcolormesh(xx,yy,Z, cmap=plt.cm.Pastel1)
plt.scatter(X2[:,0],X2[:,1],c=y2,cmap=plt.cm.spring,edgecolor='k')
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
plt.title("Classifier:KNN")
#plt.scatter(6. 75, 4. 82, marker='*',c='red', s=200)
plt.show()
#模型评估
print('模型正确率:{:.2f}'.format(clf.score(X2,y2)))
模型正确率:0.96
KNN实战葡萄酒分类
先导入必要的库:
import numpy as np
import matplotlib. pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets
from sklearn.model_selection import train_test_split
#导入数据集
wine = datasets.load_wine()
#划分训练集与测试集
X_train,X_test,y_train,y_test = train_test_split(wine.data[:,:2],wine.target,random_state = 42)
#KNN模型实例化
clf = KNeighborsClassifier()
#训练
clf.fit(X_train,y_train)
#模型评估
print('测试集模型评分:{:.2f}'.format(clf.score(X_test,y_test)))
print('训练集模型得分:{:.2f}'.format(clf.score(X_train,y_train)))
测试集模型评分:0.89 训练集模型评分:1.00
#进行预测
X_new = np.array([[13.2,2.77]])
prediction = clf.predict(X_new)
print("预测新红酒的分类为:{}".format(wine['target_names'][prediction]))
预测新红酒的分类为:['class_2']