Solved – How does scikit-learn’s kNN model handle zero-distances when using inverse distance weighting

k nearest neighbourscikit learn

OK, I could go through the code to figure this out but I feel something Googleable doesn't hurt.

When I'm using a kNN classifier with (inverse) distance weighting, how does it handle cases whereby the distance between the prediction input and (m)any of the k nearest training records is zero? What if some of them aren't?

Best Answer

KNN classifier in scikit-learn uses _get_weights method in sklearn.neighbors.base library. The inverse weighting is achieved when 'distance' is given as weights paremeter. You can also call this function directly by giving your distances as input. The weight is $w=\frac{1}{d}$, but surprisingly, when $d$ is $0$, the weight is always set to $1$. In the code, it does an np.isinf check and when a weight is infinite, it is set to the boolean value produced by np.isinf.

Related Question