Value of k in knn

increasing or decreasing k in knn both are decreasing the accuracy. why is this so ?

def knn(x, y, query_x, k=6):
val = []
m = x.shape[0]

for i in range(m):
    d = dist(query_x, x[i])
    val.append((d, y[i]))
val = sorted(val)
val = val[:k]
val = np.array(val)

newvals = np.unique(val[:,1], return_counts = True)

print(*newvals)

index = newvals[1].argmax()
pred = newvals[0][index]
return pred

hey @ayangiri3 ,
Your code works absolutely fine.
The actual thing is that we need to find a particular value of k for which our model will perform best by trying different values k, and in this case that value is currently k = 6 , therefore whenever you increase or decrease its value the accuracy is constantly decreasing and not increasing.

You can try different techniques like scaling your data , combining results of different values of K to get better results . These techniques might improve your results , but the major important thing is experiments that you need to perform to get better accuracy.

I hope this would have resolved your doubt.
Thank You.
Happy learning :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.