con
con

Reputation: 6093

Confusion Matrix: ValueError: Classification metrics can't handle a mix of unknown and multiclass targets

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?

Upvotes: 0

Views: 5088

Answers (1)

con
con

Reputation: 6093

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)

Upvotes: 3

Related Questions