KNN Mahalanobis distance error
See original GitHub issueHi,
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:
- Created 5 years ago
- Reactions:1
- Comments:12 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@lachhebo Yeah I think so. See the example provided here: https://github.com/yzhao062/pyod/blob/development/examples/knn_mahalanobis_example.py