question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:28 (26 by maintainers)

github_iconTop GitHub Comments

1reaction
RudreshVeerkharecommented, Oct 24, 2022

Thanks @jjerphan for the clarification, I’ll start working on it, and will create a WIP PR. Will discuss further on it…

1reaction
RudreshVeerkharecommented, Oct 22, 2022

Hi @RudreshVeerkhare. Yes, this needs to be implemented and you can work on it !

Thanks @jjerphan 😁, I’ve start with setup. Will communicate regarding further doubts are suggestions.😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

8. k-Nearest Neighbor Classifier in Python | Machine Learning
Introduction into k-nearest neighbor classifiers with Python.
Read more >
K-Nearest Neighbor (KNN) Algorithm in Python - Datagy
In regression problems, the KNN algorithm will predict a new data point's continuous value by returning the average of the k neighbours' values....
Read more >
How to set p < 1 in minkowski metric in KNN? - Stack Overflow
For p ≥ 1, the Minkowski distance is a metric as a result of the Minkowski inequality. When p < 1, the distance...
Read more >
sklearn.neighbors.KNeighborsClassifier
Metric to use for distance computation. Default is “minkowski”, which results in the standard Euclidean distance when p = 2. See the documentation...
Read more >
K-Nearest Neighbor from Scratch in Python - Kenzo's Blog
Let's calculate k-NN by hand using the above example. ... The smallest value means the nearest, so the nearest neighbor is [1,1] with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found