Evaluation Metrics For Classification Model in Python
Last Updated :
23 Jul, 2025
Classification is a supervised machine-learning technique that predicts the class label based on the input data. There are different classification algorithms to build a classification model, such as Stochastic Gradient Classifier, Support Vector Machine Classifier, Random Forest Classifier, etc. To choose the right model, it is important to gauge the performance of each classification algorithm.
This tutorial will look at different evaluation metrics to check the model's performance and explore which metrics to choose based on the situation.
Understanding Classification Evaluation Metrics
Understanding classification evaluation metrics is crucial for assessing the performance of machine learning models, especially in tasks like binary or multiclass classification. Some common metrics are:
Let's consider the MNIST dataset and try to understand the metrics based on the classifier. MNIST has a set of 70,000 small, handwritten-digit images. Let's go through the dataset before we start.
Python
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score, f1_score, roc_auc_score, roc_curve
import matplotlib.pyplot as plt
import seaborn as sns
(train_X, train_y), (test_X, test_y) = mnist.load_data()
train_X = train_X.reshape((train_X.shape[0], 28, 28, 1)).astype('float32') / 255
test_X = test_X.reshape((test_X.shape[0], 28, 28, 1)).astype('float32') / 255
train_y = to_categorical(train_y)
test_y = to_categorical(test_y)
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Flatten(),
Dense(100, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_X, train_y, epochs=3, batch_size=200, validation_split=0.2, verbose=2)
y_pred = model.predict(test_X)
y_pred_classes = y_pred.argmax(axis=1)
y_test_classes = test_y.argmax(axis=1)
Output:
Epoch 1/3
240/240 - 24s - loss: 0.3217 - accuracy: 0.9088 - val_loss: 0.1426 - val_accuracy: 0.9573 - 24s/epoch - 98ms/step
Epoch 2/3
240/240 - 18s - loss: 0.1022 - accuracy: 0.9707 - val_loss: 0.0805 - val_accuracy: 0.9770 - 18s/epoch - 76ms/step
Epoch 3/3
240/240 - 21s - loss: 0.0667 - accuracy: 0.9808 - val_loss: 0.0659 - val_accuracy: 0.9815 - 21s/epoch - 89ms/step
313/313 [==============================] - 2s 6ms/step
1. Accuracy
Classification accuracy is the simplest evaluation metric. It is defined as the number of correct predictions divided by the total number of predictions multiplied by 100. The accuracy metric works great if the target variable classes in the data are approximately balanced. For example, if 60% of the classes in an animal dataset are dogs and 40% are cats, then we can say that it is a balanced dataset. It calculates the ratio of correctly predicted instances to the total instances. It's calculated as:
Accuracy= \frac{\text Total Number of Predictions}
{\text Number of Correct Predictions}
In the context of the MNIST dataset, accuracy measures how often the model correctly identifies the handwritten digits.
Python
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test_classes, y_pred_classes)
print(f'Accuracy: {accuracy:.4f}')
Output:
Accuracy: 0.9822
2. Confusion Matrix
The confusion matrix is another way to evaluate the performance of a classifier. Here, it counts the number of times instances of class A are classified as class B. For example, the number of times the classifier confused images of 5s with non-5s.
This is a table that is often used to describe the performance of a classification model. It presents a summary of the predictions made by the model against the actual class labels. The confusion matrix is a matrix with four different combinations of predicted and actual classes: True Positive (TP), True Negative (TN), False Positive (FP), and False Negative (FN).
Let's compute the confusion matrix to evaluate the performance of a classifier. We can make use of MNIST dataset to compute the confusion matrix. The stepsare as follows:
Python
cm = confusion_matrix(y_test_classes, y_pred_classes)
plt.figure(figsize=(10, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
Output:
Confusion Matrix
3. Precision, Recall and F1 Score
A confusion matrix is a great way to evaluate the performance of a classifier, but sometimes we may need a more concise metric. Here comes the importance of precision.
3.1 Precision: Precision provides the accuracy of the positive prediction made by the classifier. The equation is as follows:
Precision = True Positive / (True Positive + False Positive)
When to choose precision?
In some cases, we need high precision. For example, consider that we trained a classifier to detect videos that are safe for kids. Here, we prefer a classifier that keeps only the safe one (high precision), irrespective of whether the classifier rejects many good videos (low recall).
Precision is typically used with another metric called recall (sensitivity, or the true positive rate ━ TPR).
3.2 Recall: Recall is the ratio of number of true positive predictions (correctly detected by the classifer) to the total number of actual positive instances in the dataset. It measures the completeness of positive predictions. The equation is as follows:
Recall = True Positve / (True Positive + False Negative)
When to choose recall?
In some cases, high recall is given importance instead of high precision. Suppose you train a classifier for fire detection with high precision; certain actual cases were not considered. So it is important to maintain a high recall. Here, security guards will get a few false alarms, but they will be alarmed in almost every actual case.
3.3 F1 Score: The F1 score is the harmonic mean of precision and recall. It favors classifiers that have similar precision and recall. Here, the classifier will only get a high F1 score if both recall and precision are high. The equation is as follows:
F1 = 2 * (Precision * Recall) / (Precision + Recall)
When to choose F1 Score?
F1 Score is invaluable in binary classification tasks, especially with imbalanced datasets, where accuracy can be misleading. It strikes a balance between precision and recall, crucial in scenarios where both are equally important, like medical diagnosis. This metric effectively captures the trade-off between precision and recall, offering a comprehensive evaluation of model performance.
For implementation refer to code below:
Python
precision = precision_score(y_test_classes, y_pred_classes, average='macro')
recall = recall_score(y_test_classes, y_pred_classes, average='macro')
f1 = f1_score(y_test_classes, y_pred_classes, average='macro')
print(f'Precision: {precision:.4f}')
print(f'Recall: {recall:.4f}')
print(f'F1 Score: {f1:.4f}')
Output:
Precision: 0.9823
Recall: 0.9821
F1 Score: 0.9822
In the above code, we make use of the f1_score() method from the sklearn metric to calculate the F1 score.
4. ROC Curve
The Receiver Operating Characteristic (ROC) curve is a graphical representation of the performance of a classification model at various thresholds. It plots the True Positive Rate (TPR) against the False Positive Rate (FPR). The Area Under the ROC Curve (AUC-ROC) is a metric to evaluate the performance of a binary classification model. AUC-ROC value lies between 0 and 1, where a higher value indicates better performance. AUC-ROC is insensitive to class distribution and gives an aggregate measure of performance across all possible classification thresholds.
The true positive rate is calculated as:
TPR = True Positives / (True Positives + False Negatives)
It defines how good the model is at predicting the positive class for a positive outcome. It is also known as sensitivity.
The false positive rate is calculated as:
FPR = False Positives / (False Positives + True Negatives)
It is also referred to as inverted specificity (1 - specificity), where specifity is calculated as:
Specificity = True Negative / (True Negaive + False Positive)
Let's get to the implementation part using Sklearn.The code is as follows:
Python
# Assuming y_test and y_pred_prob are the true labels and predicted probabilities respectively
y_pred_prob = model.predict(test_X)
roc_auc = roc_auc_score(test_y, y_pred_prob, multi_class='ovr')
print(f'ROC AUC: {roc_auc:.4f}')
# Plotting ROC Curve for one class (e.g., class 0)
fpr, tpr, _ = roc_curve(y_test_classes == 0, y_pred_prob[:, 0])
plt.plot(fpr, tpr, label='Class 0 ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()
Output:
ROC AUC: 0.9998
ROC-AUC Curve
Conclusion
It is important to gain insights on how well a machine learning algorithm performs on unseen data. Choosing the right evaluation metrics will help identify the right ML algorithm that performs well. Here, we have gone through different evaluation metrics and also discussed how to choose the right evaluation metrics for classification.
Similar Reads
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Do you
5 min read
Introduction to Machine Learning
Python for Machine Learning
Machine Learning with Python TutorialPython language is widely used in Machine Learning because it provides libraries like NumPy, Pandas, Scikit-learn, TensorFlow, and Keras. These libraries offer tools and functions essential for data manipulation, analysis, and building machine learning models. It is well-known for its readability an
5 min read
Pandas TutorialPandas is an open-source software library designed for data manipulation and analysis. It provides data structures like series and DataFrames to easily clean, transform and analyze large datasets and integrates with other Python libraries, such as NumPy and Matplotlib. It offers functions for data t
6 min read
NumPy Tutorial - Python LibraryNumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Scikit Learn TutorialScikit-learn (also known as sklearn) is a widely-used open-source Python library for machine learning. It builds on other scientific libraries like NumPy, SciPy and Matplotlib to provide efficient tools for predictive data analysis and data mining.It offers a consistent and simple interface for a ra
3 min read
ML | Data Preprocessing in PythonData preprocessing is a important step in the data science transforming raw data into a clean structured format for analysis. It involves tasks like handling missing values, normalizing data and encoding variables. Mastering preprocessing in Python ensures reliable insights for accurate predictions
6 min read
EDA - Exploratory Data Analysis in PythonExploratory Data Analysis (EDA) is a important step in data analysis which focuses on understanding patterns, trends and relationships through statistical tools and visualizations. Python offers various libraries like pandas, numPy, matplotlib, seaborn and plotly which enables effective exploration
6 min read
Feature Engineering
Supervised Learning
Unsupervised Learning
Model Evaluation and Tuning
Advance Machine Learning Technique
Machine Learning Practice