小编kdd*_*kdd的帖子

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

我正在使用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)

python confusion-matrix scikit-learn

5
推荐指数
1
解决办法
3402
查看次数

标签 统计

confusion-matrix ×1

python ×1

scikit-learn ×1