Skip to content Skip to sidebar Skip to footer

Sklearn The Truth Value Of An Array With More Than One Element Is Ambiguous. Use A.any() Or A.all() Error

I'm trying out a code on training datasets that I saw online, but can't seem to resolve the error as mentioned. When I first ran the code, I get the above error as such: ValueError

Solution 1:

You are using check_cv wrong. According to the documentation:-

check_cv(cv=’warn’, y=None, classifier=False):

cv : int, 
     cross-validation generator or an iterable, optional

y : array-like, optional
    The target variable for supervised learning problems.

classifier : boolean, optional, defaultFalse
             Whether the task is a classification task, 
             in which case stratified KFold will be used

So it wants y and estimator in input. But you are providing X and y which is wrong. Change the below lines:

cv = check_cv(self.cv, X, y)
knn = KNeighborsClassifier(metric='precomputed', algorithm='brute')

to:

knn = KNeighborsClassifier(metric='precomputed', algorithm='brute')
cv = check_cv(self.cv, y, knn)

Note the order of lines.

Post a Comment for "Sklearn The Truth Value Of An Array With More Than One Element Is Ambiguous. Use A.any() Or A.all() Error"