Python Predict() Function: How It Works

Making predictions with a Python model starts with understanding the result it returns. The predict() method applies a fitted model to input examples and returns an output based on the model and its task.

I’ll explain how to read predict() results and prepare inputs for different frameworks.

TL;DR

Python’s predict() method applies a fitted model to new input, but its return value depends on the model and task.

  • Regressors return numeric estimates. Classifiers return selected class labels.
  • Use predict_proba() for per-class estimates only when the classifier supports it.
  • Give the model the same input layout and feature order used for fitting. In scikit-learn, one sample is a two-dimensional row.
  • PyTorch modules do not have a generic predict() method. Call eval() before inference when evaluation behavior is needed.

What Is predict() in Python?

predict() is a method exposed by many fitted machine-learning models, not a built-in Python function with one universal contract. It takes input examples and computes outputs using the parameters learned during training.

Training and prediction are separate operations. In scikit-learn, fit(X, y) learns from feature data X and target values y.

Calling predict(X_new) applies that fitted estimator to new feature rows. The result is an estimate for each row, rather than an evaluation score.

A regressor returns a numeric quantity such as a predicted score. A classifier selects a class label. Some classifiers also expose predict_proba() to return one estimate per class, so check which method and estimator produced the result.

For example, the current scikit-learn documentation defines LinearRegression.predict() as returning predicted values. Its input has shape (n_samples, n_features), meaning rows are samples and columns are features. LogisticRegression.predict() instead returns a label for each sample, while its probability columns follow classes_.

How to Read predict() Results Step by Step

The model and task determine whether predict() returns a numeric estimate or a class label, as the examples below show.

from sklearn.linear_model import LinearRegression, LogisticRegression

# One numeric feature: hours studied, with a numeric score as the target.
X = [[1], [2], [3], [4]]
y = [50, 60, 70, 80]
regressor = LinearRegression().fit(X, y)
print("Regression:", regressor.predict([[5]]))

# A classifier returns a label; predict_proba returns class-wise scores.
classifier = LogisticRegression().fit([[0], [1], [2], [3]], [0, 0, 1, 1])
print("Label:", classifier.predict([[2]]))
print("Classes:", classifier.classes_)
print("Probabilities:", classifier.predict_proba([[2]]).round(3))

Run the file with .venv/bin/python predict_demo.py to compare these fitted models.

Regression: [90.]
Label: [1]
Classes: [0 1]
Probabilities: [[0.382 0.618]]

The regressor predicts 90, while the classifier selects label 1.

Terminal showing regression, class label and class probability output
Regression returns a number. Classification returns a label and class-wise scores.

For a binary classifier, check classes_ before selecting one probability column. A multiclass result has a column per class, and an estimator without predict_proba() cannot provide these values through that method.

The output is model-specific. In this example, I ran scikit-learn 1.9.1 and observed a numeric regression prediction alongside a classification label and probability row.

Input Shapes and Framework Differences

Choose the API that fits the model, then check what each returned value represents.

CaseSymptomFix
One scikit-learn sample, one featureA one-dimensional list raises a two-dimensional input errorPass a row such as [[4]]
One feature across several samplesReshaping creates the wrong sample layoutUse reshape(-1, 1)
One sample with several featuresColumns are missing or in a different orderUse reshape(1, -1) and preserve training feature order
Keras batch predictionOutput is mistaken for a label or probabilityCheck the configured output layer and task
PyTorch inferenceTraining-time layer behavior remains activeCall eval() and disable gradients when appropriate
Statsmodels predictionNew explanatory variables do not match the fitted designFollow the fitted results object’s prediction documentation

Use the model’s documented input layout. In my test, a one-dimensional input produced ValueError with the message “Expected 2D array, got 1D array instead.” The corrected row [[4]] returned [8.].

Use reshape(-1, 1) for one feature across many samples. Use reshape(1, -1) for one sample with several features. The feature names and their order must still match the data used for fitting.

Keras Model.predict() processes batches and returns the configured model output. Its FAQ distinguishes that API from calling model(x) directly, which suits an in-memory tensor or a gradient-tape workflow.

PyTorch calls a module’s forward computation when you invoke the module. The base nn.Module has no generic predict() method. Call eval() for evaluation behavior, then use inference_mode() or no_grad() when gradients are unnecessary.

Statsmodels results objects also expose predict() for new explanatory variables. Formula-based fits carry variable and transformation expectations, so consult the fitted model’s API before preparing rows.

For a worked regression workflow, see Simple Linear Regression in Python. The predictive model guide covers the broader model-building task.

A quick return-value comparison helps before you plug an output into another calculation. These are common cases, not a promise that every estimator exposes every method.

Model or methodTypical resultCheck before use
Scikit-learn regressorNumeric estimate per rowShape follows samples and target count
Scikit-learn classifier predict()One class label per rowLabels can be strings or numbers
Scikit-learn classifier predict_proba()One value per class per rowRead column order from classes_
Keras Model.predict()Configured model outputInterpret based on output layer and task
PyTorch module callForward-pass outputSet eval mode and inspect output layer

A class label identifies the estimator’s selected category. Class-wise probabilities retain the estimates for every category, which can support threshold decisions. Calibration must be evaluated before treating those estimates as confidence.

For regression, there may be no class probabilities at all. A numeric estimate might predict a continuous quantity, and its scale is inherited from the target used during training. Predicting a salary encoded in dollars gives dollar-scale estimates, while a transformed target needs the corresponding inverse transformation before readers can interpret the value.

The input data must also receive the same preprocessing used in training. If the model was fitted after scaling, encoding categories or selecting columns, send prediction data through that same fitted preprocessing workflow. A matching number of columns does not guarantee matching meaning.

Keep the fitted preprocessing and estimator together when possible, for example in a scikit-learn Pipeline. Calling predict() on the pipeline then applies its transformations before the final estimator. This reduces the chance that training rows and new rows are transformed in different ways.

Keras prediction processes batches, while a direct model(x) call handles an in-memory input. Use the direct call when gradients matter or the input is small. For a large input, batch processing controls how data reaches the model.

For larger prediction jobs, process bounded batches when memory is limited. The final batch may contain fewer rows than the others, so append every batch result in order and verify the combined output length against the number of input samples. A silent row-order mismatch can pair predictions with the wrong records even when each individual call succeeds.

These distinctions explain why a generic wrapper around several frameworks can be misleading. It must know the estimator type, input conversion, output meaning and device behavior. A short framework-specific call is often clearer than a wrapper that guesses whether an array contains labels, probabilities or raw scores.

Conclusion

Read predict() through the model’s own contract: check the input shape, then identify whether the output is a numeric estimate, a class label or a model-specific value. In scikit-learn, compare predict() with predict_proba() when class probabilities matter.

Consult the official LinearRegression and LogisticRegression API references for scikit-learn, the Keras prediction API, and the PyTorch docs for Module evaluation and inference mode.

FAQ

These quick answers cover common questions about prediction results.

What does predict() do in Python?

It applies a fitted model to input examples and returns the model’s output, such as numeric estimates or class labels, depending on the model and task.

What is the difference between predict() and predict_proba()?

In scikit-learn classifiers that implement both, predict() returns class labels and predict_proba() returns an estimate for each class, ordered by classes_.

How do I predict one sample with scikit-learn?

Pass one sample as a two-dimensional row, such as [[4]] for one feature, with the same feature count and order used during fitting.

Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189