I am printing out the precision score and confusion matrix using sklearn.

print("Confusion matrix:")
print(confusion_matrix(test_y, predict_y))
print("Precision:", precision_score(test_y, predict_y))

The output is:

Confusion matrix:
[[910  16]
 [ 47 177]]
Precision: 0.917098445595855

According to the confusion matrix:

True positive = 177 False positive = 47

Precision should be 177/(177+47) or about 0.79. This doesn't match what sklearn is showing as precision. What am I doing wrong here?


$177/(177+16) = 0.9170984$, so it looks like the top right cell ($16$) is the False Positives, rather than the bottom left one ($47$). Looks like a simple mismatch between your understanding and the actual code.

Calculating something like this by hand is a great way to check that we understand our code correctly. Cf. unit testing.

| cite | improve this answer | |
  • 1
    $\begingroup$ Ah, my understanding of confusion_matrix() was wrong. I found the documentation hard to parse mentally: "Thus in binary classification, the count of true negatives is C00, false negatives is C10, true positives is C11, and false positives is C01." Simply stated, the output of confusion matrix is [[Tn, Fp], [Fn, Tp]]. $\endgroup$ – RajV Oct 6 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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