fromsklearn.cross_validationimportcross_val_score# 10-fold cross-validation with K=5 for KNN (the n_neighbors parameter)knn=KNeighborsClassifier(n_neighbors=5)scores=cross_val_score(knn,X,y,cv=10,scoring='accuracy')printscores

[ 0.6 0.75 0.75 0.25 0.75 1.
0.33333333 1. 0.66666667 0.66666667]

# use average accuracy as an estimate of out-of-sample accuracyprintscores.mean()

0.676666666667

# search for an optimal value of K for KNNk_range=range(1,31)k_scores=[]forkink_range:knn=KNeighborsClassifier(n_neighbors=k)scores=cross_val_score(knn,X,y,cv=10,scoring='accuracy')k_scores.append(scores.mean())# print k_scores# plot the value of K for KNN (x-axis) versus the cross-validated accuracy (y-axis)plt.plot(k_range,k_scores)plt.xlabel('Value of K for KNN')plt.ylabel('Cross-Validated Accuracy')

<matplotlib.text.Text at 0xbeb65c0>

# 10-fold cross-validation with the best KNN modelknn=KNeighborsClassifier(n_neighbors=13)printcross_val_score(knn,X,y,cv=10,scoring='accuracy').mean()