Skip to main content

How to Scale Features with StandardScaler in scikit-learn

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Many models work best when every feature is on a similar scale, but real data mixes heights in centimeters with weights in kilograms. StandardScaler rescales each feature to a mean of 0 and a spread of 1. This guide scales a small table of heights and weights, with real output from scikit-learn 1.9.1.

Syntax​

Syntax
from sklearn.preprocessing import StandardScaler

<scaler> = StandardScaler()
<scaled_train> = <scaler>.fit_transform(<train>)
<scaled_new> = <scaler>.transform(<new rows>)
PartWhat it means
fit_transform(<train>)Learns each column's mean and standard deviation from the training data, then scales it
transform(<new rows>)Scales other rows with the numbers learned from the training data
<scaler>.mean_, <scaler>.scale_The learned means and standard deviations

The scikit-learn reference: "Standardize features by removing the mean and scaling to unit variance. The standard score of a sample x is calculated as: z = (x - u) / s where u is the mean of the training samples or zero if with_mean=False, and s is the standard deviation of the training samples or one if with_std=False."

Prerequisites​

  • scikit-learn in your project, for example with uv add scikit-learn. This guide uses scikit-learn 1.9.1 on Python 3.15.2.

Step 1: Write the Script​

Four people's heights in centimeters and weights in kilograms:

scale_demo.py
from sklearn.preprocessing import StandardScaler

heights_and_weights = [[150, 50], [160, 60], [170, 70], [180, 80]]

scaler = StandardScaler()
scaled = scaler.fit_transform(heights_and_weights)

print(scaler.mean_)
print(scaled.round(2))

The scale_demo.py script in a macOS-style code window: four rows of height and weight, StandardScaler().fit_transform on them, then print of scaler.mean_ and the scaled values rounded to two decimals.

Step 2: Run It​

Terminal
uv run python scale_demo.py
Output
[165. 65.]
[[-1.34 -1.34]
[-0.45 -0.45]
[ 0.45 0.45]
[ 1.34 1.34]]

The script's output in a terminal: the means 165 and 65, then the scaled rows from -1.34 to 1.34 in both columns.

The means were 165 cm and 65 kg. After scaling, both columns run from -1.34 to 1.34 around 0, so neither outweighs the other just because of its unit.

Common Mistakes​

MistakeWhat happensDo this instead
Fitting a new scaler on the test dataThe test data is scaled by its own numbers: a single new row [175, 72] came out as [[0. 0.]]transform it with the scaler fitted on the training data: [[0.89 0.63]]
Scaling before splitting into training and test dataThe scaler learns from the test rows tooSplit first, or put the scaler in a pipeline

Troubleshooting​

SymptomLikely causeFix
You need the original units backPredictions or values are in scaled unitsscaler.inverse_transform(...): the first scaled row came back as [[150. 50.]]

FAQ​

What did the scaler learn besides the means?​

The standard deviations, in scale_: [11.18 11.18] here. Each value is scaled as (value − mean) / standard deviation.

Does every model need scaled features?​

No. The decision tree guide lists among trees' advantages: "Requires little data preparation. Other techniques often require data normalization". Linear models, such as logistic regression, are among those that do.

Conclusion​

StandardScaler().fit_transform learned means of 165 and 65 and put both columns on the same scale. Fit it on training data only, and transform everything else with it.

Additional Resources​