Standardize a feature list to mean 0 and unit variance via (x āˆ’ mean)/std, where std uses ddof=0 (population std). Three loops: accumulate mean, accumulate variance sum, apply transform. Library: sklearn.preprocessing.StandardScaler ().fit_transform(X). RESULT: standardized list (rounded). CRITICAL: sklearn StandardScaler uses population std (ddof=0), NOT sample std (ddof=1).

By hand

With scikit-learn

StandardScaler().fit_transform(X) computes the same formula with ddof=0 internally. The snapshot shows the learned mean and scale for verification.

naive.py
import math
data = [1, 2, 3, 4, 5]
n = len(data)
total = 0.0
for v in data:
    total = total + v
mean = total / n
var_sum = 0.0
for v in data:
    var_sum = var_sum + (v - mean) ** 2
std = math.sqrt(var_sum / n)
scaled = []
for v in data:
    scaled.append(round((v - mean) / std, 4))
print('RESULT:', scaled)
library.py
import numpy as np
from sklearn.preprocessing import StandardScaler
from dalib.display import set_display
set_display()

data = [1, 2, 3, 4, 5]
X = np.array(data).reshape(-1, 1)
scaler = StandardScaler()
scaled = [round(v, 4) for v in scaler.fit_transform(X).ravel().tolist()]
print('mean:', round(float(scaler.mean_[0]), 4))
print('std (ddof=0):', round(float(scaler.scale_[0]), 4))
print('RESULT:', scaled)
mean: 3.0
std (ddof=0): 1.4142
RESULT: [-1.4142, -0.7071, 0.0, 0.7071, 1.4142]

Implementation notes

  • ddof trap: StandardScaler uses population std (ddof=0), dividing by n. The z-scores lesson in python-stats ch02 used sample std (ddof=1, dividing by nāˆ’1) — the two produce different results for finite samples. Always match the formula to what the library uses when verifying parity.
  • scaler.scale_ equals np.std(data) (ddof=0 default), NOT np.std(data, ddof=1).
  • After standardization the population mean is 0 and population std is 1; the sample std (ddof=1) is slightly above 1 for finite n.
  • Cross-reference: min-max-scale (this chapter) for the [0,1] alternative; z-scores (python-stats ch02) for the ddof=1 variant.