Skip to content Skip to sidebar Skip to footer

Arrays Tp, Tn, Fp And Fn In Python

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], Prediction

Solution 1:

When using np.argmax the matrices that you input sklearn.metrics.confusion_matrix isn't binary anymore, as np.argmax returns the index of the first occuring maximum value. In this case along axis=1.

You don't get the good'ol true-positives / hits, true-negatives / correct-rejections, etc., when your prediction isn't binary.

You should find that sum(sum(cm)) indeed equals 200.


If each index of the arrays represents an individual prediction, i.e. you are trying to get TP/TN/FP/FN for a total of 200 (10 * 20) predictions with the outcome of either 0 or 1 for each prediction, then you can obtain TP/TN/FP/FN by flattening the arrays before parsing them to confusion_matrix. That is to say, you could reshape TestArray and PreditionArry to (200,), e.g.:

cm = confusion_matrix(TestArray.reshape(-1), PredictionArray.reshape(-1))

TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]

print(TN, FN, TP, FP, '=', TN + FN + TP + FP)

Which returns

74 28 73 25 = 200

Post a Comment for "Arrays Tp, Tn, Fp And Fn In Python"