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

Scikit-Learn · Module

fit_predict

fit_predict fits an estimator and returns predictions for the same training data in one call. It is especially useful for unsupervised, transductive estimators and clustering workflows.

How it works

Call fit_predict on an estimator when you want it to learn from the supplied data and immediately produce predictions for that same data. It is especially common for unsupervised transductive estimators; for clusterers, the returned labels are also available through labels_.

Syntax

estimator.fit_predict(X, y=None, **fit_params)

Parameters

NameTypeDescription
Xarray-likeTraining data used to fit the estimator and generate predictions.
yarray-like, default=NoneOptional target values, when required or supported by the estimator.
**fit_paramsdictAdditional estimator-specific fitting parameters, depending on the implementation.

Returns

Predictions for the training data; for clusterers, an array of cluster labels.

Key points

  • The method is primarily intended for estimators that learn from data and assess those same observations.
  • It combines model fitting with prediction on the input training set.
  • Configure fit_predict with the same arguments accepted by the estimator's fit method.
  • For clusterers, its result normally matches fitting first and then predicting on X.

Common mistakes

  • For clustering estimators, the output is duplicated in the estimator's labels_ attribute.

Example: Cluster training samples

from sklearn.cluster import KMeans

X = [[1, 1], [1, 2], [8, 8], [9, 8]]
labels = KMeans(n_clusters=2, random_state=0, n_init=10).fit_predict(X)
print(labels)

Fits KMeans on the samples and returns the cluster label assigned to each training sample.

When to use it

Transductive learning

Apply an unsupervised estimator to its training observations and immediately obtain their assigned outputs.

Clustering

Train a clustering estimator and retrieve one cluster label per input sample.

Training-set outlier detection

Detect outliers in the data used to fit LocalOutlierFactor.

Interview questions

QWhat does fit_predict do?

fit_predict both trains an estimator and returns predictions for the training data in one operation. It is particularly associated with unsupervised, transductive estimators.

Follow-up: How does it commonly relate to separate calls for a clusterer? The result is usually equivalent to fit(X).predict(X), and is also stored in labels_.

QWhat does fit_predict return for clustering estimators?

For a clusterer, fit_predict(X) returns a label for each training sample and also populates labels_. The method's parameters are the same as fit's.

Follow-up: Why might the one-call form be useful? It expresses the intended training-data prediction operation directly and avoids writing separate fit and predict calls.

QHow does LocalOutlierFactor use fit_predict?

LocalOutlierFactor supports fit_predict for outlier detection on the training data, but does not provide predict by default. Setting novelty=True changes the intended workflow to fitting first and predicting on new unseen data.

Follow-up: Can predict and fit_predict be substituted for one another in novelty mode? No; the documentation warns that predictions on training samples are not equivalent to fit_predict.

  • fit
  • predict
  • fit_transform
  • labels_
  • Pipeline.fit_predict

Source: Glossary of Common Terms and API Elements - Scikit-learn. Checked 2026-10-15.