Which accuracy to use in KNN to select number of neighbors



0

When I run a loop over KNN with several values for neighbor and calculate the accuracy of model, I am getting different value of # of neighbors. Code below - 

score = []
for i in range(1,20):
    knn = KNeighborsClassifier(i)
    knn.fit(titanicTrain_X, titanicTrain_Y)
    score.append(knn.score(titanicTest_X, titanicTest_Y))   

Here max score is for i=3

However, when I use GridSearchCV for selecting best parameter, I get best value for 6 as below - 

knn2 = KNeighborsClassifier()
params = [{"n_neighbors":range(1,20)}]
gridsearch = GridSearchCV(estimator = knn2, cv=10, param_grid=params)

How do I decide the value for neighbors given this situation? Is GridSearchCV using different scoring criteria than knn.score?


1 Answer(s)


0

Using elbow measure as explained in video it will help. To identify which k value is good to build the cluster. As it's part of ilunsupervised learning.