简体   繁体   中英

Confusion matrix sklearn bug?

I was doing a test with sklearn.metrics.confusion_matrix to see what happen if in the prediction array there is a class which is not in the labels and mapping arrays. My code is:

from sklearn.metrics import confusion_matrix as cm

a = ["positive\n", "positive\n", "negative\n", "positive\n", "negative\n"]
b = ["negative\n", "negative\n", "don't\n", "negative\n", "negative\n"]
m = ["positive\n", "negative\n"]
c = cm(a, b, m)
TN, FP, FN, TP = c.ravel()

print(c)
print("")
print("{} {} {} {}\n".format(TN, FP, FN, TP))

The ouput is:

[[0 3]
 [0 1]]

0 3 0 1

So the class don't is skipped.


But if you look at the documentation for the version v0.21.2 which is the one I installed the ravel() method "should" output the values of the confusion matrix as I wrote: TN, FP, FN, TP. The output of my print is different. It seems that the real ouput of ravel() is flipped: TP, FN, FP, TN. Is my thought right?

There is no bug. You have defined labels:

m = ["positive\n", "negative\n"]

therefore "positive\\n" is negative and "negative\\n" is positive. And the result meets your specification.

If you modify m this way:

m = ["negative\n", "positive\n"]

you will get:

1 0 3 0

for TN, FP, FN, TP , respectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM