machine learning
classification
model confidence
predict_proba
decision_function

predict_proba or decision_function as estimator confidence

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

predict_proba() and decision_function() are both useful, but they do not mean the same thing. If you want class probabilities, use predict_proba() when the estimator offers well-defined probabilities. If you want ranking strength or margin from the decision boundary, use decision_function().

What predict_proba() Returns

predict_proba() returns one probability per class for each sample. The values are constrained to the interval from 0 to 1, and each row sums to 1.

Example:

python
1from sklearn.linear_model import LogisticRegression
2from sklearn.datasets import make_classification
3
4X, y = make_classification(random_state=0)
5clf = LogisticRegression().fit(X, y)
6
7proba = clf.predict_proba(X[:3])
8print(proba)

A binary classifier might produce rows such as:

text
[[0.08 0.92]
 [0.73 0.27]
 [0.49 0.51]]

That is convenient when you need thresholds like "only act if class 1 probability is at least 0.9."

What decision_function() Returns

decision_function() returns a score, not a probability. The score usually measures signed distance or margin relative to the learned decision boundary.

python
scores = clf.decision_function(X[:3])
print(scores)

For binary classification:

  • Positive values lean toward the positive class
  • Negative values lean toward the negative class
  • Larger magnitude usually means the model is more certain in its own internal scale

The important caveat is that the scale is model-specific. A score of 3.0 in one estimator is not directly comparable to 3.0 in another.

Which One Is "Confidence"

That depends on what you mean by confidence.

If you mean "estimated probability of belonging to a class," then predict_proba() is the right API.

If you mean "strength of the model's preference before converting it to a probability," then decision_function() is the better fit.

For tasks such as:

  • Sorting examples from most positive to least positive
  • Drawing ROC curves
  • Comparing margins around a classifier boundary

decision_function() is often perfectly appropriate, and sometimes better.

Probabilities Are Not Always Well Calibrated

A second important caveat is that probabilities are not automatically trustworthy just because the method name says predict_proba(). Some classifiers produce poor probability calibration even when their ranking ability is strong.

If you need probabilities that reflect real-world frequencies, use calibration:

python
1from sklearn.calibration import CalibratedClassifierCV
2from sklearn.svm import LinearSVC
3
4base = LinearSVC()
5calibrated = CalibratedClassifierCV(base)
6calibrated.fit(X, y)
7
8print(calibrated.predict_proba(X[:3]))

This is especially relevant for SVM-like models, where the raw margin is often more natural than the probability estimate.

Practical Rule of Thumb

Use predict_proba() when:

  • You need probabilities for thresholds, risk estimates, or UI display
  • Your estimator supports it directly or through calibration

Use decision_function() when:

  • You care about relative ranking
  • You are building ROC or precision-recall analysis
  • The estimator naturally exposes margins rather than calibrated probabilities

Multi-Class Behavior

In multi-class problems, predict_proba() returns one probability per class, which makes it easy to explain the full class distribution. decision_function() may return one score per class or a one-vs-rest style margin depending on the estimator. That makes it useful for internal ranking logic, but less convenient when downstream code expects something that sums cleanly to 1.0.

Common Pitfalls

  • Treating decision_function() values as probabilities.
  • Assuming predict_proba() is always well calibrated.
  • Comparing raw decision scores across different model families as if they shared a common scale.
  • Forcing a probability interpretation when ranking is the real requirement.

Summary

  • 'predict_proba() gives class probabilities.'
  • 'decision_function() gives raw model scores or margins.'
  • Probabilities are easier to explain, but not always calibrated.
  • Decision scores are often better for ranking and threshold tuning.
  • Choose the API based on the downstream use, not on which one looks more "confident."

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.