【问题标题】:What is the easiest way to obtain the confusion matrix from python CRFSuite?从 python CRFSuite 获取混淆矩阵的最简单方法是什么?
【发布时间】:2021-03-21 05:10:52
【问题描述】:

我正在尝试从python CRFsuite 获取混淆矩阵。

这是我的代码:

from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, pred_y, normalize='true', labels=lables)

错误:

ValueError: You appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead - the MultiLabelBinarizer transformer can convert to this format.

我尝试使用MultiLabelBinarizer(),但仍然无法得到混淆矩阵。

在谷歌搜索后,我发现了这个answer,它说对于混淆矩阵函数,您必须将y_testpred_y 展平。我查看了 CRFsuite 的其他指标的源代码here,它们确实使用了一个 fallaten 函数:

def _flattens_y(func):
    @wraps(func)
    def wrapper(y_true, y_pred, *args, **kwargs):
        y_true_flat = flatten(y_true)
        y_pred_flat = flatten(y_pred)
        return func(y_true_flat, y_pred_flat, *args, **kwargs)
    return wrapper

但是没有获取confusion matrix的功能。

y_testpred_y 是嵌套列表。

如何将y_testpred_y 展平以获得混淆矩阵?

谢谢。

【问题讨论】:

标签: python machine-learning confusion-matrix crf python-crfsuite


【解决方案1】:
from itertools import chain

f_y_test = list(chain.from_iterable(y_test))
f_pred_y = list(chain.from_iterable(pred_y))

【讨论】:

  • 请添加有关此回复如何应用于该问题的更多详细信息。
猜你喜欢
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 1970-01-01
  • 2018-10-02
相关资源
最近更新 更多