A confusion matrix is a technique for summarizing the performance of a classification algorithm. It is represented in an N x N matrix form where N is the number of target or output classes.

Let’s say we have gender classification problem where input is sample of 12 pictures where 8 are Males and 4 of Females .Lets classify Males belong to class 1(Positive) and Females belong to class 0(Negative) .

Sample | Actual | Predicted | True Condition |
---|---|---|---|

1 | M | F | FN |

2 | M | M | TP |

3 | F | F | TN |

4 | M | M | TP |

5 | M | M | TP |

6 | F | F | TN |

7 | M | M | TP |

8 | F | M | FP |

9 | M | M | TP |

10 | F | F | TN |

11 | M | M | TP |

12 | M | F | FN |

The 2X2 confusion matrix would be represented as below.

Actual | ||
---|---|---|

Predicted | Male | Female |

Male | 6 | 1 |

Female | 2 | 3 |

Actual | ||
---|---|---|

Predicted | P | N |

P | TP = 6 | FP = 1 |

N | FN = 2 | TN = 3 |

The table on right represents the same information in terms of true condition.Lets understand the terminologies

** TP** = A Positive’s prediction is True** FP** = A Positive’s prediction is False ( Type I Error )** FN** = A Negative’s prediction is True** TN **= A Negative’s prediction is False ( Type II Error )

Classification accuracy is measured through different metrics.

**Accuracy**

This gives how much we predicted correctly.

Accuracy = correct predictions / total predictions

= ( TP + TN ) / ( TP + FP +TN +FN )

**Precision**

Precision = (TP) / (TP+FP)

Measure how many “Positive” predictions are actually correct from all “Positive” predictions .ie when predicted “Positive” how often it is correct .

**Recall**

Recall = (TP) / (TP+FN)

It is the measure of correctness of for predictions made for all the “Positive” class .

**Classification report through sklearn**

```
# Example of a confusion matrix in Python
from sklearn import metrics
#You need to do actual Predictions to arrive at below values
Actual = [1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1]
Predicted = [0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0]
cm = metrics.confusion_matrix(Actual, Predicted)
print("============== Confusion Matrix ==================")
print(cm)
print("============ Classification Report ===============")
cr=metrics.classification_report(Actual, Predicted)
print(cr)
print("Precision score: {}".format(metrics.precision_score(Actual,Predicted)))
print("Recall score: {}".format(metrics.recall_score(Actual,Predicted)))
print("F1 Score: {}".format(metrics.f1_score(Actual,Predicted)))
```

The output is below.

**References**