How to find K in KNN

How do we find the value of K for best possible answer

As of now we have to do hit and trial for finding out value of k. But there is a concept of grid search. The grid search object takes the model as your input and a list of parameters whose value you want to find out with the range in between which you want to find out your parama value. So for k let’s say we give params to grid search as n_nearest=[0-30] Then for every value between 0-30 it will train the knn model and will return the value of best parameter which results in the best training accuracy. But the downside of using grid search is that it works only for training data

You can read the documentation of grid search from sklearn

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.