+2 votes

Best answer

You can use functions of sklearn library or a simple python script to caculate true positive, true negative, false positive, and false negative. Here are two approaches; you can try one of these:

**Approach 1:**

>>> y_true = [1, 0, 1, 0, 0, 1, 1, 0, 1]

>>> y_pred = [1, 1, 0, 0, 1, 1, 1, 1, 0]

>>> from sklearn.metrics import confusion_matrix

>>> confusion_matrix(y_true, y_pred)

array([[1, 3],

[2, 3]], dtype=int64)

>>> confusion_matrix(y_true, y_pred).ravel()

array([1, 3, 2, 3], dtype=int64)

>>> tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()

>>> tn, fp, fn, tp

(1, 3, 2, 3)

**Approach 2:**

>>> y_true = [1, 0, 1, 0, 0, 1, 1, 0, 1]

>>> y_pred = [1, 1, 0, 0, 1, 1, 1, 1, 0]

>>> (tn, fp, fn, tp) = (0,0,0,0)

>>> for i in range(len(y_true)):

... if (y_true[i]==1 and y_pred[i] ==1):

... tp+=1

... elif (y_true[i]==1 and y_pred[i] ==0):

... fn+=1

... elif (y_true[i]==0 and y_pred[i] ==1):

... fp+=1

... elif (y_true[i]==0 and y_pred[i] == 0):

... tn+=1

...

>>> tn, fp, fn, tp

(1, 3, 2, 3)