【发布时间】: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_test 和pred_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_test 和 pred_y 是嵌套列表。
如何将y_test 和pred_y 展平以获得混淆矩阵?
谢谢。
【问题讨论】:
-
您可能会发现sklearn's classification report 很有用。
标签: python machine-learning confusion-matrix crf python-crfsuite