multiclass classification
precision recall accuracy
f1-score
scikit learn
machine learning metrics

How to compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In machine learning, evaluating the performance of models is crucial for understanding how well they generalize to new data. For classification problems, this evaluation often involves calculating metrics such as precision, recall, accuracy, and F1-score. These metrics are particularly significant in multiclass classification scenarios, where the task is to assign one label from multiple possible categories to each sample. Using Python's Scikit-learn library, one can efficiently compute these metrics for the multiclass setting. This article elaborates on how to compute each metric using Scikit-learn, explaining both the concepts and code implementation.

Technical Explanation

Understanding Precision, Recall, Accuracy, and F1-Score

  • Precision: Precision is the number of true positive results divided by the number of all positive predictions. In a multiclass context, precision can be averaged in different ways, explained under "Averaging Techniques." Mathematically, Precision = True Positives / (True Positives + False Positives).
  • Recall: Also known as sensitivity or true positive rate, recall is the number of true positive results divided by the number of positives that should have been retrieved: Recall = True Positives / (True Positives + False Negatives).
  • Accuracy: The ratio of correctly predicted instances to total instances. It can be misleading in imbalanced classes. The formula is Accuracy = (True Positives + True Negatives) / Total Samples.
  • F1-Score: The harmonic mean of precision and recall, providing a balance between the two: F1-Score = 2 × (Precision × Recall) / (Precision + Recall).

Averaging Techniques

In multilabel or multiclass classification, you might need to average these metrics across classes:

  • Macro-Averaging: Calculate the metric independently for each class and then take the average. This treats all classes equally.
  • Micro-Averaging: Accumulate the true positives, false negatives, and false positives for each class, and then calculate the metric. This is useful if one wants to account for class imbalance.
  • Weighted-Averaging: Similar to macro-averaging but each class's contribution is weighted by its size.

Implementation with Scikit-learn

Let's move to practical implementation with a sample dataset and code to compute these metrics using Scikit-learn.

Step-by-Step Implementation

  1. Import Necessary Libraries
python
1   import numpy as np
2   from sklearn.datasets import make_classification
3   from sklearn.model_selection import train_test_split
4   from sklearn.ensemble import RandomForestClassifier
5   from sklearn.metrics import precision_score, recall_score, accuracy_score, f1_score
  1. Create a Dataset
python
   # Creating a synthetic dataset
   X, y = make_classification(n_samples=1000, n_classes=3, n_informative=4, n_features=20, random_state=0)
  1. Split the Dataset
python
   X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
  1. Train a Classifier
python
   classifier = RandomForestClassifier()
   classifier.fit(X_train, y_train)
   y_pred = classifier.predict(X_test)
  1. Calculate Metrics
python
1   # Precision
2   precision_macro = precision_score(y_test, y_pred, average='macro')
3   precision_micro = precision_score(y_test, y_pred, average='micro')
4   precision_weighted = precision_score(y_test, y_pred, average='weighted')
5
6   # Recall
7   recall_macro = recall_score(y_test, y_pred, average='macro')
8   recall_micro = recall_score(y_test, y_pred, average='micro')
9   recall_weighted = recall_score(y_test, y_pred, average='weighted')
10
11   # Accuracy
12   accuracy = accuracy_score(y_test, y_pred)
13
14   # F1-Score
15   f1_macro = f1_score(y_test, y_pred, average='macro')
16   f1_micro = f1_score(y_test, y_pred, average='micro')
17   f1_weighted = f1_score(y_test, y_pred, average='weighted')
  1. Output Results
python
1   print("Precision (Macro):", precision_macro)
2   print("Precision (Micro):", precision_micro)
3   print("Precision (Weighted):", precision_weighted)
4
5   print("Recall (Macro):", recall_macro)
6   print("Recall (Micro):", recall_micro)
7   print("Recall (Weighted):", recall_weighted)
8
9   print("Accuracy:", accuracy)
10
11   print("F1-score (Macro):", f1_macro)
12   print("F1-score (Micro):", f1_micro)
13   print("F1-score (Weighted):", f1_weighted)

Summary Table

MetricMacro (Treats all classes equally)Micro (Global view considering all classes)Weighted (Considers class distribution)
PrecisionPrecision MacroPrecision MicroPrecision Weighted
RecallRecall MacroRecall MicroRecall Weighted
AccuracyNot Applicable (It's global)AccuracyNot Applicable
F1-ScoreF1 MacroF1 MicroF1 Weighted

Additional Considerations

  • Confusion Matrix: It's often helpful to visualize a confusion matrix to see how well the model is performing concerning each class.
python
1  from sklearn.metrics import confusion_matrix
2  import seaborn as sns
3  import matplotlib.pyplot as plt
4
5  cm = confusion_matrix(y_test, y_pred)
6  sns.heatmap(cm, annot=True)
7  plt.title('Confusion Matrix')
8  plt.show()
  • Imbalanced Data: For datasets with a significant class imbalance, micro-averaging might be more appropriate as it gives equal importance to each sample.
  • Model Selection: It's essential to choose a model that performs well on the given distribution of classes. Hyperparameter tuning can significantly impact performance in multiclass scenarios.

By understanding these metrics and their computation techniques, you can better assess the performance of your multiclass classification models, ensuring they meet your project's expectations. Using Scikit-learn simplifies this task, offering robust functions for model evaluation.


Course illustration
Course illustration

All Rights Reserved.