Transductive learning
Apply an unsupervised estimator to its training observations and immediately obtain their assigned outputs.
Scikit-Learn · Module
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.
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_.
estimator.fit_predict(X, y=None, **fit_params)| Name | Type | Description |
|---|---|---|
X | array-like | Training data used to fit the estimator and generate predictions. |
y | array-like, default=None | Optional target values, when required or supported by the estimator. |
**fit_params | dict | Additional estimator-specific fitting parameters, depending on the implementation. |
Predictions for the training data; for clusterers, an array of cluster labels.
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.
Apply an unsupervised estimator to its training observations and immediately obtain their assigned outputs.
Train a clustering estimator and retrieve one cluster label per input sample.
Detect outliers in the data used to fit LocalOutlierFactor.
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_.
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.
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.
fitpredictfit_transformlabels_Pipeline.fit_predict