confusion_matrix-太多值无法解压缩 [英] confusion_matrix - too many values to unpack
问题描述
我正在尝试使用 confusion_matrix 函数,如下所示:
I'm trying to use the confusion_matrix function, as follows:
tn, fp, fn, tp = confusion_matrix(y_true, y_predict).ravel()
y_true
和 y_predict
均为列表.当我返回它们的形状时,我得到:(71,)
.
y_true
and y_predict
are both lists. When I return their shape, I get: (71,)
.
但是,我收到上述语句的以下错误:
I'm however getting the following error for the above statement:
ValueError: too many values to unpack
我不确定是否是因为(71,)
中的第二个(空)维?如果这是这里的问题,我不确定如何将其删除.
I'm not sure if it is because of the second (empty) dimension in (71,)
? I'm not sure how to remove it if it is the issue here.
有什么想法吗?
谢谢.
推荐答案
如果输出的数量一定,则只能动态分配多个变量.如果将 confusion_matrix
的结果分配给单个变量,则可以循环检查其内容并有条件地分配内容:
You can only assign multiple variables dynamically if the number of outputs is certain. If you assign the result of confusion_matrix
to a single variable, you can then check its contents in a loop and assign the contents conditionally:
returned = confusion_matrix(y_true,y_predict).ravel()
for var in returned:
#... do stuff with each item in the returned collection
您也可以只检查其长度,如果长度为4,则可以照常进行操作:
You could also just check its length and if it is 4, you can proceed as usual:
if len(returned) == 4:
tn, fp, fn, tp = returned
这篇关于confusion_matrix-太多值无法解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!