Learn and apply scaling
Training feature preprocessing
Scikit-Learn · Module
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.
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.
fit_transform(X, y=None, **fit_params)| Name | Type | Description |
|---|---|---|
X | array-like | Input samples, array-like of shape (n_samples, n_features). |
y | array-like or None, default=None | Target values, array-like of shape (n_samples,) or (n_samples, n_outputs); None for unsupervised transformations. |
**fit_params | dict | Additional fit parameters; pass only when the estimator's fit method accepts them. |
X_new, an array of shape (n_samples, n_features_new), containing the transformed values.
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.
Training feature preprocessing
Dimensionality reduction
Custom transformers
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.
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.
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.
fittransformTransformerMixinStandardScalerPCA