如何让sklearn.metrics.confusion_matrix()始终返回TP,TN,FP,FN?

Sco*_*d C 5 python confusion-matrix scikit-learn

我正在使用sklearn.metrics.confusion_matrix(y_actual, y_predict)提取tn,fp,fn,tp,并且大多数时候它完美地工作.

from sklearn.metrics import confusion_matrix

y_actual, y_predict = [1,1,1,1], [0,0,0,0]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict).ravel()
>>> [0 0 4 0]   # ok

y_actual, y_predict = [1,1,1,1],[0,1,0,1]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict).ravel()
>>> [0 0 2 2]   # ok
Run Code Online (Sandbox Code Playgroud)

但是,在某些情况下,confusion_matrix()并不总是返回这些信息,我会得到ValueError,如下所示.

from sklearn.metrics import confusion_matrix

y_actual, y_predict = [0,0,0,0],[0,0,0,0]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict).ravel()
>>> [4]    # ValueError: not enough values to unpack (expected 4, got 1)

y_actual, y_predict = [1,1,1,1],[1,1,1,1]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict).ravel()
>>> [4]    # ValueError: not enough values to unpack (expected 4, got 1)
Run Code Online (Sandbox Code Playgroud)

我的临时解决方案是编写自己的函数来提取这些信息.有什么方法可以强制confusion_matrix()总是返回tn,fp,fn,tp输出?

谢谢

kdd*_*kdd 8

此问题与输入矩阵中包含的唯一标签数量有关.在你的第二个例子中,它(正确地)构建一个只有一个类的混淆矩阵,分别为0或1.

要强制它输出两个类,即使没有预测其中一个类,也要使用该label属性.

y_actual, y_predict = [0,0,0,0],[0,0,0,0]
tn, fp, fn, tp = confusion_matrix(y_actual, y_predict, labels=[0,1]).ravel()
>> array([[4, 0],
          [0, 0]])
Run Code Online (Sandbox Code Playgroud)