What Is Confusion Matrix In Machine Learning world?
Tags: machinelearning, datascience, python
Published: 2020-07-21 11:26:21

This Article was originally published on [CodePerfectPlus](http://codeperfectplus.herokuapp.com/)

**Introduction**

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).`

<img src="https://raw.githubusercontent.com/DrakeEntity/project-Image/master/main-qimg-db821005f3d1712163c9115ff4f6caf2.png" class="img-fluid" alt="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.

```python
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 = iris.data
y = iris.target

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)
print(cMatrix)
```

### More Articles by Author

- [Build Your First Python Chatbot In 5 Minutes](http://codeperfectplus.herokuapp.com/build-your-first-python-chatbot-in-5-minutes)
- [What is Simple Linear Regression?](http://codeperfectplus.herokuapp.com/what-is-simple-linear-regression)
- [Logistic Regression for Machine Learning Problem](http://codeperfectplus.herokuapp.com/logistic-regression-for-machine-learning-problem)
- [5 Tips for Computer Programming Beginners](http://codeperfectplus.herokuapp.com/5-tips-for-computer-programming-beginners)
- [What Is Git and GitHub?](http://codeperfectplus.herokuapp.com/what-is-git-and-gitHub)

### Join for Weekly Updates.

- [Whatapp Group](https://chat.whatsapp.com/H8lJop5N3AI0BgY9CbKze8)

- [Telegram Channel](https://t.me/codeperfectplus)

React ❤️ to encourage Author.