KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)
See original GitHub issueDescribe the workflow you want to enable
I would like to be able to use the KNeighborsClassifier with something like:
neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)
The error you get is:
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
392 effective_p = self.p
394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395 raise ValueError("p must be greater or equal to one for minkowski metric")
ValueError: p must be greater or equal to one for minkowski metric
Change the above line to limit minkowski p value to < 0:
if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:
There is nothing wrong with using a p value > 0 and < 1.
Describe your proposed solution
Don’t throw an exception if p is < 1.0.
Calculating Minkowski distance is most definitely valid for p > 0 and p < 1. I.e.: 0.1, 0.2, 0.3, 0.4, etc
The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions: distance = distance_sum**p
There are many cases where it is desirable to compute minkowski > 0 and < 1
Describe alternatives you’ve considered, if relevant
Write my own kNN classifier with my own minkowski distance calculator:
def minkowski_distance(row1, row2, p=3):
distance = 0.0
for i in range(len(row1)-1):
distance += abs(row1[i] - row2[i])**p
return distance**(1/p)
Additional context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:28 (26 by maintainers)
Top GitHub Comments
Thanks @jjerphan for the clarification, I’ll start working on it, and will create a WIP PR. Will discuss further on it…
Thanks @jjerphan 😁, I’ve start with setup. Will communicate regarding further doubts are suggestions.😄