# Confusion matrix and precision problem

I'm trying to calculate the precision of a trained model. I have generated the right values for the true positive rate and the false positive rate. And I know that the formula should be TP/TP + FP. But I can't seem to generate the right values in python.

My code:

true_positives_0 = confusion_matrix_0[0, 0]
false_positives_0 = confusion_matrix_0[0, 1]
precision_sex_0 = true_positives_0 / (true_positives_0 + false_positives_0)

true_positives_1 = confusion_matrix_1[0, 0]
false_positives_1 = confusion_matrix_1[0, 1]
precision_sex_1 = true_positives_1 / (true_positives_1 + false_positives_1)

print("The precision for sex 0 is: {:0.3f}".format(precision_sex_0))
print("The precision for sex 1 is: {:0.3f}".format(precision_sex_1))


What am I doing wrong?

• Welcome to DataScienceSE. It depends how your confusion_matrix_0 and confusion_matrix_1 have been initialized, this is probably where the problem is. Generally it does not make sense to take the precision of both classes in a binary problem. Jan 17, 2023 at 17:52

tn, fp, fn, tp = confusion_matrix(y_true, y_pred)