While fitting a numpy array with OPTICS and using mahalanobis distance in metric, It raises the issue of V or VI{covariance matrix}. After passing the co-variance matrix via metric_params with brute setting, It still does not fit.
import warnings
warnings.filterwarnings(action='ignore')
import numpy as np
from sklearn.cluster import OPTICS
# distances
scipy_spatial_distances = ['braycurtis', 'canberra', 'chebyshev', 'correlation',
'dice', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis',
'minkowski', 'rogerstanimoto','russellrao', 'seuclidean',
'sokalmichener','sokalsneath','sqeuclidean','yule']
# test array
test_array = np.random.rand(25,10)
for distance in scipy_spatial_distances:
try:
OPTICS(metric=distance).fit(test_array)
except Exception as e:
print('FAILURE: {}\nERROR_MSG: {}\n'.format(distance,e))
After passing covariance matrix, it raises LinAlgError
OPTICS(metric='mahalanobis',metric_params={'V': np.cov(test_array)}).fit(test_array)
changing the algorithm to brute also doesnt seem to work
test_array = np.random.rand(25,10)
OPTICS(algorithm='brute',metric='mahalanobis',metric_params={'V': np.cov(test_array)}).fit(test_array)
This is very rare. Had to run the following cell a couple of time till it fitted.
Should I be doing this to my distribution: https://stackoverflow.com/questions/44305456/why-am-i-getting-linalgerror-singular-matrix-from-grangercausalitytests?rq=1
test_array = np.random.rand(25,10)
OPTICS(algorithm='brute',metric='mahalanobis',metric_params={'V': np.cov(test_array)}).fit(test_array)