I am trying to find optimized n_neighbors value for KnearestClassifier using GridSearchCV. I am able to get optimized parameters but when I enter those in my classifier results don't match with GridSearchCVs best results.
clf = KNeighborsClassifier(n_neighbors=15, weights='uniform')
clf.fit(features_train, labels_train)
print('Score using optimized parameters: {}'.format(clf.score(features_test, labels_test)))
params = {'n_neighbors':[1,10,15,20,25,30,35,40,45,50,60,70,80,90,100], 'weights':['uniform', 'distance']}
grid = GridSearchCV(clf, params, cv=10, )
grid.fit(features_train, labels_train)
print('Optimized Parameters:{}'.format(grid.best_params_))
print('Best Score from GridsearchCV parameters{}'.format(grid.best_score_))
Output:
Score using optimized parameters: 0.928
Optimized Parameters:{'n_neighbors': 15, 'weights': 'uniform'}
Best Score from GridsearchCV parameters: 0.962666666667