[英]What is the easiest way to obtain the confusion matrix from python CRFSuite?
I am trying to obtain the confusion matrix from python CRFsuite .我正在尝试从python CRFsuite获取混淆矩阵。
This my code:这是我的代码:
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, pred_y, normalize='true', labels=lables)
error:错误:
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.
I tried to use MultiLabelBinarizer()
, but still couldn't get the confusion matrix.我尝试使用MultiLabelBinarizer()
,但仍然无法获得混淆矩阵。
After googling around I found this answer , it says that for the confusion matrix function you have to flatten the y_test
and pred_y
.在谷歌搜索后我找到了这个答案,它说对于混淆矩阵 function 你必须展平y_test
和pred_y
。 I took a look at the source code of CRFsuite for other metrics here , they do use a fallaten function:我在这里查看了其他指标的 CRFsuite 的源代码,它们确实使用了一个错误的 function:
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
But there is no function for obtaining the confusion matrix
.但是没有用于获得confusion matrix
的 function 。
The y_test
and pred_y
are nested lists. y_test
和pred_y
是嵌套列表。
How can I flatten the y_test
and pred_y
to obtain the confusion matrix?如y_test
和pred_y
以获得混淆矩阵?
Thank you.谢谢你。
from itertools import chain
f_y_test = list(chain.from_iterable(y_test))
f_pred_y = list(chain.from_iterable(pred_y))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.