I used the following code to try finding the optimum of k, but it is giving the max accuracy as 58% in the function. Where I am making mistake. Moreover my submission score is 61% max. How to increase it to 100

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( x, y, test_size=0.2, random_state=4)

from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics

ans=0
for i in range(2):
neigh = KNeighborsClassifier(n_neighbors = k).fit(X_train,y_train)
Pred_y = neigh.predict(X_test)
if ans<metrics.accuracy_score(y_test, Pred_y):
ans=metrics.accuracy_score(y_test, Pred_y)

print(ans)

hey @varshneynishu2017 ,
can you please share you full code file ,either upload it on github or on drive and share the link here with me.

Thank You :slightly_smiling_face:.

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.