image_output_1.png
from sklearn.model_selection import GridSearchCV

from sklearn.neighbors import KNeighborsClassifier

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_
print('best model:', clf_best.get_params())
clf_best.fit(X_train, y_train)