我正在使用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, …
Run Code Online (Sandbox Code Playgroud)