0

I have a long script, but the key point is here:

result = confusion_matrix(y_test, ypred)

where y_test is

    >>> y_test
    ZFFYZTN     3
    ZDDKDTY     0
    ZTYKTYKD    0
    ZYNDQNDK    1
    ZYZQNKQN    3
               ..
    ZYMDDTM     3
    ZYLNYFLM    0
    ZTNTKDY     0
    ZYYLZNKM    3
    ZYZMQTZT    0

Name: BT, Length: 91, dtype: object

and the values are

>>> y_test.values
array([3, 0, 0, 1, 3, 0, 0, 1, 0, 3, 1, 0, 3, 1, 0, 0, 3, 0, 3, 0, 0, 0,
       1, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 2, 3, 3, 0, 0, 3, 3, 1, 1, 0, 2,
       0, 0, 0, 3, 3, 3, 1, 0, 3, 3, 3, 2, 3, 3, 0, 1, 0, 3, 3, 0, 0, 0,
       0, 0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 3, 3, 3, 0,
       0, 3, 0], dtype=object)

and ypred is

>>> ypred
array([3, 0, 0, 1, 3, 0, 0, 1, 0, 3, 1, 0, 3, 1, 0, 0, 3, 0, 3, 0, 0, 0,
       1, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 2, 3, 3, 0, 0, 3, 3, 1, 1, 0, 2,
       0, 0, 0, 3, 3, 3, 1, 0, 3, 3, 3, 2, 3, 3, 0, 1, 0, 3, 3, 0, 0, 0,
       0, 0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 3, 3, 3, 0,
       0, 3, 0])

gives

    raise ValueError("Classification metrics can't handle a mix of {0} "
ValueError: Classification metrics can't handle a mix of unknown and multiclass targets

The confusing part is that I don't see any unknown targets.

so I checked out ValueError: Classification metrics can't handle a mix of unknown and binary targets but the solution there doesn't apply in my case, because all values are integers.

I've also checked Skitlearn MLPClassifier ValueError: Can't handle mix of multiclass and multilabel-indicator but there aren't any encodings in my data.

What can I do to get the confusion matrix and avoid these errors?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
con
  • 5,767
  • 8
  • 33
  • 62
  • 1
    did you try `result = confusion_matrix(y_test.values, ypred)`? It seems that your `y_test` contains more than only classes – krisograbek Sep 30 '21 at 02:57
  • I tried that, but it didn't work – con Sep 30 '21 at 03:03
  • It would help if you provided more info. My guess is that y_test is the problem. What is its shape? There are multiple columns, this is why the error says multiclass targets – krisograbek Sep 30 '21 at 03:19

1 Answers1

2

This error is due to confusing types.

The solution is to cast y_test values as a list to confusion_matrix:

result = confusion_matrix(list(y_test.values), ypred)
con
  • 5,767
  • 8
  • 33
  • 62