Normalize feature magnitudes
Numeric features have different units or magnitudes and a model benefits from comparable feature scales.
Scikit-Learn · Class
StandardScaler removes each feature's training mean and scales by its training standard deviation. It is useful for putting numeric features on comparable scales before modeling.
StandardScaler learns one mean and standard deviation per feature from the training data. It then centers and scales later values independently by feature, while allowing either centering or scaling to be disabled through its constructor options.
class sklearn.preprocessing.StandardScaler(*, copy=True, with_mean=True, with_std=True)| Name | Type | Description |
|---|---|---|
copy | bool, default=True | Whether to center the data before scaling. |
with_mean | bool, default=True | Whether to center the data before scaling. Centering sparse matrices is not supported. |
with_std | bool, default=True | Whether to scale the data to unit variance. |
An estimator that standardizes features and stores the training means and standard deviations for subsequent transformation.
from sklearn.preprocessing import StandardScaler
X = [[0, 0], [0, 0], [1, 1], [1, 1]]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(scaler.mean_)
print(X_scaled)Fits the scaler on the sample data and transforms each feature using the learned statistics.
Numeric features have different units or magnitudes and a model benefits from comparable feature scales.
A preprocessing pipeline must apply the same training-data statistics to validation, test, or production inputs.
Sparse CSR or CSC features need scaling without converting the matrix to dense form.
It computes feature-wise statistics on the training samples, then applies the standard-score transformation independently to each feature. The learned mean and standard deviation are retained for transforming later data.
Follow-up: Why should fitting be restricted to training data? Because fitting on later data would use statistics unavailable at prediction time and can leak information into the transformation.
with_mean controls centering, and with_std controls scaling to unit variance. Both default to True.
Follow-up: What setting is appropriate for sparse matrices? Disable centering with with_mean=False, because the documentation specifically supports sparse CSR or CSC input in that mode.
The documented standard score is z = (x - u) / s, where u is the training mean and s is the training standard deviation. If centering or scaling is disabled, the corresponding value is zero or one.
Follow-up: How are later samples handled? The statistics learned during fit are reused by transform rather than recomputed from each later batch.
sklearn.preprocessingRobustScalerMinMaxScalerMaxAbsScalerNormalizer