![]() |
Multiclass classification is a common problem in machine learning where a model is required to predict one of several predefined categories. Evaluating the performance of such models can be complex, especially when dealing with imbalanced datasets. Two essential metrics for evaluating multiclass classification models are precision and recall. In this article, we will delve into the details of how to calculate precision and recall using a confusion matrix for multiclass classification. Table of Content Understanding the Confusion MatrixA confusion matrix is a table used to describe the performance of a classification model. It compares the actual target values with those predicted by the model. For a multiclass classification problem, the confusion matrix is a square matrix where the number of rows and columns equals the number of classes. Each cell in the matrix represents the count of instances for a specific combination of actual and predicted classes. The diagonal elements represent the counts of correctly classified instances (True Positives), while off-diagonal elements represent misclassifications. Calculating Precision and RecallPrecision and recall are calculated for each class individually. Let’s define these metrics:
FormulasPrecision: [Tex]= \frac{TP}{FP+ TP} [/Tex] Recall : [Tex]= \frac{TP}{FN+ TP}[/Tex] Where:
Example: Calculating Precision and Recall from a Confusion MatrixLet’s consider a multiclass classification problem with four classes. The confusion matrix is as follows:
To calculate the precision and recall for each class, we need to compute the true positives, false positives, and false negatives for each class. Class 1
[Tex]Precision_1 = \frac{TP_1}{FP_1+TP_1} = \frac{100}{35+100} =0.74[/Tex] [Tex]Recall_1 = \frac{TP_1}{FN_1+ TP_1}= \frac{100}{30+100} =0.77[/Tex] Class 2
[Tex]Precision_2=\frac{TP_2}{FP_2+TP_2}= \frac{80}{30+80} =0.73[/Tex] [Tex]Recall_2= \frac{TP_2}{FN_2+ TP_2}= \frac{80}{35+80} =0.69[/Tex] Class 3
[Tex]Precision_3= \frac{TP_ 3}{FP_3+ TP_3}= \frac{90}{20+90} =0.82[/Tex] [Tex]Recall_3= \frac{TP_3}{FN_3+TP_3} = \frac{90}{20+90} =0.82[/Tex] Class 4
[Tex]Precision_4= \frac{TP_4}{FP_4+ TP_4}= \frac{80}{20+80} =0.80[/Tex] [Tex]Recall_4= \frac{TP_4}{FN_4+TP_4}= \frac{80}{20 +80} =0.80[/Tex] Different Methods for Calculating Precision and Recall1. Micro-AveragingMicro-averaging is a method to calculate precision and recall across all classes. It involves summing up the true positives, false positives, and false negatives across all classes and then calculating the precision and recall using these total counts.
2. Macro-AveragingMacro-averaging calculates the precision and recall for each class individually and then takes the arithmetic mean across all classes. This method gives equal weight to each class, regardless of the number of instances.
3. Weighted AveragingWeighted averaging is similar to macro-averaging but assigns different weights to each class based on the number of instances in that class.
where [Tex]n_i[/Tex] is the number of instances in class i and n is the total number of instances. Calculating Precision and Recall with Python ImplementationIn practice, you can use libraries like scikit-learn in Python to calculate these metrics efficiently. Here is a sample code snippet:
Output: Confusion Matrix: [[2 1 0] [0 2 1] [1 0 3]] Precision per class: [0.66666667 0.66666667 0.75 ] Recall per class: [0.66666667 0.66666667 0.75 ] Macro Precision: 0.6944444444444443 Macro Recall: 0.6944444444444443 Micro Precision: 0.7 Micro Recall: 0.7 ConclusionPrecision and recall are essential metrics for evaluating the performance of a multiclass classification model. By using a confusion matrix, you can calculate these metrics for each class and then aggregate them using macro or micro averaging to get a comprehensive view of your model’s performance. Understanding and applying these metrics will help you fine-tune your model and improve its accuracy in real-world applications. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |