0
$\begingroup$

My prediction results look like this

TestArray

[1,0,0,0,1,0,1,...,1,0,1,1],
[1,0,1,0,0,1,0,...,0,1,1,1],
[0,1,1,1,1,1,0,...,0,1,1,1],
.
.
.
[1,1,0,1,1,0,1,...,0,1,1,1],

PredictionArray

[1,0,0,0,0,1,1,...,1,0,1,1],
[1,0,1,1,1,1,0,...,1,0,0,1],
[0,1,0,1,0,0,0,...,1,1,1,1],
.
.
.
[1,1,0,1,1,0,1,...,0,1,1,1],

this is the size of the arrays that I have

TestArray.shape

Out[159]: (200, 24)

PredictionArray.shape

Out[159]: (200, 24)

I want to get TP, TN, FP and FN for these arrays

I tried this code

cm=confusion_matrix(TestArray.argmax(axis=1), PredictionArray.argmax(axis=1))
TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]
print(TN,FN,TP,FP)

but the results I got

TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]
print(TN,FN,TP,FP)

125 5 0 1

I checked the shape of cm

cm.shape

Out[168]: (17, 17)

125 + 5 + 0 + 1 = 131 and that does not equal the number of columns I have which is 200

I am expecting to have 200 as each cell in the array suppose to be TF, TN, FP, TP so the total should be 200

How to fix that?

Here is an example of the problem

import numpy as np
from sklearn.metrics import confusion_matrix


TestArray = np.array(
[
[1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,1,1,0,0,1],
[0,1,1,0,1,0,0,1,0,0,0,1,0,1,0,1,1,0,1,1],
[1,0,1,1,1,1,0,0,1,1,1,1,0,0,1,0,0,0,0,0],
[0,1,1,1,0,0,0,0,0,1,0,0,1,0,0,1,0,1,1,1],
[0,0,0,0,1,1,0,1,1,0,0,1,0,1,1,0,1,1,1,1],
[1,0,0,1,1,1,0,1,1,0,1,0,0,1,1,0,0,1,0,0],
[1,1,1,0,0,1,0,0,1,1,0,1,0,1,1,1,1,1,0,1],
[0,0,0,1,0,0,1,0,1,0,1,0,0,0,0,1,0,0,1,1],
[1,0,1,0,0,0,0,1,0,1,0,1,0,0,0,0,1,0,1,0],
[1,1,0,1,1,1,1,0,1,0,1,0,1,1,1,1,0,1,0,0]
])

TestArray.shape



PredictionArray = np.array(
[
[0,0,0,1,1,1,1,0,0,0,1,0,0,0,1,0,1,0,1,1],
[0,1,0,0,1,0,1,1,0,0,0,1,1,0,0,1,1,0,0,1],
[1,1,0,1,1,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0],
[0,1,0,1,0,0,1,0,0,1,0,1,1,0,0,1,0,0,1,1],
[0,0,1,0,0,1,0,1,1,1,0,1,1,1,0,0,1,1,0,1],
[1,0,0,1,0,1,1,1,1,0,0,1,0,1,1,1,0,1,1,0],
[1,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1,0,1,0,1],
[0,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1],
[1,0,1,1,0,0,0,1,0,1,0,1,1,1,1,0,0,0,1,0],
[1,1,0,1,1,1,1,1,1,0,1,0,0,0,0,1,1,1,0,0]
])

PredictionArray.shape

cm=confusion_matrix(TestArray.argmax(axis=1), PredictionArray.argmax(axis=1))
TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]

print(TN,FN,TP,FP)

The output is

5 0 2 0 

= 5+0+2+0 = 7 !!

There are 20 columns in the array and 10 rows

but cm gives to total of 7!!

How can I get the actual TP, TN, FP, and FN?

$\endgroup$
2
$\begingroup$

1) Your code :

Expected inputs for confusion matrix

sklearn.metrics.confusion_matrix expect $y_{pred}$ and $y_{true}$ to be $(n\_samples,)$ shape. :

y_true array-like of shape (n_samples,) Ground truth (correct) target values.

y_pred array-like of shape (n_samples,) Estimated targets as returned by a classifier.

Results of argmax on your matrix :

The argmax function on your test and prediction array is : for each row, where is the first (max element) of the row. So let's take the first four rows of your test set :

TestArray = np.array(
[
[1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,1,1,0,0,1],
[0,1,1,0,1,0,0,1,0,0,0,1,0,1,0,1,1,0,1,1],
[1,0,1,1,1,1,0,0,1,1,1,1,0,0,1,0,0,0,0,0],
[0,1,1,1,0,0,0,0,0,1,0,0,1,0,0,1,0,1,1,1],...]

Argmax returns : [0, 1, 0, 1, ...] Wich is the locations of the first 1 in each row. I imagine this is not what you expected.

2) What I propose :

If you want to get a confusion matrix that sums up to $(n_{rows}$ x $n_{columns})$ you have to ravel the inputs :

cm=confusion_matrix(TestArray.ravel(), PredictionArray.ravel())
TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]

print(TN,FN,TP,FP)

returns (sum up to 200) :

74 28 73 25
$\endgroup$
1
  • $\begingroup$ Thanks ravel() did the trick. $\endgroup$
    – asmgx
    Apr 4 '20 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.