1、从sklearn自带的数据集中导入数据
from sklearn.datasets import load_irisfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn import cross_validationimport numpy as npdata = load_iris()X = data.datay = data.targetsize = np.random.permutation(y.size)X = X[size]y = y[size]
2、通过交叉验证来判断n_neighbors的值,也就是k值,为多少时分类效果最好。
n_range = range(1,31)n_scores = []for n in n_range: knn = KNeighborsClassifier(n_neighbors=n) score = cross_validation.cross_val_score(knn, X, y, cv=10) n_scores.append(score.mean())import pandasresult = pandas.DataFrame({ 'n_range':n_range, 'n_scores':n_scores})zuijia = int(result[result['n_scores']==max(n_scores)]['n_range'])#zuijia就是效果最好的k值import matplotlib.pyplot as pltplt.plot(n_range, n_scores,'b:+')plt.show()
3、将iris数据分为训练集和测试集,带入最佳的k值,运用knn预测。
X_train = X[:100]y_train = y[:100]X_test = X[100:]y_test = y[100:]KNN = KNeighborsClassifier(n_neighbors=zuijia)KNN.fit(X_train,y_train)plt.plot(KNN.predict(X_test),y_test, 'b:+')plt.show()