Probability Calibration Curve in Scikit Learn
Last Updated :
28 Apr, 2025
Probability Calibration is a technique used to convert the output scores from a binary classifier into probabilities to correlate with the actual probabilities of the target class. In this article, we will discuss probability calibration curves and how to plot them using Scikit-learn.
Probability Calibration
A probability calibration curve is a plot between the predicted probabilities and the actual observed frequency of the positive class of a binary classification problem. It is used to check the calibration of a classifier, i.e., how closely the predicted probabilities match the actual probabilities. A perfectly calibrated classifier will have a calibration curve that follows the 45-degree line, which means the predicted probabilities match the actual probabilities.
It is the process of modifying a binary classification model's predicted probability in order to correlate with the actual probabilities of the target class. The purpose of probability calibration is to ensure that the predicted probabilities are well-calibrated i.e., that a predicted probability of, say, 0.8 corresponds to an actual likelihood of the positive class being true of roughly 0.8. To put it another way, predicted probabilities have to be accurate projections of the actual possibilities.
In many classification tasks, the output of the model is a probability score between 0 and 1 indicating the confidence of the model that a given sample belongs to a particular class. However, these predicted probabilities may not always match the true probabilities of the classes, especially when the model is trained on imbalanced data or has a complex decision boundary.
Key Terminologies:
- Binary Classifier: A classifier that is trained to distinguish between two classes or categories, typically labeled as 0 and 1. It is commonly used in a variety of applications, including spam filtering, fraud detection, etc.
- Predicted Probabilities: These are the probabilities predicted by a classification model from the given set of input features.
- True Probabilities: These are the actual probabilities for the underlying classes.
- Calibration curve: This is the graph between the predicted probabilities and True Probabilities. It represents the calibration of the classification model and signifies where the model is overconfident or underconfident.
- Calibration curve: It is the difference between the predicted probabilities and True Probabilities.
- Reliability diagram: When the predicted probabilities are binned into discrete intervals, and the mean predicted probability and the true frequency of the positive class are plotted for each interval, it is known as the reliability diagram.
- Brier score: The Brier score is the mean squared error between the predicted probabilities and the true probabilities, and it ranges from 0 (perfect calibration) to 1 (worst calibration).
Scikit-learn provides two methods to plot probability calibration curves:
- CalibratedClassifierCV - It is a meta-estimator that trains a classifier on a dataset and calibrates the predicted probabilities using cross-validation.
- calibration_curve - It is a function that computes the true positive rate and the predicted positive rate for a given set of predicted probabilities.
Now, let's see how to plot a probability calibration curve using Scikit-learn.
Example:
In this example, we will use the Breast Cancer Wisconsin (Diagnostic) dataset from Scikit-learn and train a logistic regression classifier to predict if a tumor is malignant or benign.
python code
Python3
# Import necessary libraries
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import brier_score_loss
# Load the Breast Cancer Wisconsin (Diagnostic) dataset
data = load_breast_cancer()
X = data.data
y = data.target
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
random_state=23)
# Train a logistic regression classifier
clf = LogisticRegression(max_iter=1000, random_state=23)
clf.fit(X_train, y_train)
# Probability estimate
prob_pos = clf.predict_proba(X_test)[:, 1]
# Brier Score
b_score = brier_score_loss(y_test, prob_pos)
print("Brier Score :",b_score)
# True and Predicted Probabilities
true_pos, pred_pos = calibration_curve(y_test, prob_pos, n_bins=10)
#Plot the Probabilities Calibrated curve
plt.plot(pred_pos,
true_pos,
marker='o',
linewidth=1,
label='Logistic Regression')
#Plot the Perfectly Calibrated by Adding the 45-degree line to the plot
plt.plot([0, 1],
[0, 1],
linestyle='--',
label='Perfectly Calibrated')
# Set the title and axis labels for the plot
plt.title('Probability Calibration Curve')
plt.xlabel('Predicted Probability')
plt.ylabel('True Probability')
# Add a legend to the plot
plt.legend(loc='best')
# Show the plot
plt.show()
Output:
Brier Score : 0.022772327739296966
Probability Calibration Curve
In this example, we first loaded the Breast Cancer Wisconsin (Diagnostic) dataset from Scikit-learn and split it into training and testing sets. We then trained a logistic regression classifier on the training set and predicted the probabilities of the positive class (i.e., malignant tumor) for the testing set.
We then used the calibration_curve function from Scikit-learn to compute the true positive rate and the predicted positive rate for a given set of predicted probabilities. We plotted these rates using the plot function from Matplotlib and added the 45-degree line to the plot to represent a perfectly calibrated classifier.
Finally, we added a title, axis labels, and a legend to the plot and displayed it using the show function from Matplotlib. The resulting plot shows the probability calibration curve for the logistic regression classifier.
Conclusion:
In this article, we discussed probability calibration curves and how to plot them using Scikit-learn. Probability calibration is an important technique to ensure that the predicted probabilities from a binary classifier are accurate and reliable. Probability calibration curves are useful to visually inspect the calibration of a classifier and to compare the calibration of different classifiers.
We showed an example of how to plot a probability calibration curve using Scikit-learn's calibration_curve function and Matplotlib. In this example, we trained a logistic regression classifier on the Breast Cancer Wisconsin (Diagnostic) dataset and plotted its probability calibration curve.
Probability calibration curves are just one tool in the toolbox of binary classifier evaluation. They can be used in combination with other evaluation metrics such as precision, recall, and ROC curves to assess the performance of a classifier and to tune its parameters.
It is important to keep in mind that probability calibration is not always necessary or beneficial for all applications of binary classification. It depends on the specific requirements of the problem at hand, and it is always a good practice to evaluate the calibration of a classifier before using it for important decisions.
Overall, probability calibration curves are a powerful tool for binary classifier evaluation, and Scikit-learn provides an easy and efficient way to plot them.
Similar Reads
Machine Learning Algorithms Machine learning algorithms are essentially sets of instructions that allow computers to learn from data, make predictions, and improve their performance over time without being explicitly programmed. Machine learning algorithms are broadly categorized into three types: Supervised Learning: Algorith
8 min read
Top 15 Machine Learning Algorithms Every Data Scientist Should Know in 2025 Machine Learning (ML) Algorithms are the backbone of everything from Netflix recommendations to fraud detection in financial institutions. These algorithms form the core of intelligent systems, empowering organizations to analyze patterns, predict outcomes, and automate decision-making processes. Wi
14 min read
Linear Model Regression
Ordinary Least Squares (OLS) using statsmodelsOrdinary Least Squares (OLS) is a widely used statistical method for estimating the parameters of a linear regression model. It minimizes the sum of squared residuals between observed and predicted values. In this article we will learn how to implement Ordinary Least Squares (OLS) regression using P
3 min read
Linear Regression (Python Implementation)Linear regression is a statistical method that is used to predict a continuous dependent variable i.e target variable based on one or more independent variables. This technique assumes a linear relationship between the dependent and independent variables which means the dependent variable changes pr
14 min read
Multiple Linear Regression using Python - MLLinear regression is a statistical method used for predictive analysis. It models the relationship between a dependent variable and a single independent variable by fitting a linear equation to the data. Multiple Linear Regression extends this concept by modelling the relationship between a dependen
4 min read
Polynomial Regression ( From Scratch using Python )Prerequisites Linear RegressionGradient DescentIntroductionLinear Regression finds the correlation between the dependent variable ( or target variable ) and independent variables ( or features ). In short, it is a linear model to fit the data linearly. But it fails to fit and catch the pattern in no
5 min read
Bayesian Linear RegressionLinear regression is based on the assumption that the underlying data is normally distributed and that all relevant predictor variables have a linear relationship with the outcome. But In the real world, this is not always possible, it will follows these assumptions, Bayesian regression could be the
10 min read
How to Perform Quantile Regression in PythonIn this article, we are going to see how to perform quantile regression in Python. Linear regression is defined as the statistical method that constructs a relationship between a dependent variable and an independent variable as per the given set of variables. While performing linear regression we a
4 min read
Isotonic Regression in Scikit LearnIsotonic regression is a regression technique in which the predictor variable is monotonically related to the target variable. This means that as the value of the predictor variable increases, the value of the target variable either increases or decreases in a consistent, non-oscillating manner. Mat
6 min read
Stepwise Regression in PythonStepwise regression is a method of fitting a regression model by iteratively adding or removing variables. It is used to build a model that is accurate and parsimonious, meaning that it has the smallest number of variables that can explain the data. There are two main types of stepwise regression: F
6 min read
Least Angle Regression (LARS)Regression is a supervised machine learning task that can predict continuous values (real numbers), as compared to classification, that can predict categorical or discrete values. Before we begin, if you are a beginner, I highly recommend this article. Least Angle Regression (LARS) is an algorithm u
3 min read
Linear Model Classification
Regularization
K-Nearest Neighbors (KNN)
Support Vector Machines
ML - Stochastic Gradient Descent (SGD) Stochastic Gradient Descent (SGD) is an optimization algorithm in machine learning, particularly when dealing with large datasets. It is a variant of the traditional gradient descent algorithm but offers several advantages in terms of efficiency and scalability, making it the go-to method for many d
8 min read
Decision Tree
Ensemble Learning