text_plain_output_1.png
from sklearn import metrics

from sklearn.model_selection import GridSearchCV

from sklearn.neighbors import KNeighborsClassifier

val_results = []
train_results = []

k_range = range(1, 21)

for k in k_range:
    clf_2 = KNeighborsClassifier(n_neighbors=k)
    clf_2 = clf_2.fit(X_train, y_train)
    pred_train = clf_2.predict(X_train)
    train_score = metrics.accuracy_score(y_train, pred_train)
    train_results.append(train_score)
    pred_val = clf_2.predict(X_val)
    val_score = metrics.accuracy_score(y_val, pred_val)
    val_results.append(val_score)

clf_3 = KNeighborsClassifier()
param_grid = [{'weights': ['uniform'], 'n_neighbors': list(range(1, 21))}, {'weights': ['distance'], 'n_neighbors': list(range(1, 21))}]

gs = GridSearchCV(clf_3, param_grid, scoring='accuracy', cv=10)
gs = gs.fit(X_train, y_train)

clf_best = gs.best_estimator_
clf_best.fit(X_train, y_train)

y_pred = clf_best.predict(X_test)
print(metrics.accuracy_score(y_test, y_pred))