8

I am using Scikit-learning and I need to calculate the True positive (TP), the False Positive (FP), the True Negative (TN) and the False Negative (FN) from a confusion matrix like this:

[[2 0 3 4]
 [0 4 5 1]
 [1 0 3 2]
 [5 0 0 4]]

I know how to calculate the TP, the FP and the FN but I don't know how to get the TN. Can someone tell me?

Euskalduna
  • 1,517
  • 2
  • 13
  • 12
  • Does this answer your question? [Scikit-learn: How to obtain True Positive, True Negative, False Positive and False Negative](https://stackoverflow.com/questions/31324218/scikit-learn-how-to-obtain-true-positive-true-negative-false-positive-and-fal) – Chris Tang Feb 22 '20 at 05:36

2 Answers2

9

I think you should treat this multi-class classification in a one-vs-the-rest manner (so each 2x2 table i measures the performance of a binary classification problem that whether each obs belongs to label i or not). Consequently, you can calculate the TP, FP, FN, TN for each individual label.

import numpy as np

confusion_matrix = np.array([[2,0,3,4],
                             [0,4,5,1],
                             [1,0,3,2],
                             [5,0,0,4]])

def process_cm(confusion_mat, i=0, to_print=True):
    # i means which class to choose to do one-vs-the-rest calculation
    # rows are actual obs whereas columns are predictions
    TP = confusion_mat[i,i]  # correctly labeled as i
    FP = confusion_mat[:,i].sum() - TP  # incorrectly labeled as i
    FN = confusion_mat[i,:].sum() - TP  # incorrectly labeled as non-i
    TN = confusion_mat.sum().sum() - TP - FP - FN
    if to_print:
        print('TP: {}'.format(TP))
        print('FP: {}'.format(FP))
        print('FN: {}'.format(FN))
        print('TN: {}'.format(TN))
    return TP, FP, FN, TN

for i in range(4):
    print('Calculating 2x2 contigency table for label{}'.format(i))
    process_cm(confusion_matrix, i, to_print=True)

Calculating 2x2 contigency table for label0
TP: 2
FP: 6
FN: 7
TN: 19
Calculating 2x2 contigency table for label1
TP: 4
FP: 0
FN: 6
TN: 24
Calculating 2x2 contigency table for label2
TP: 3
FP: 8
FN: 3
TN: 20
Calculating 2x2 contigency table for label3
TP: 4
FP: 7
FN: 5
TN: 18
Jianxun Li
  • 24,004
  • 10
  • 58
  • 76
  • 1
    Great answer! To add to the discussion a little, I'll point out that `scikit-learn` also have functionality for multi-class scoring metrics. If you were planning to aggregate TP,FP, FN, and TN into an ROC, I would suggest using the scoring metric methods that are [documented here](http://scikit-learn.org/stable/modules/classes.html#sklearn-metrics-metrics) and addressed in [the user guide here](http://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics). Metrics like F1-score get very confusing when lots of classes are introduced, so these metrics are VERY useful. – AN6U5 Jul 10 '15 at 22:56
-2

I think for a multiclass problem like this you have to decide which one of these 4 classes can be considered positive and you have to combine rest 3 as negative to calculate true negative.A detailed discussion was done here.

Community
  • 1
  • 1
0xF
  • 546
  • 4
  • 20