# Understanding the Confusion Matrix (II)

### Banso D. Wisdom γ»8 min read

In the first part of this article, I talked about the confusion matrix in general, the 2-class confusion matrix, how to calculate accuracy, precision and other metrics using it and also how to generate a confusion matrix in python.

In this article, we'll be looking at the multi-class confusion matrix.

### What is the multi-class confusion matrix?

β As the name implies, it is a confusion matrix that deals with multiple classes (i.e. more than 2 classes). Just like the 2-class confusion matrix, it describes the performance of a multi-class classification model.

For the purpose of this article, we'll be assuming that our multi-class classification model is one that classifies images of dogs into the following breeds: **Greyhound**, **Mastiff** and **Samoyed**.

A confusion matrix for this classifier can be visualized as such:

In this visualization, we have two sections which have been outlined. We have the **predicted** classifications section which contains three subsections for each of the classes we want to classify into and the **actual** classifications section which has three subsections for each of the classes.

Having visualized this confusion matrix, we can use this visualization to calculate the following metrics:

- True Positives.
- True Negatives.
- False Positives.
- False Negatives.
- Accuracy.
- Precision.
- True Positive Rate is also known as
**Sensitivity**or**Recall**. - True Negative Rate is also known as
**Specificity**.

Before we calculate these metrics, let's define the variables in the visualization:

**PGG**

β This variable represents the number of predictions where images of a **Greyhound** were correctly classified [as a **Greyhound**]. This is also the **True Positive** for the **Greyhound** class.

**PMG**

β This variable represents the number of predictions where images of a **Greyhound** were incorrectly classified as a **Mastiff**.

**PSG**

β This variable represents the number of predictions where images of a **Greyhound** were incorrectly classified as a **Samoyed**.

**PGM**

β This variable represents the number of predictions where images of a **Mastiff** were incorrectly classified as a **Greyhound**.

**PMM**

β This variable represents the number of predictions where images of a **Mastiff** were correctly classified [as a **Mastiff**]. This is also the **True Positive** for the **Mastiff** class.

**PSM**

β This variable represents the number of predictions where images of a **Mastiff** were incorrectly classified as a **Samoyed**.

**PGS**

β This variable represents the number of predictions where images of a **Samoyed** were incorrectly classified as a **Greyhound**.

**PMS**

β This variable represents the number of predictions where images of a **Samoyed** were incorrectly classified as a **Mastiff**.

**PSS**

β This variable represents the number of predictions where images of a **Samoyed** were correctly classified [as a **Samoyed**]. This is also the **True Positive** for the **Samoyed** class.

Now that we have defined these variables, we can now calculate the aforementioned metrics.

#### True Positives

β The definition for the **True Positive** is the same as in the 2-class confusion matrix. However, here we calculate the **True Positives** for each class in the confusion matrix unlike the general or absolute **True Positives** in the 2-class confusion matrix.

The **True Positives** is the number of predictions where data labelled to belong to a particular class was correctly classified as the said class. E.g Number of predictions where images of a Samoyed was correctly classified as a Samoyed.

From the definition of the matrix variables, we have already identified the **True Positives** for each of the classes:

True Positivesfor theGreyhoundclass is the variablePGGin the confusion matrix.

True Positivesfor theMastiffclass is the variablePMMin the confusion matrix.

True Positivesfor theSamoyedclass is the variablePSSin the confusion matrix.

#### True Negatives

β The definition of the **True Negative** is the same as in the 2-class confusion matrix. Here we calculate the **True Negatives** for each class in the confusion matrix unlike the general or absolute **True Negatives** in the 2-class confusion matrix.

The **True Negatives** for a particular class is calculated by taking the sum of the values in every row and column except the row and column of the class we're trying to find the **True Negatives** for.

For example, calculating the **True Negatives** for the **Greyhound** class:

We omit the row and columns belonging to the **Greyhound** class and sum the variables that are left, which are the rows and columns of the other classes (**Mastiff** and **Samoyed**).

Therefore the **True Negatives** for the **Greyhound** class is:

β **TN** = **PMM** + **PSM** + **PMS** + **PSS**

Similarly, we can calculate the **True Negatives** for the other classes.

Calculating the **True Negatives** for the **Mastiff** class:

The **True Negatives** for the **Mastiff** class is:

β **TN** = **PGG** + **PSG** + **PGS** + **PSS**

Calculating the **True Negatives** for the **Samoyed** class:

The **True Negatives** for the **Samoyed** class is:

β **TN** = **PGG** + **PMG** + **PGM** + **PMM**

#### False Positives

β The definition of the **False Positive** is the same as in the 2-class confusion matrix. Here we calculate the **False Positives** for each class in the confusion matrix unlike the general or absolute **False Positives** in the 2-class confusion matrix.

The **False Positives** for a particular class can be calculated by taking the sum of all the values in the column corresponding to that class except the **True Positives** value.

For example, calculating the **False Positives** for the **Greyhound** class:

β We sum all the values in the highlighted area, which is the column corresponding to the **Greyhound** class with the exception of the variable **PGG** which we had earlier identified to be the **True Positives** for the **Greyhound** class.

β Therefore the **False Positives** for **Greyhound** class is:

β **FP** = **PGM** + **PGS**

Similarly, we can calculate the **False Positives** for the other classes.

Calculating the **False Positives** for the **Mastiff** class:

β The **False Positives** for the **Mastiff** class is:

β **FP** = **PMG** + **PMS**

Calculating the **False Positives** for the **Samoyed** class:

β The **False Positives** for the **Samoyed** class is:

β **FP** = **PSG** + **PSM**

#### False Negatives

β The definition of the **False Negative** is the same as in the 2-class confusion matrix. Here we calculate the **False Negatives** for each class in the confusion matrix unlike the general or absolute **False Positives** in the 2-class confusion matrix.

The **False Negatives** for a particular class can be calculated by taking the sum of all the values in the row corresponding to that class except the **True Positives** value.

For example, calculating the **False Negatives** for the **Greyhound** class:

β We sum all the values in the highlighted area, which is the row corresponding to the **Greyhound** class with the exception of the variable **PGG** which we had earlier identified to be the **True Positives** for the **Greyhound** class.

β Therefore the **False Negatives** for **Greyhound** class is:

β **FN** = **PMG** + **PSG**

Similarly, we can calculate the **False Negatives** for the other classes.

Calculating the **False Negatives** for the **Mastiff** class:

β The **False Negatives** for the **Mastiff** class is:

β **FN** = **PGM** + **PSM**

Calculating the **False Negatives** for the **Samoyed** class:

β The **False Negatives** for the **Samoyed** class is:

β **FN** = **PGS** + **PMS**

#### Accuracy

β **Accuracy** is calculated as the ratio of the number of correct classifications to the total number of classifications. From our confusion matrix, the correct classifications are the **True Positives** for each class and the total number of classifications is the sum of every value in the confusion matrix, including the **True Positives**.

β Therefore, the accuracy is:

β **AC** = (**PGG** + **PMM** + **PSS**) / (**PGG + PMG + PSG + PGM + PMM + PSM + PGS + PMS + PSS**)

#### Precision

β **Precision** is a multi-class confusion matrix is the measure of the accuracy relative to the prediction of a specific class. It is calculated as the ratio of the **True Positives** of the class in question to the sum of its **True Positives** and **False Positives**.

For example, calculating the Precision of the **Greyhound** class:

β **Precision** (**G**) = **TP** / (**TP + FP**)

β = **PGG** / (**PGG** + (**PGM + PGS**))

Similarly, we can calculate the Precision of other classes.

Calculating the Precision of the **Mastiff** class:

β **Precision (M)** = **TP / (TP + FP)**

β = **PMM** / (**PMM** + (**PMG** + **PMS**))

Calculating the Precision of the **Samoyed** class:

β **Precision (S)** = **TP** / **(TP + FP)**

β = **PSS** / (**PSS** + (**PSG** + **PSM**))

#### True Positive Rate

β The **True Positive Rate** (also known as **Recall** or **Sensitivity**) is calculated as the ratio of the **True Positives** of a specific class to the sum of its **True Positives** and **False Negatives**.

For example, calculating the **True Positive Rate** of the **Greyhound** class:

β **TPR (G)** = **TP** / (**TP + FN**)

β = **PGG** / (**PGG** + (**PMG + PSG**))

Similarly, we can calculate the True Positive Rate of other classes.

Calculating the True Positive Rate of the **Mastiff** class:

β **TPR (M)** = **TP** / (**TP + FN**)

β = **PMM** / (**PMM** + (**PGM + PSM**))

Calculating the True Positive Rate of the **Samoyed** class:

β **TPR (S)** = **TP** / (**TP + FN**)

β = **PSS** / (**PSS** + (**PGS + PMS**))

#### True Negative Rate

β The **True Negative Rate** (also known as **Specificity**) is calculated as the ratio of the **True Negatives** of a specific class to the sum of its **True Negatives** and **False Positives**.

For example, calculating the **True Negative Rate** of the **Greyhound** class:

β **TNR (G)** = **TN** / (**TN + FP**)

β = (**PMM** + **PSM** + **PMS** + **PSS**) / ((**PMM** + **PSM** + **PMS** + **PSS**) + (**PGM + PGS**))

Similarly, we can calculate the True Negative Rate of other classes.

Calculating the True Negative Rate of the **Mastiff** class:

β **TNR (M)** = **TN** / (**TN + FP**)

β = (**PGG** + **PSG** + **PGS** + **PSS**) / ((**PGG** + **PSG** + **PGS** + **PSS**) + (**PMG** + **PMS**))

Calculating the True Negative Rate of the **Samoyed** class:

β **TNR (S)** = **TN** / (**TN + FP**)

β = (**PGG** + **PMG** + **PGM** + **PMM**) / ((**PGG** + **PMG** + **PGM** + **PMM**) + (**PSG** + **PSM**))

Phewww, that was a lot. π π

### Now, a quick example

β Suppose we have the image below as the visualized confusion matrix for our classifier, we can use the information in the visualization and the metrics defined above to evaluate its performance.

From this confusion matrix, we can identify that:

- The variable
PGGand theTrue Positivesof theGreyhoundclass is250.- The variable
PMMand theTrue Positivesof theMastiffclass is320.- The variable
PSSand theTrue Positivesof theSamoyedclass is180.- The variable
PMGis25.- The variable
PSGis18.- The variable
PGMis21.- The variable
PSMis24.- The variable
PGSis22.- The variable
PMSis12.

Using this information that we've "extracted", we can calculate the metrics mentioned earlier and thus evaluate the performance of the classifier.

##### True Negatives

TN(Greyhound) =PMM + PSM + PMS + PSSβ = 320 + 24 + 24 + 180 β =548TN(Mastiff) =PGG + PSG + PGS + PSSβ = 250 + 18 + 22 + 180 β =470TN(Samoyed) =PGG + PMG + PGM + PMMβ = 250 + 25 + 21 + 320 β =616

##### False Positives

FP(Greyhound) =PGM + PGSβ = 21 + 22 β =43FP(Mastiff) =PMG + PMSβ = 25 + 12 β =37FP(Samoyed) =PSG + PSMβ = 18 + 24 β =42

##### False Negatives

FN(Greyhound) =PMG + PSGβ = 25 + 18 β =43FN(Mastiff) =PGM + PSMβ = 21 + 24 β =45FN(Samoyed) =PGS + PMSβ = 22 + 12 β =34

##### True Positive Rate / Recall / Sensitivity

TPR(Greyhound) =TP/ (TP + FN) β = 250 / (250 + 43) β = 250 / 293 β = 0.8532423208 β =0.85TPR(Mastiff) =TP/ (TP + FN) β = 320 / (320 + 45) β = 320 / 365 β = 0.8767123288 β =0.88TPR(Samoyed) =TP/ (TP + FN) β = 180 / (180 + 34) β = 180 / 214 β = 0.8411214953 β =0.81

##### True Negative Rate / Specificity

TNR(Greyhound)=TN/ (TN + FP) β = 548 / (548 + 43) β = 548 / 591 β = 0.9272419628 β =0.93TNR(Mastiff)=TN/ (TN + FP) β = 470 / (470 + 37) β = 470 / 507 β = 0.9270216963 β =0.93TNR(Samoyed)=TN/ (TN + FP) β = 616 / (616 + 42) β = 616 / 658 β = 0.9361702128 β =0.94

##### Precision

Precision(Greyhound) =TP/ (TP + FP) β = 250 / (250 + 43) β = 250 / 293 β = 0.8532423208 β =0.85

Therefore, the classifier has a precision of **0.85**, which is **85%**, in classifying images of Greyhounds.

Precision(Mastiff) =TP/ (TP + FP) β = 320 + (320 + 37) β = 320 / 357 β = 0.8963585434 β =0.90

Therefore, the classifier has a precision of **0.90**, which is **90%**, in classifying images of Mastiffs.

Precision(Samoyed) =TP/ (TP + FP) β = 180 / (180 + 42) β = 180 / 222 β = 0.8108108108 β =0.81

Therefore, the classifier has a precision of **0.81**, which is **81%**, in classifying images of Samoyeds.

##### Accuracy

β **AC** = (**PGG** + **PMM** + **PSS**) / (**PGG + PMG + PSG + PGM + PMM + PSM + PGS + PMS + PSS**)

β = (250 + 320 + 180) / (250 + 25 + 18 + 21 + 320 + 24 + 22 + 12 + 180)

= 750 / 872

β = 0.8600917431

β = **0.86**

β Therefore, the classifier has a total accuracy of **0.86** which is **86%**

### How to generate a multi-class confusion matrix in Python

β A multi-class confusion matrix is created in the same way as a 2-class confusion matrix. See the first part of this article.