从python CRFSuite获取混淆矩阵的最简单方法是什么?

2024-05-29 07:38:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从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以获得混淆矩阵

多谢各位


Tags: to函数testtruedefargs矩阵wrapper

热门问题