Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Confusion Matrix
course content

Course Content

Classification with Python

Confusion MatrixConfusion Matrix

When we make a prediction for a binary classification problem, there are only four possible outcomes.

Note

In the image above, the actual values are in descending order, and the predicted values are in ascending. This is the layout used in the Scikit-learn for the confusion matrix(learned later in the chapter). You may encounter different layouts in other visualizations, but nothing apart from the order changes.

We call those outcomes True Positive(TP), True Negative(TN), False Positive(FP), False Negative(FN), where True/False stands for whether the prediction is correct and Positive/Negative stands for what is the predicted class 1 or 0.
So we can make two types of errors: False Positive and False Negative.
The case of False Positive prediction is also called a Type 1 Error.
And the case of False Negative prediction – Type 2 Error.

Confusion Matrix

The first way to look at the model's performance is to organize the predictions into a confusion matrix like this:

You can build a confusion matrix in Python using the confusion_matrix() from sklearn.

And, for better visualization, you can use the heatmap() function of sns(seaborn).

Here is an example of calculating the confusion matrix for a Random Forest prediction on the titanic dataset:

We can also plot the percentages instead of the instances count using the normalize parameter:

Everything was clear?

Section 5. Chapter 1
some-alt