Grid search related to machine learning knn algorithm

machine_learning

#1

How do we apply grid search over KNN algorithms?


#2

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)
clf.fit(X,Y)

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

clf.best_params_

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


#3
#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],
              'leaf_size':[1,3,5],
              'algorithm':['auto', 'kd_tree'],
              'n_jobs':[-1]}

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

#predictions on test data
prediction=model.predict(test_X)