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.

KNN Mahalanobis distance error

See original GitHub issue

Hi,

When I use the Mahalanobis metric for KNN I always get the error “Must provide either V or VI for Mahalanobis distance” even when I provide V with metric_params. The same request works with sklearn.neighbors.


from pyod.models.knn import KNN  
from pyod.utils.data import generate_data
from sklearn.neighbors import NearestNeighbors
import numpy as np

contamination = 0.1  
n_train = 200  
n_test = 100 

X_train, y_train, X_test, y_test = generate_data(n_train=n_train, n_test=n_test, contamination=contamination)

#Doesn't work (Must provide either V or VI for Mahalanobis distance)
clf = KNN(algorithm='brute', metric='mahalanobis', metric_params={'V': np.cov(X_train)})
clf.fit(X_train)

#Works
nn = NearestNeighbors(algorithm='brute', metric='mahalanobis', metric_params={'V': np.cov(X_train)})
nn.fit(X_train)

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
yzhao062commented, Oct 14, 2018

Thanks for reporting this. It is due to the problem of passing parameters into KDTree. Specifically, it is caused by the difference between the parameters of NearestNeighbors and KDTree…KDTree has a distinct way of using customized distance metrics.

I will fix it in this next few days and update you here.

1reaction
yzhao062commented, Jun 23, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

KNN Mahalanobis error - size of V does not match - Python
I am trying to implement a KNN model, using Mahalanobis as the distance metric, however when I execute the code I am getting...
Read more >
KNN using Mahalanobis distance gives low score [closed]
I want to get average score of all possible K but the average accuracy I'm getting is much lower than what's given to...
Read more >
Efficiently Compute KNN with Mahalanobis Distance
On the other hand, Mahalanobis distance measure first transforms the features into uncorrelated features with variances equal to 1 by dividing the features...
Read more >
Mahalanobis Distance - Understanding the math with ...
Mahalanobis distance is an effective multivariate distance metric that measures the distance between a point and a distribution.
Read more >
Classification error for the various Mahalanobis distances.
We consider variations of the Mahalanobis distance measures which rely on the inverse covariance matrix of the data. Unfortunately --- for time series...
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