4
$\begingroup$

Working in a multi-label classification problem with 13 possibles outputs in my neural network with Keras, sklearn, etc...

Each output can be an array like [0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 ,0].

I have an imbalance dataset and i trying to apply compute_class_weight method, like:

class_weight = compute_class_weight('balanced', np.unique(Y_train), Y_train)

When i try to run my code, i got Unhashable Type: 'numpy.ndarray':

Traceback (most recent call last):
 File "main.py", line 115, in <module>
   train(dataset, labels)
 File "main.py", line 66, in train
   class_weight = compute_class_weight('balanced', np.unique(Y_train), Y_train)
 File "/home/python-env/env/lib/python3.6/site-packages/sklearn/utils/class_weight.py", line 41, in compute_class_weight
  if set(y) - set(classes):
  TypeError: unhashable type: 'numpy.ndarray'

I know that is because i working with arrays, already tried add some dict,

i.e.:

class_weight_dict = dict(enumerate(np.unique(y_train), class_weight))

Well, i don't know what to do, tried others strategies, but no success... Any ideas?

Thanks in advance!

$\endgroup$
3
$\begingroup$

You're seeing this error because your Y_train data is a 2d array, where compute_class_weights expects a 1d array.

compute_class_weights can be used for multiclass classifications, but apparently not multi-label problems like yours.

You could try using compute_sample_weight instead, which is slightly different but handles multi-label output problems such as this.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.