Skip to content Skip to sidebar Skip to footer

On Colab - Class_weight Is Causing A Valueerror: The Truth Value Of An Array With More Than One Element Is Ambiguous. Use A.any() Or A.all()

i'm running a CNN with keras sequential on google colab. i'm getting the following error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any

Solution 1:

The problem is that the sklearn API returns a numpy array but the keras requires a dictionary as an input for class_weight (see here). You can resolve the error using below method:

from sklearn.utils import class_weight
weight = class_weight.compute_class_weight('balanced', np.unique(y_train), y_train)
weight = {i : weight[i] for i in range(5)}

Solution 2:

class_weights = sklearn.utils.class_weight.compute_class_weight('balanced', np.unique(labels[i]), labels[i])
class_weights = {l:c for l,c in zip(np.unique(labels[i]), class_weights)}

Post a Comment for "On Colab - Class_weight Is Causing A Valueerror: The Truth Value Of An Array With More Than One Element Is Ambiguous. Use A.any() Or A.all()"