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

Scikit-Learn · Class

StandardScaler

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.

How it works

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.

Syntax

class sklearn.preprocessing.StandardScaler(*, copy=True, with_mean=True, with_std=True)

Parameters

NameTypeDescription
copybool, default=TrueWhether to center the data before scaling.
with_meanbool, default=TrueWhether to center the data before scaling. Centering sparse matrices is not supported.
with_stdbool, default=TrueWhether to scale the data to unit variance.

Returns

An estimator that standardizes features and stores the training means and standard deviations for subsequent transformation.

Key points

  • StandardScaler standardizes features by subtracting their learned means and scaling them to unit variance by default.
  • Each transformed value follows the standard-score calculation z = (x - u) / s.
  • Statistics and transformations are computed separately for every feature rather than across the whole input matrix.
  • After fitting, the learned statistics are retained and reused when new data is passed to transform.
  • with_std=True makes each feature's scale target unit variance, and it is enabled by default.

Common mistakes

  • Do not center sparse CSR or CSC input; set with_mean=False to preserve its sparse structure.
  • When scaling is disabled, the divisor used by the documented formula is effectively one.

Example: Fit and transform features

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.

When to use it

Normalize feature magnitudes

Numeric features have different units or magnitudes and a model benefits from comparable feature scales.

Reuse learned preprocessing

A preprocessing pipeline must apply the same training-data statistics to validation, test, or production inputs.

Scale sparse input

Sparse CSR or CSC features need scaling without converting the matrix to dense form.

Interview questions

QHow does StandardScaler transform a feature?

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.

QWhat do with_mean and with_std control?

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.

QWhat formula does StandardScaler use?

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.preprocessing
  • RobustScaler
  • MinMaxScaler
  • MaxAbsScaler
  • Normalizer

Source: StandardScaler. Checked 2026-10-15.