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.

NearestNeighbors doesn't work with algorithm="auto" when using mahalanobis distance

See original GitHub issue

When I use NearestNeighbors with mahalanobis distance, I find setting algorithm=‘auto’ (which defaults to ‘ball_tree’) doesn’t seem to work. This throw an error: ValueError: Mahalanobis dist: size of V does not match

import numpy as np from sklearn.datasets import make_classification from sklearn.neighbors import DistanceMetric X, y = make_classification() nn = NearestNeighbors(algorithm='auto', metric='mahalanobis', metric_params={'V': np.cov(X)}) nn.fit(X)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jnothmancommented, Aug 14, 2018

I suspect it wants covariance between the features, not the samples. I.e. use rowvar=False​

0reactions
thomasjpfancommented, Dec 1, 2022

Thank you @cmarmo for following up on this issue. From https://github.com/scikit-learn/scikit-learn/issues/11807#issuecomment-1302822458, I agree this issue can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use mahalanobis distance in sklearn DistanceMetrics?
how do you use the distance metric, in, say nearest neighbors or clustering? When I try to use it, I get ValueError: Metric...
Read more >
Nearest Neighbor- Including Mahalanobis Distance
We were discussing various distance algorithms for a nearest neighbor algorithm. Our prof discussed one called the Mahalanobis distance.
Read more >
K-Nearest Neighbors: Part 1 Introduction | by Walid Hadri
The Mahalanobis distance measures distance relative to the centroid — a base or central point which can be thought of as an overall...
Read more >
Python – How to use mahalanobis distance in sklearn ... - iTecNote
Is there an example of using the Mahalanobis distance that I can see? ... setting algorithm='auto' (which defaults to 'ball_tree' ) doesn't seem...
Read more >
Guide to the K-Nearest Neighbors Algorithm in Python and ...
Note: The distance can be measured in different ways. You can use a Minkowski, Euclidean, Manhattan, Mahalanobis or Hamming formula, ...
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