DEV Community

Cover image for What Is Confusion Matrix In Machine Learning world?
Deepak Raj
Deepak Raj

Posted on • Originally published at

What Is Confusion Matrix In Machine Learning world?

This Article was originally published on CodePerfectPlus


Have you been in a situation where you expected your machine learning model to perform really well but it sputtered out a poor accuracy? You’ve done all the hard work – so where did the classification model go wrong? How can you correct this?

Here comes the Confusion matrix is also known as an error matrix. The confusion matrix mostly used to evaluate the classification model performance. As the Name says confusion matrix can be confusing sometimes.

After Preprocessing, Cleaning, Fitting and Predicting the Data we check model performance to build a final production model.

What is Confusion Matrix

The confusion matrix mainly uses for binary classification. It's a comparison table of predict and actual values. There are two predicted class in binary classification yes(1) or no(0) (e.g. Alive or Death, Profit and Loss) .
Confusion matrix consist of 4 class True Positive(TP), True Negative(TN), False Positive(FP), False Negative(FN).

Confusion matrix

True Positive:

You projected positive and it turns out to be true. For example, you had predicted that France would win the world cup, and it won.

True Negative:

When you predicted negative, and it's true. You had predicted that England would not win and it lost.

False Positive:

Your prediction is positive, and it is false.

You had predicted that England would win, but it lost.

False Negative:

Your prediction is negative, and the result is also false.

You had predicted that France would not win, but it won.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix

iris = load_iris()
X =
y =

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=24)

clf = LogisticRegression().fit(X_train, y_train)
y_predict = clf.predict(X_test)
cMatrix = confusion_matrix(y_test, y_predict)
Enter fullscreen mode Exit fullscreen mode

More Articles by Author

Join for Weekly Updates.

React ❤️ to encourage Author.

Top comments (0)