0

I would like to use the KNN model of Sklearn.
In order to optimize hyper parameters (k and the distance), I use GridsearchCV from the package.
It works when i just use euclidean, minkowski and chebyshev distances. However when i add mahalanobis and seuclidean distances, the program runs few minutes and then, return this error :

"Buffer has wrong number of dimensions (expected 1, got 2) " and the core crash.
How fix it ?

My data : data

V=np.cov(X_train3.T)
VI=np.linalg.inv(V)
interval=np.arange(1,30)
paramétres = [
 {'n_neighbors':interval , 'metric': ['euclidean', 'minkowski','chebyshev']},

 {'n_neighbors': interval, 'metric': ['mahalanobis', 'seuclidean'],'metric_params': [{'V':V },{'VI':VI }]}
]

grid=GridSearchCV(KNeighborsClassifier(),paramétres,cv=10)
grid.fit(X_train3,Y_train3)
print(grid.best_params_)

---------------------------------ERROR------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
  • read this: https://stackoverflow.com/questions/34643548/how-to-use-mahalanobis-distance-in-sklearn-distancemetrics – Roim May 12 '20 at 19:01
  • Thanks, as mentioned is the post, the option algorithm wich is by default on "auto" doesn't work and we are forced to set it on "brute". Moreover, seuclidian and mahalanobis can't be in the same metric because the requiered parameter "V" is not the same especially the shape required of V in seuclidan is 1. – Victor Goubet May 12 '20 at 20:12

0 Answers0