Classify new samples
Apply a trained classifier to unseen feature rows.
Scikit-Learn · Function
predict applies a fitted scikit-learn estimator to input samples and returns predictions. Classification produces class labels; regression produces numeric values.
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.
predict(X)| Name | Type | Description |
|---|---|---|
X | array-like or sparse matrix | Samples with shape (n_samples, n_features); the input data to predict. |
An array-like prediction result with shape (n_samples,) or (n_samples, n_outputs): predicted classes or predicted values.
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.
Apply a trained classifier to unseen feature rows.
Estimate a continuous target after fitting a regressor.
Run a fitted Pipeline on raw or transformed feature data.
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.
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.
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.
fitpredict_probadecision_functionPipelineclasses_