- rishabhdwivedi062

# Confusion matrix in machine learning

Updated: Nov 7, 2022

A confusion matrix is a N x N matrix that is used to interpret model evaluation performance systematically. Here N is the number of distinct classes in the target variables that represent the Confusion matrix in machine learning.

Each row in a confusion matrix in machine learning represents an **Actual Class**, while each column represents a **Predicted Class**. The confusion matrix serves as the best platform of representation for most classification problems just like data analysis in machine learning, hence it gives you a lot of information, but sometimes you may prefer a more concise metric.

For the 2 x 2 matrix we have 2 classes:-

**Class 1**: Positive Class**Class 0**: Negative Class

Meaning of the parameters present in the matrix:-

*Let's consider the example of a person who is a cancer patient.*

**True Positive(TP)**: When a person is actually suffering from cancer and the predicted result is also true.**False Positive(FP)**: When a person is suffering from cancer but the predicted result is false.**False Negative(FN)**: When the person is not suffering but according to the predicted result he is suffering.**True Negative(TN)**: When a person is not a cancer patient and the predicted result also says that.

The following metrics are derived from the confusion matrix:-

#### 1. Precision

It is used when avoiding false positives is more essential than encountering false negatives.

**Precision **= (Predictions actually positive)/(Total predicted positive)

= (TP)/(TP+FP)

eg: If the above value is 33%, then we can say the model is 33% precise.

#### 2. Recall

It is used when avoiding false negatives is prioritized over encountering false positives.

Precision and recall handle the imbalance dataset efficiently.

**Recall**= (Predictions actually positive)/(Total actual positive)

= (TP)/(TP+FN)

eg: If the above value comes out to be 66%, then we can say that only 66% of the actual positive value was predicted as positive.

If the precision is high then recall will be low and vice-versa.

But if we are not sure from the graph or metrics then we can use **F1 **metrics for the task, which is nothing but the harmonic mean of precision and recall.

**F1 = 2/ (1/Precision + 1/Recall )**

**F1 is maximum when precision is equal to recall.**

But if precision, recall, and F1 score yield the same result then we need to use **log loss** function for the task, which is nothing but the cost function for logistic regression.

The smaller the value of log loss, the better the model is.

*Keep following the blogs for more structured learning*