Probability calibration checks if the model's predicted chances match real outcomes. For example, if a model says 70% chance of rain, it should rain about 7 times out of 10 when it says that. The key metrics are Calibration Curve and Brier Score. The calibration curve shows how predicted probabilities compare to actual results. The Brier score measures the average squared difference between predicted probabilities and actual outcomes. Lower Brier scores mean better calibration.
Probability calibration in ML Python - Model Metrics & Evaluation
Start learning this pattern below
Jump into concepts and practice - no test required
Probability calibration is about probabilities, not just yes/no predictions, so confusion matrix alone is not enough. Instead, we use a Calibration Curve which groups predictions by probability bins and compares predicted vs actual frequency.
Probability Bin | Predicted Probability | Actual Frequency
---------------------------------------------------------
0.0 - 0.1 | 0.08 | 0.07
0.1 - 0.2 | 0.15 | 0.18
0.2 - 0.3 | 0.25 | 0.22
0.3 - 0.4 | 0.35 | 0.33
0.4 - 0.5 | 0.45 | 0.48
0.5 - 0.6 | 0.55 | 0.52
0.6 - 0.7 | 0.65 | 0.68
0.7 - 0.8 | 0.75 | 0.73
0.8 - 0.9 | 0.85 | 0.88
0.9 - 1.0 | 0.95 | 0.94
This table shows predicted probabilities and how often the event actually happened in that range. Good calibration means these numbers are close.
Precision and recall focus on classification decisions (yes/no), but calibration focuses on how well predicted probabilities match reality. A model can have high precision and recall but poor calibration if its probabilities are too confident or too low. For example, a spam filter might catch spam well (high recall) but if it always says 99% spam chance even when unsure, it is poorly calibrated. Calibration helps trust the probability values, which is important for decisions like medical diagnosis or weather forecasts.
Good calibration: Predicted probabilities closely match actual outcomes. For example, when the model predicts 0.7 chance, the event happens about 70% of the time. The calibration curve is close to the diagonal line. Brier score is low (closer to 0).
Bad calibration: Predicted probabilities are too high or too low compared to actual outcomes. For example, the model predicts 0.9 chance but the event happens only 50% of the time. The calibration curve deviates far from the diagonal. Brier score is higher.
- Ignoring calibration: Using raw probabilities without checking calibration can mislead decisions.
- Small sample sizes: Calibration curves can be noisy if there are few samples in probability bins.
- Overfitting calibration: Adjusting probabilities too much on training data can hurt performance on new data.
- Confusing accuracy with calibration: A model can be accurate in classification but poorly calibrated in probabilities.
- Data leakage: If calibration is done on data used for training, it gives overly optimistic results.
Not necessarily. High accuracy means the model predicts the right class often, but if the predicted probabilities are not reliable, decisions based on those probabilities can be wrong. For example, if a medical test says 99% chance of disease but the real chance is only 50%, doctors might overreact. So, good calibration is important when you need trustworthy probability estimates, even if accuracy is high.
Practice
probability calibration in machine learning?Solution
Step 1: Understand the purpose of probability calibration
Probability calibration aims to make predicted probabilities match the actual chance of an event happening.Step 2: Differentiate from accuracy and training speed
Accuracy relates to correct labels, not probability quality. Calibration focuses on probability quality, not dataset size or speed.Final Answer:
To adjust predicted probabilities to better reflect true likelihoods -> Option AQuick Check:
Calibration = Adjust probabilities [OK]
- Confusing calibration with accuracy improvement
- Thinking calibration changes dataset size
- Assuming calibration speeds training
Solution
Step 1: Identify calibration methods
Platt scaling is a sigmoid-based method commonly used to calibrate probabilities.Step 2: Exclude unrelated methods
Gradient boosting is a model training technique, K-means is clustering, and PCA is dimensionality reduction, none are calibration methods.Final Answer:
Platt scaling -> Option CQuick Check:
Calibration method = Platt scaling [OK]
- Confusing boosting with calibration
- Mixing clustering or PCA with calibration
- Choosing any popular ML method as calibration
calibrated_clf.predict_proba([[0.5, 1.5]])?
from sklearn.datasets import make_classification from sklearn.linear_model import LogisticRegression from sklearn.calibration import CalibratedClassifierCV X, y = make_classification(n_samples=100, n_features=2, random_state=42) clf = LogisticRegression().fit(X, y) calibrated_clf = CalibratedClassifierCV(clf, method='sigmoid', cv='prefit') calibrated_clf.fit(X, y) probs = calibrated_clf.predict_proba([[0.5, 1.5]]) print(probs)
Solution
Step 1: Understand CalibratedClassifierCV output
Using method='sigmoid' with cv='prefit' fits calibration on the existing model and outputs probabilities as a 2D array for each class.Step 2: Check predict_proba output format
predict_proba returns probabilities for each class in a 2D array, not a single float or labels.Final Answer:
A 2D array with calibrated probabilities for each class, e.g. [[0.3, 0.7]] -> Option AQuick Check:
predict_proba output = 2D array [OK]
- Expecting a single float instead of array
- Confusing predict_proba with predict
- Misunderstanding cv='prefit' usage
CalibratedClassifierCV with cv=5, but got an error: "ValueError: Expected cv split to be a cross-validation generator or an iterable, got int instead." What is the likely cause?Solution
Step 1: Analyze the error message
The error "Expected cv split to be a cross-validation generator or an iterable, got int instead." directly points to the cv parameter receiving an integer (5) where a splitter was expected.Step 2: Check CalibratedClassifierCV cv usage
This occurs when cv is passed as int but the context requires an explicit cross-validation object like StratifiedKFold(5).Step 3: Rule out unrelated causes
Base fitting (D) is for cv='prefit'; dataset size (B) or method (C) don't trigger this error.Final Answer:
You passed an integer instead of a cross-validation splitter object -> Option BQuick Check:
Error 'got int instead' = cv type mismatch [OK]
- Passing an integer to cv instead of a splitter object
- Confusing cv parameter usage
- Assuming calibration method causes error
Solution
Step 1: Consider calibration methods for small datasets
Platt scaling (sigmoid) is preferred for small datasets because it is less prone to overfitting than isotonic regression.Step 2: Use cross-validation to avoid losing accuracy
Applying Platt scaling with cross-validation calibrates probabilities without retraining the base model or losing accuracy.Step 3: Evaluate other options
Isotonic regression may overfit small data, retraining may not fix calibration, discarding probabilities loses useful info.Final Answer:
Apply Platt scaling calibration using cross-validation -> Option DQuick Check:
Small data calibration = Platt scaling + CV [OK]
- Using isotonic regression on small data causing overfit
- Retraining model instead of calibrating
- Ignoring probability calibration importance
