Creating a probability calibrated classifier involves training a model that can predict the probability of an event occurring, rather than just predicting the event itself. This is useful when you want to make decisions based on the probability of the event occurring, rather than just whether it will occur or not. Calibration ensures that the predicted probabilities match the true probabilities.
Here's a simple example of how you might do this using Python and scikit-learn. This example uses logistic regression, but you can use any classifier that outputs probabilities:
python
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.calibration import calibration_curve, CalibratedClassifierCV
import numpy as np
import matplotlib.pyplot as plt
# Create a binary classification problem
X, y = make_classification(n_samples=100000, random_state=0, shuffle=False)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Train a logistic regression model
lr = LogisticRegression(solver='lbfgs', max_iter=10000)
lr.fit(X_train, y_train)
# Calculate predicted probabilities
lr_probs = lr.predict_proba(X_test)[:, 1]
# Calibration
fraction_of_positives, mean_predicted_value = calibration_curve(y_test, lr_probs, n_bins=10)
# Plot the calibration curve
plt.plot([0, 1], [0, 1], "k:", label="Perfectly calibrated")
plt.plot(mean_predicted_value, fraction_of_positives, "s-", label="Logistic regression")
plt.xlabel("Mean predicted value")
plt.ylabel("Fraction of positives")
plt.legend()
plt.show()
# Ensemble model
ensemble_clf = CalibratedClassifierCV(lr, method="sigmoid", cv="prefit")
ensemble_clf.fit(X_train, y_train)
# Check ensemble model performance
ensemble_probs = ensemble_clf.predict_proba(X_test)[:, 1]
# Check calibration of the ensemble model
ensemble_fraction_of_positives, ensemble_mean_predicted_value = calibration_curve(y_test, ensemble_probs, n_bins=10)
plt.plot([0, 1], [0, 1], "k:", label="Perfectly calibrated")
plt.plot(ensemble_mean_predicted_value, ensemble_fraction_of_positives, "s-", label="Ensemble classifier")
plt.xlabel("Mean predicted value")
plt.ylabel("Fraction of positives")
plt.legend()
plt.show()
This script first creates a binary classification problem using scikit-learn's make_classification function. It then splits the data into training and testing sets. It trains a logistic regression model on the training data and calculates its predicted probabilities on the testing data.
Calibration is then performed using the calibration_curve function, and the results are plotted to show how well the model is calibrated. The ensemble model is then created using CalibratedClassifierCV, which combines the predictions of multiple models to improve performance and calibration.
The ensemble model is then evaluated in the same way as the logistic regression model. The results are plotted to show how well the ensemble model is calibrated compared to the logistic regression model.