import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
#-----------------------------------------------------------
# Generate Data
#-----------------------------------------------------------
np.random.seed(2)
n = 1000
y_true = np.random.choice([0,1],size=n, p=[0.9, 0.1])
flip1s = np.random.choice([0,1], size=n, p=[0.9, 0.1])
y_pred = np.where( (y_true == 1) & (flip1s == 1), (y_true + 1)%2, y_true )
flip0s = np.random.choice([0,1], size=n, p=[0.8, 0.2])
y_pred = np.where( (y_true == 0) & (flip0s == 1), (y_true + 1)%2, y_pred )
acc = np.sum(y_pred == y_true) / len(y_true)
print(acc)
Pred Neg | Pred Pos | |
Actual Neg | TN | FP |
Actual Pos | FN | TP |
print(confusion_matrix(y_true, y_pred))
Recall = $\Large\frac{TP}{TP + FN} = \Large\frac{TP}{\textrm{Number of Positive Samples}}$
Precision = $\Large\frac{TP}{TP + FP} = \Large\frac{TP}{\textrm{Number of Positive Predictions}}$
Each class can be viewed as having its own precision and recall values.
Positive Recall = $\Large\frac{TP}{TP + FN} = \Large\frac{TP}{\textrm{Number of Positive Samples}}$
Positive Precision = $\Large\frac{TP}{TP + FP} = \Large\frac{TP}{\textrm{Number of Positive Predictions}}$
Negative Recall = $\Large\frac{TN}{TN + FP} = \Large\frac{TN}{\textrm{Number of Negative Samples}}$
Netgative Precision = $\Large\frac{TN}{TN + FN} = \Large\frac{TN}{\textrm{Number of Negative Predictions}}$
print(classification_report(y_true, y_pred))
#-----------------------------------------------------------
# Generate Data
#-----------------------------------------------------------
np.random.seed(1)
n = 1000
y_true = np.random.choice([0,1,2],size=n, p=[0.4, 0.3, 0.3])
shift = np.random.choice([1,2], size=n)
flip0s = np.random.choice([0,1], size=n, p=[0.8, 0.2])
y_pred = np.where( (y_true == 0) & (flip0s == 1), (y_true + shift)%3, y_true )
flip1s = np.random.choice([0,1], size=n, p=[0.7, 0.3])
y_pred = np.where( (y_true == 1) & (flip1s == 1), (y_true + shift)%3, y_pred )
flip2s = np.random.choice([0,1], size=n, p=[0.94, 0.06])
y_pred = np.where( (y_true == 2) & (flip2s == 1), (y_true + shift)%3, y_pred )
acc = np.sum(y_pred == y_true) / len(y_true)
print(acc)
print(confusion_matrix(y_true, y_pred))
print(classification_report(y_true, y_pred))