【问题标题】:Confusion Matrix: ValueError: Classification metrics can't handle a mix of unknown and multiclass targets混淆矩阵:ValueError:分类指标无法处理未知目标和多类目标的混合
【发布时间】:2021-11-21 21:27:20
【问题描述】:

我的脚本很长,但重点在这里:

result = confusion_matrix(y_test, ypred)

y_test 在哪里

    >>> 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

值是

>>> 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)

ypred

>>> 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])

给予

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

令人困惑的是我没有看到任何个未知目标。

所以我检查了ValueError: Classification metrics can't handle a mix of unknown and binary targets,但那里的解决方案不适用于我的情况,因为所有值都是整数。

我还检查了Skitlearn MLPClassifier ValueError: Can't handle mix of multiclass and multilabel-indicator,但我的数据中没有任何编码。

如何获得混淆矩阵并避免这些错误?

【问题讨论】:

  • 你试过result = confusion_matrix(y_test.values, ypred)吗?看来您的 y_test 不仅仅包含类
  • 我试过了,但没用
  • 如果您提供更多信息会有所帮助。我的猜测是 y_test 是问题所在。它的形状是什么?有多个列,这就是错误显示多类目标的原因

标签: python-3.x scikit-learn


【解决方案1】:

这个错误是由于类型混淆造成的。

解决方案是将 y_test 值作为列表转换为confusion_matrix

result = confusion_matrix(list(y_test.values), ypred)

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 2019-09-07
    • 2021-05-14
    • 2020-04-03
    • 2019-05-29
    • 2018-08-26
    • 2019-10-23
    • 2019-06-29
    相关资源
    最近更新 更多