18
Tensors
Preprocessing
Preprocessing — Scalers
Feature scaling is essential before many ML algorithms. Deepbox provides 7 feature scalers. This example uses deepbox/ndarray, deepbox/preprocess and focuses on tensor; StandardScaler, MinMaxScaler, RobustScaler, MaxAbsScaler, Normalizer, PowerTransformer, QuantileTransformer.
Deepbox Modules Used
deepbox/ndarraydeepbox/preprocessWhat You Will Learn
- Use deepbox/ndarray for tensor.
- Use deepbox/preprocess for StandardScaler, MinMaxScaler, RobustScaler, MaxAbsScaler, Normalizer, PowerTransformer, QuantileTransformer.
- Feature scaling is essential before many ML algorithms. Deepbox provides 7 feature scalers.
Source Files
index.ts
1/**2 * Example 18: Preprocessing — Scalers3 *4 * Feature scaling is essential before many ML algorithms.5 * Deepbox provides 7 feature scalers.6 */78import { tensor } from "deepbox/ndarray";9import {10 MaxAbsScaler,11 MinMaxScaler,12 Normalizer,13 PowerTransformer,14 QuantileTransformer,15 RobustScaler,16 StandardScaler,17} from "deepbox/preprocess";1819console.log("=== Preprocessing: Scalers ===\n");2021// Sample data with different scales22const X = tensor([23 [1, 100, 0.01],24 [2, 200, 0.02],25 [3, 300, 0.03],26 [4, 400, 0.04],27 [5, 500, 0.05],28 [100, 50, 0.5],29]);3031// ---------------------------------------------------------------------------32// Part 1: StandardScaler — zero mean, unit variance33// ---------------------------------------------------------------------------34console.log("--- Part 1: StandardScaler ---");3536const ss = new StandardScaler();37ss.fit(X);38const XStd = ss.transform(X);39console.log("Scaled (first 3 rows):\n", XStd.toString());4041const XInv = ss.inverseTransform(XStd);42console.log("Inverse (first row):", XInv.toString());4344// ---------------------------------------------------------------------------45// Part 2: MinMaxScaler — scale to [0, 1]46// ---------------------------------------------------------------------------47console.log("\n--- Part 2: MinMaxScaler ---");4849const mms = new MinMaxScaler();50mms.fit(X);51const XMinMax = mms.transform(X);52console.log("Scaled (first 3 rows):\n", XMinMax.toString());5354// ---------------------------------------------------------------------------55// Part 3: RobustScaler — uses median and IQR (robust to outliers)56// ---------------------------------------------------------------------------57console.log("\n--- Part 3: RobustScaler ---");5859const rs = new RobustScaler();60rs.fit(X);61const XRobust = rs.transform(X);62console.log("Scaled (first 3 rows):\n", XRobust.toString());6364// ---------------------------------------------------------------------------65// Part 4: MaxAbsScaler — scale by maximum absolute value66// ---------------------------------------------------------------------------67console.log("\n--- Part 4: MaxAbsScaler ---");6869const mas = new MaxAbsScaler();70mas.fit(X);71const XMaxAbs = mas.transform(X);72console.log("Scaled (first 3 rows):\n", XMaxAbs.toString());7374// ---------------------------------------------------------------------------75// Part 5: Normalizer — normalize each sample (row) to unit norm76// ---------------------------------------------------------------------------77console.log("\n--- Part 5: Normalizer ---");7879const norm = new Normalizer();80const XNorm = norm.transform(X);81console.log("Normalized (first 3 rows):\n", XNorm.toString());8283// ---------------------------------------------------------------------------84// Part 6: PowerTransformer — Gaussian-like transformation85// ---------------------------------------------------------------------------86console.log("\n--- Part 6: PowerTransformer ---");8788const pt = new PowerTransformer();89pt.fit(X);90const XPower = pt.transform(X);91console.log("Transformed (first 3 rows):\n", XPower.toString());9293// ---------------------------------------------------------------------------94// Part 7: QuantileTransformer — map to uniform or normal distribution95// ---------------------------------------------------------------------------96console.log("\n--- Part 7: QuantileTransformer ---");9798const qt = new QuantileTransformer();99qt.fit(X);100const XQuantile = qt.transform(X);101console.log("Transformed (first 3 rows):\n", XQuantile.toString());102103console.log("\n=== Preprocessing: Scalers Complete ===");104Console Output
$ npx tsx 18-preprocessing-scalers/index.ts
Console output comparing all 7 scaler outputs on the same dataset