我正在尝试从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。我看了一下其他指标here的CRFsuite源代码,它们确实使用了一个下降函数:
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以获得混淆矩阵?
谢谢。
发布于 2020-12-10 03:36:25
from itertools import chain
f_y_test = list(chain.from_iterable(y_test))
f_pred_y = list(chain.from_iterable(pred_y))https://stackoverflow.com/questions/65223342
复制相似问题