KNN Algorithm python

Please explain why vals=vals[:k] ? why is it is used and please explain!!

While using knn we need to consider only k nearest neighbors and thats the reason we have used the slicing for list, [:k] after sorting them, means this will return only the first k elements. After this we will take majority voting from these k examples.

Hope this cleared your doubt. :blush:

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.