1
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – Erwan
    Jan 17, 2023 at 17:52

1 Answer 1

6
$\begingroup$

I think you retrieved the wrong values for tn, tp, fn, fp.

The confusion matrix from sklearn is

The proper way to retrieve these numbers is:

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

Hope this helps!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.