Skip to main content

How to Get Class Probabilities with predict_proba in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A classifier's predict gives you one answer per sample, the class it picks. predict_proba tells you how sure it is: a probability for every class. That's what you need to rank customers by how likely they are to buy, or to pick your own cut-off. This guide compares the two on three flowers, with real output from scikit-learn 1.9.1.

Syntax​

Syntax
probabilities = model.predict_proba(X)
PartWhat it means
modelA trained classifier that supports probabilities, such as LogisticRegression
XThe samples, one per row, with the columns the model was trained on
probabilitiesAn array with one row per sample and one column per class, in the order of model.classes_. Each row adds up to 1

Step 1: Compare predict and predict_proba​

The example trains a logistic regression on scikit-learn's iris flowers, three species numbered 0, 1 and 2, and asks about three flowers, one from each species:

The predict_proba.py script in a macOS-style code window: it trains a LogisticRegression on iris, picks rows 0, 70 and 140, and prints classes_, predict and predict_proba rounded to three decimals.

predict_proba.py
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

X, y = load_iris(return_X_y=True, as_frame=True)
model = LogisticRegression(max_iter=500).fit(X, y)

rows = X.iloc[[0, 70, 140]]
print("Classes: ", model.classes_)
print("predict: ", model.predict(rows))
print("predict_proba:")
print(model.predict_proba(rows).round(3))
Terminal
uv run python predict_proba.py
Output
Classes: [0 1 2]
predict: [0 2 2]
predict_proba:
[[0.982 0.018 0. ]
[0.002 0.441 0.557]
[0. 0.02 0.98 ]]

The output of predict_proba.py in a terminal: classes 0 1 2, predictions 0 2 2, and three rows of probabilities, the middle one 0.002, 0.441, 0.557.

  • The columns follow classes_: the first column is class 0, the second class 1, the third class 2.
  • The first and last flowers are clear cases: 0.982 for class 0, 0.98 for class 2.
  • The middle flower is not: 0.441 for class 1 against 0.557 for class 2. predict says 2, the class with the highest probability, though the flower is in fact from species 1 (row 70 of the data). predict hides that doubt; predict_proba shows it.

Step 2: Take One Class's Column​

For a yes/no model, the column you usually want is the probability of "yes". With classes 0 and 1, that's the second column: model.predict_proba(X)[:, 1]. How to Calculate ROC AUC in scikit-learn scores a model with exactly that column.

Common Mistakes​

MistakeWhat happensDo this instead
Assuming the columns are in alphabetical or any fixed orderYou read the wrong class's probabilityCheck model.classes_
Using predict to rank samples by how likely they areEvery sample gets only 0 or 1predict_proba(X)[:, 1]

Troubleshooting​

SymptomLikely causeFix
AttributeError: 'LinearSVC' object has no attribute 'predict_proba'Not every classifier gives probabilities; LinearSVC doesn'tUse a classifier that does, such as LogisticRegression

FAQ​

Do the probabilities in a row always add up to 1?​

Yes: each row shares 1 across the classes, as all three rows above do.

Can I change the 0.5 cut-off for a yes/no model?​

Yes: compare predict_proba(X)[:, 1] with your own threshold instead of calling predict.

Conclusion​

predict gave one class per flower; predict_proba gave three probabilities each, in the order of classes_, and showed that the middle flower was a close call. For a yes/no model, take the second column for the probability of yes.

Additional Resources​