Bird
Raised Fist0
ML Pythonml~5 mins

Probability calibration in ML Python - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is probability calibration in machine learning?
Probability calibration is the process of adjusting a model's predicted probabilities so they better reflect the true likelihood of an event happening. For example, if a model says 70% chance of rain, it should actually rain about 70% of those times.
Click to reveal answer
beginner
Why is probability calibration important?
It helps make predictions more trustworthy and useful. Well-calibrated probabilities allow better decision-making, like knowing when to trust a model's prediction or how to weigh risks.
Click to reveal answer
intermediate
Name two common methods for probability calibration.
Two common methods are Platt Scaling, which fits a logistic regression on the model's scores, and Isotonic Regression, which fits a flexible non-decreasing function to adjust probabilities.
Click to reveal answer
intermediate
What does a perfectly calibrated model's reliability diagram look like?
It is a diagonal line from bottom-left to top-right, meaning predicted probabilities match observed frequencies exactly. For example, predictions of 0.8 correspond to events happening 80% of the time.
Click to reveal answer
intermediate
How can you check if a model's probabilities are well calibrated?
You can use calibration plots (reliability diagrams) or metrics like the Brier score, which measures the mean squared difference between predicted probabilities and actual outcomes.
Click to reveal answer
What does probability calibration adjust in a model?
AThe model's training data size
BThe model's input features
CThe predicted probabilities to better match true event frequencies
DThe model's architecture
Which method fits a logistic regression to calibrate probabilities?
AIsotonic Regression
BRandom Forest
CK-Means Clustering
DPlatt Scaling
What shape does a reliability diagram have for a perfectly calibrated model?
ADiagonal line from bottom-left to top-right
BVertical line
CHorizontal line
DRandom scattered points
Which metric measures the mean squared difference between predicted probabilities and actual outcomes?
ABrier score
BPrecision
CRecall
DAccuracy
Why might a model with high accuracy still need probability calibration?
ABecause accuracy measures probability quality
BBecause probabilities might not reflect true likelihoods
CBecause calibration changes model architecture
DBecause calibration increases training speed
Explain in your own words what probability calibration is and why it matters.
Think about how confident a model's predictions should be to match reality.
You got /3 concepts.
    Describe two methods used for probability calibration and how they differ.
    One fits a logistic curve, the other fits a flexible shape.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is the main goal of probability calibration in machine learning?
      easy
      A. To adjust predicted probabilities to better reflect true likelihoods
      B. To increase the accuracy of class labels
      C. To reduce the size of the training dataset
      D. To speed up the training process

      Solution

      1. Step 1: Understand the purpose of probability calibration

        Probability calibration aims to make predicted probabilities match the actual chance of an event happening.
      2. 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.
      3. Final Answer:

        To adjust predicted probabilities to better reflect true likelihoods -> Option A
      4. Quick Check:

        Calibration = Adjust probabilities [OK]
      Hint: Calibration fixes probability quality, not accuracy or speed [OK]
      Common Mistakes:
      • Confusing calibration with accuracy improvement
      • Thinking calibration changes dataset size
      • Assuming calibration speeds training
      2. Which of the following is a common method used for probability calibration?
      easy
      A. K-means clustering
      B. Gradient boosting
      C. Platt scaling
      D. Principal component analysis

      Solution

      1. Step 1: Identify calibration methods

        Platt scaling is a sigmoid-based method commonly used to calibrate probabilities.
      2. 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.
      3. Final Answer:

        Platt scaling -> Option C
      4. Quick Check:

        Calibration method = Platt scaling [OK]
      Hint: Remember Platt scaling for calibration, others are different tasks [OK]
      Common Mistakes:
      • Confusing boosting with calibration
      • Mixing clustering or PCA with calibration
      • Choosing any popular ML method as calibration
      3. Given the following Python code using scikit-learn, what will be the output of 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)
      medium
      A. A 2D array with calibrated probabilities for each class, e.g. [[0.3, 0.7]]
      B. A single float value representing probability
      C. An error because cv='prefit' requires a different fit method
      D. A list of predicted class labels, e.g. [1]

      Solution

      1. 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.
      2. Step 2: Check predict_proba output format

        predict_proba returns probabilities for each class in a 2D array, not a single float or labels.
      3. Final Answer:

        A 2D array with calibrated probabilities for each class, e.g. [[0.3, 0.7]] -> Option A
      4. Quick Check:

        predict_proba output = 2D array [OK]
      Hint: predict_proba always returns 2D array of class probabilities [OK]
      Common Mistakes:
      • Expecting a single float instead of array
      • Confusing predict_proba with predict
      • Misunderstanding cv='prefit' usage
      4. You tried to calibrate a classifier using 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?
      medium
      A. You passed cv=5 but the dataset has fewer than 5 samples
      B. You passed an integer instead of a cross-validation splitter object
      C. You used an unsupported calibration method
      D. You forgot to fit the base classifier before calibration

      Solution

      1. 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.
      2. 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).
      3. Step 3: Rule out unrelated causes

        Base fitting (D) is for cv='prefit'; dataset size (B) or method (C) don't trigger this error.
      4. Final Answer:

        You passed an integer instead of a cross-validation splitter object -> Option B
      5. Quick Check:

        Error 'got int instead' = cv type mismatch [OK]
      Hint: cv requires cross-validation generator or iterable, not plain int [OK]
      Common Mistakes:
      • Passing an integer to cv instead of a splitter object
      • Confusing cv parameter usage
      • Assuming calibration method causes error
      5. You have a binary classifier that outputs probabilities but they are poorly calibrated. You want to improve calibration on a small dataset without losing model accuracy. Which approach is best?
      hard
      A. Discard probabilities and use only predicted labels
      B. Retrain the model with more epochs to improve accuracy
      C. Use isotonic regression calibration on a separate validation set
      D. Apply Platt scaling calibration using cross-validation

      Solution

      1. 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.
      2. 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.
      3. Step 3: Evaluate other options

        Isotonic regression may overfit small data, retraining may not fix calibration, discarding probabilities loses useful info.
      4. Final Answer:

        Apply Platt scaling calibration using cross-validation -> Option D
      5. Quick Check:

        Small data calibration = Platt scaling + CV [OK]
      Hint: For small data, prefer Platt scaling with CV for calibration [OK]
      Common Mistakes:
      • Using isotonic regression on small data causing overfit
      • Retraining model instead of calibrating
      • Ignoring probability calibration importance