Grid search related to machine learning knn algorithm



How do we apply grid search over KNN algorithms?


Hi @shashank_kumar,

Suppose X contains your data and Y contains the target values. Now first of all you will define your kNN model:

knn = KNeighborsClassifier()   

Now, you can decide which parameter you want to tune using GridSearchCV. Now you will define the GridSearchCV model and fit the dataset.

clf = GridSearchCV(knn, parameters, cv=5),Y)

Now, you can look for the best value of the parameters using .best_params_ function.


This will give you the best value for the given parameter, which you can use further to train your model.

#import knn and gridsearch cv from sklearn

from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV

#define the model and parameters
knn = KNeighborsClassifier()

parameters = {'n_neighbors':[4,5,6,7],
              'algorithm':['auto', 'kd_tree'],

#Fit the model
model = GridSearchCV(knn, param_grid=parameters),train_y)

#predictions on test data