Sklearn NearestNeighbors with mahalanobis distance returns error for slice of array

1.6k Views Asked by At

The following code returns an error:

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

nn.fit(X).kneighbors(X[:1, :]) 

Returns the following error:

/usr/local/lib/python3.7/dist-packages/sklearn/metrics/pairwise.py in _precompute_metric_params(X, Y, metric, **kwds)
   1545         else:
   1546             raise ValueError(
-> 1547                 "The 'VI' parameter is required for the mahalanobis metric "
   1548                 "when Y is passed."
   1549             )

ValueError: The 'VI' parameter is required for the mahalanobis metric when Y is passed.

I'm not sure what the error message means, as I'm not passing in Y anywhere. In addition, if I change X[:1,:] to X in the last line, then the code runs fine, but I prefer not running this on the entire input array for speed reasons.

0

There are 0 best solutions below