Sim*_*ity 4 python list confusion-matrix scikit-learn
我正在尝试使用fusion_matrix函数,如下所示:
tn, fp, fn, tp = confusion_matrix(y_true, y_predict).ravel()
Run Code Online (Sandbox Code Playgroud)
y_true和y_predict都是列表。当我返回它们的形状时,我得到:(71,)。
但是,对于上述语句,我收到以下错误:
ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)
我不确定是否是因为 中的第二个(空)维度(71,)?如果这是这里的问题,我不确定如何删除它。
有什么想法吗?
谢谢。
仅当输出数量确定时,才可以动态分配多个变量。如果将 的结果分配confusion_matrix给单个变量,则可以在循环中检查其内容并有条件地分配内容:
returned = confusion_matrix(y_true, y_predict).ravel()
for var in returned:
#... do stuff with each item in the returned collection
Run Code Online (Sandbox Code Playgroud)
你也可以检查它的长度,如果它是 4,你可以像往常一样继续:
if len(returned) == 4:
tn, fp, fn, tp = returned
Run Code Online (Sandbox Code Playgroud)