KNN方法中關於K值的影響
阿新 • • 發佈:2019-02-18
from sklearn.datasets import load_iris
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import cross_val_score
import matplotlib.pyplot as plt
iris = load_iris()
X = iris.data
Y = iris.target
#X_train,X_test,Y_train,Y_test = train_test_split(X,Y,random_state=4)
#knn = KNeighborsClassifier(n_neighbors=5)
#scores = cross_val_score(knn,X,Y,cv=5,scoring='accuracy')
#print(scores.mean())
k_range = range(1,31)
k_scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
scores = cross_val_score(knn,X,Y,cv=10,scoring='accuracy')
#loss = -cross_val_score(knn,X,Y,cv=10,scoring='mean_squared_error')
k_scores.append(scores.mean())
plt.plot(k_range,k_scores)
plt.xlabel('Value of K for KNN')
plt.ylabel('Cross_validated accuracy')
plt.show()