1
$\begingroup$

Does this line in Python indicate that KNN is weighted?

clf = KNeighborsClassifier(n_neighbors=5, metric='euclidean', weights='distance')

Are the weights the inverse of the distance?

Can anyone also give an example of how weighted KNN works mathematically?

$\endgroup$

1 Answer 1

2
$\begingroup$

Yes, the line indicates that KNN is weighted and that the weight is the inverse of the distance. All of this can easily be found in scikit-learn's documentation

Also, pro-tip, you can find an object's documentation using the help function. In this case:

from sklearn.neighbors import KNeighborsClassifier

print(help(KNeighborsClassifier))

As to how weighted KNN works, I actually made a learning unit on my website about this.

In short, weighting the instances means that instead of each neighbor having an equal vote, their vote is weighted. This means that if for instance, in my 3 nearest-neighbors, 2 of them are of class A but have low weights, and 1 of them is of class B and has a high weight, KNN might still pick class B even though more neighbors are of class A.

$\endgroup$
2
  • $\begingroup$ Thank you. I know the logic of wknn but I meant mathematically. I need to see this code line as equations. I would be grateful if you can help $\endgroup$
    – Mona
    Dec 11, 2019 at 0:08
  • $\begingroup$ the wikipedia page (en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) already provides some more mathematical foundation $\endgroup$ Dec 11, 2019 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.