Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

predict

predict applies a fitted scikit-learn estimator to input samples and returns predictions. Classification produces class labels; regression produces numeric values.

How it works

After fitting an estimator, call predict with compatible samples arranged as rows and features arranged as columns. The estimator applies its learned model and returns one prediction per sample; the result represents classes for classification and numeric values for regression.

Syntax

predict(X)

Parameters

NameTypeDescription
Xarray-like or sparse matrixSamples with shape (n_samples, n_features); the input data to predict.

Returns

An array-like prediction result with shape (n_samples,) or (n_samples, n_outputs): predicted classes or predicted values.

Key points

  • The X argument contains the samples to classify or evaluate.
  • The output is an array containing predictions, with one result per sample.
  • The method supports both classification and regression estimators.

Common mistakes

  • Classification returns predicted classes, whereas regression returns predicted numeric values.
  • For classifiers, interpret class labels using the estimator's classes_ ordering.

Example: Predict class labels

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

X, y = load_iris(return_X_y=True)
model = LogisticRegression(max_iter=200).fit(X, y)
predictions = model.predict(X[:3])
print(predictions)

Fit a classifier, then use predict to obtain one class label for each input sample.

When to use it

Classify new samples

Apply a trained classifier to unseen feature rows.

Predict regression values

Estimate a continuous target after fitting a regressor.

Use preprocessing with prediction

Run a fitted Pipeline on raw or transformed feature data.

Interview questions

QWhat does predict return for classification versus regression?

predict applies a fitted estimator to input samples and returns one output for each sample. In classification, outputs are predicted classes; in regression, they are predicted values.

Follow-up: How do you obtain class probabilities instead? Use an estimator's predict_proba method when that estimator supports it.

QWhat input does predict accept?

X is the collection of samples, with shape (n_samples, n_features). Each row is a sample and each column is a feature used by the fitted model.

Follow-up: What happens if feature preparation differs from training? Use the same preprocessing, commonly by fitting and predicting through a Pipeline.

QHow can a classifier resolve tied class probabilities?

For DecisionTreeClassifier, predict chooses the class associated with the largest output of predict_proba. If probabilities tie, it selects the class with the lowest index in classes_.

Follow-up: Why use predict_proba? It exposes class probability estimates rather than only the selected class.

  • fit
  • predict_proba
  • decision_function
  • Pipeline
  • classes_

Source: DecisionTreeClassifier — scikit-learn 1.9.1 documentation. Checked 2026-10-15.