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

Scikit-Learn · Module

fit_transform

fit_transform fits a transformer on data and then transforms that data in one call. It accepts optional targets and fit parameters, returning the transformed feature array.

How it works

fit_transform first learns the transformer's parameters from X and optional y, then applies the learned transformation to X. It combines fitting and transforming in one operation and returns the resulting feature representation.

Syntax

fit_transform(X, y=None, **fit_params)

Parameters

NameTypeDescription
Xarray-likeInput samples, array-like of shape (n_samples, n_features).
yarray-like or None, default=NoneTarget values, array-like of shape (n_samples,) or (n_samples, n_outputs); None for unsupervised transformations.
**fit_paramsdictAdditional fit parameters; pass only when the estimator's fit method accepts them.

Returns

X_new, an array of shape (n_samples, n_features_new), containing the transformed values.

Key points

  • The operation learns transformation parameters from the supplied data and immediately applies them.
  • Optional fit parameters can pass extra information into fitting before transformation occurs.
  • y supplies targets when the transformation is supervised; unsupervised transformers generally leave it unset.

Common mistakes

  • Only provide extra fit parameters when the estimator's fit method explicitly supports them.

Example: Scale training data

from sklearn.preprocessing import StandardScaler

X = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
X_scaled = StandardScaler().fit_transform(X)
print(X_scaled)

Creates standardized features by learning statistics from X and transforming X in one call.

When to use it

Learn and apply scaling

Training feature preprocessing

Create a reduced representation

Dimensionality reduction

Use a transformer’s combined operation

Custom transformers

Interview questions

QWhat does fit_transform do?

It fits a transformer on X and y, then transforms X, returning the transformed array. It is commonly used when the same data must be used both to learn and apply a transformation.

Follow-up: Why can this be preferable to fit(X).transform(X)? It can provide a transformer-specific optimized implementation, and some estimators warn that separate operations may be unsafe when fit mutates the input.

QWhat are the X and y inputs?

X is array-like with shape (n_samples, n_features), while y is optional and has shape (n_samples,) or (n_samples, n_outputs). For unsupervised transformations, y is normally None.

Follow-up: What should be checked when passing y? Confirm that the transformer supports supervised fitting and that y matches the sample count.

QWhat does fit_transform return?

The method returns X_new, an array with shape (n_samples, n_features_new). The number of output features can differ from the input because a transformer may reduce, expand, or otherwise change the representation.

Follow-up: How would you determine the output width? Inspect the fitted transformer or the returned array rather than assuming it equals the input feature count.

  • fit
  • transform
  • TransformerMixin
  • StandardScaler
  • PCA

Source: sklearn.base.TransformerMixin — scikit-learn 1.9.1 documentation. Checked 2026-10-15.