Confusion Matrix in Python — ELI5

Imagine you have a dog and a cat, and you ask your little cousin to sort photos of them into two piles: “dog photos” and “cat photos.” After sorting, you check the piles and make a scorecard.

The scorecard has four boxes:

  • Photos of dogs that went into the dog pile — great, that is a hit.
  • Photos of cats that went into the cat pile — also a hit.
  • Photos of dogs that accidentally ended up in the cat pile — oops, your cousin was confused.
  • Photos of cats that ended up in the dog pile — another mix-up.

That scorecard is a confusion matrix. It does not just say “you got 80 percent right.” It shows you exactly where the mistakes happened. Maybe your cousin is perfect at recognizing dogs but keeps putting fluffy cats in the dog pile because they look like small dogs.

A computer sorting data works the same way. The confusion matrix lets you peek inside its answer sheet and see which categories it mixes up. That is much more helpful than a single grade.

Once you know where the confusion lives, you can fix it — maybe by giving the computer more examples of those tricky fluffy cats.

One thing to remember: A confusion matrix does not just tell you if the computer was wrong — it tells you exactly how it was wrong, which is the first step toward fixing it.

pythonconfusion-matrixmachine-learningclassification

See Also

  • Python Cross Validation Find out why testing a computer's homework on different practice sets keeps it from cheating.
  • Python Model Evaluation Metrics Discover why asking 'how good is my model?' needs more than one number to get an honest answer.
  • Python Roc Auc Curves Understand how one picture and one number tell you whether a computer's predictions are trustworthy or just lucky guesses.
  • Python Sklearn Learning Curves Why your machine learning model might need more data — or a simpler brain — explained with zero jargon.
  • Activation Functions Why neural networks need these tiny mathematical functions — and how ReLU's simplicity accidentally made deep learning possible.