Skip to content
LearnPY
scikit-learnfunction reference

StandardScaler

Center features and scale them to unit variance.

Signature

StandardScaler(*, with_mean=True, with_std=True)

What it does

StandardScaler learns the mean and standard deviation of each feature during fit, then transforms values to z-scores. Fit it on training data and reuse the same scaler on test or new data.

Parameters

with_meanbool, optionaloptional
Whether to center features by subtracting the mean.
with_stdbool, optionaloptional
Whether to divide by the standard deviation.

Returns

StandardScaler

Example

pythonStandardScaler example
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

Watch out for

  • Calling fit_transform on the test set leaks information; use transform there.
  • Scaling sparse data with centering enabled can require dense memory; set with_mean=False when needed.