18
Preprocessing
Scaling
Feature Engineering
Preprocessing — Scalers
Feature scaling ensures that all features contribute equally to the model, preventing features with large magnitudes from dominating. This example demonstrates all 7 Deepbox scalers: StandardScaler (zero mean, unit variance — the default choice), MinMaxScaler (scales to [0, 1]), RobustScaler (uses median and IQR, robust to outliers), MaxAbsScaler (scales by maximum absolute value), Normalizer (scales each sample to unit norm), PowerTransformer (makes data more Gaussian), and QuantileTransformer (maps to uniform or normal distribution). Each scaler is fit on training data and applied to both train and test sets. The example shows before/after statistics to verify the transformation.
Deepbox Modules Used
deepbox/ndarraydeepbox/preprocessWhat You Will Learn
- StandardScaler is the default — centers to zero mean and unit variance
- MinMaxScaler scales to [0, 1] — good for neural networks
- RobustScaler uses median/IQR — use when data has outliers
- Always fit on training data only, then transform both train and test
Source Code
18-preprocessing-scalers/index.ts
1import { tensor } from "deepbox/ndarray";2import { StandardScaler, MinMaxScaler, RobustScaler, MaxAbsScaler } from "deepbox/preprocess";3import { mean, std } from "deepbox/stats";45const X = tensor([[1, 100], [2, 200], [3, 300], [4, 400], [5, 500]]);67// StandardScaler: zero mean, unit variance8const ss = new StandardScaler();9ss.fit(X);10const X_std = ss.transform(X);11console.log("StandardScaler:");12console.log(" Mean:", mean(X_std, 0).toString()); // ≈ [0, 0]13console.log(" Std:", std(X_std, 0).toString()); // ≈ [1, 1]1415// MinMaxScaler: scale to [0, 1]16const mm = new MinMaxScaler();17mm.fit(X);18const X_mm = mm.transform(X);19console.log("\nMinMaxScaler:");20console.log(" Min:", X_mm.toString()); // [0..1] range2122// RobustScaler: uses median/IQR — robust to outliers23const rs = new RobustScaler();24rs.fit(X);25const X_rs = rs.transform(X);26console.log("\nRobustScaler:");27console.log(" Scaled:", X_rs.toString());Console Output
$ npx tsx 18-preprocessing-scalers/index.ts
StandardScaler:
Mean: [0.000, 0.000]
Std: [1.000, 1.000]
MinMaxScaler:
Min: [[0, 0], [0.25, 0.25], [0.5, 0.5], [0.75, 0.75], [1, 1]]
RobustScaler:
Scaled: [[-1, -1], [-0.5, -0.5], [0, 0], [0.5, 0.5], [1, 1]]