The predict method
- Machine Learning course
- Intermediate
- Lesson 4/112
- 5 min
The predict() method makes predictions for new data.
The predict family
Classifiers have several prediction methods:
predict: the class label.predict_proba: class probabilities (column orderclasses_).decision_function: the raw score before the threshold; in SVMs and linear models.
m = LogisticRegression(max_iter=1000).fit(X_train, y_train)
print(m.predict(X_test[:2]))
print(m.predict_proba(X_test[:2]).round(3))
print(m.decision_function(X_test[:2]).round(2))SVC doesn't offer predict_proba by default; it is enabled with probability=True, but training slows down.
Shape of the prediction input
predict always needs two-dimensional input with the training column count. For a single example, reshape(1, -1) or the slice X[i:i+1] is used:
ornek = X_test[0] # shape (4,)
m.predict(ornek.reshape(1, -1)) # shape (1, 4)
m.predict(X_test[0:1]) # the sameThe slice X[0:1] keeps two dimensions; the index X[0] drops one.
Practice Score and probability
Train scaled logistic regression on the breast cancer data. For the first test example print the decision_function score and the predict_proba[:, 1] probability with round(..., 3) on one line. Then print with np.isclose that the probability equals 1 / (1 + np.exp(-skor)).
Starter code
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
# buraya yazSample solution
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
m = make_pipeline(StandardScaler(), LogisticRegression()).fit(X_train, y_train)
skor = m.decision_function(X_test[:1])[0]
olasilik = m.predict_proba(X_test[:1])[0, 1]
print(round(skor, 3), round(olasilik, 3))
print(np.isclose(olasilik, 1 / (1 + np.exp(-skor))))Expected output
5.457 0.996 True
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 In binary logistic regression, if <code>decision_function</code> is 0, what is the probability?
- 0.5 Correct answer
- 0
- 1
- Undefined
Explanation: The probability is sigmoid(score); sigmoid(0) = 0.5, the decision boundary.
2 Make the single example two-dimensional.
m.predict(ornek.reshape(____))
m.predict(ornek.reshape(1, -1))
Explanation: reshape(1, -1) produces one row with as many columns as needed.
3 Match the methods.
| Term | Meaning |
|---|---|
predict |
Label |
predict_proba |
Probabilities |
decision_function |
Raw score |
classes_ |
Column order |