v9.1.1
Menu

Feature Scaling

easyFREEDimensionality ReductionMachine Learning 2

Implement two common feature scaling methods:

  1. Standardization (Z-score): x′=x−μσx' = \frac{x - \mu}{\sigma} where μ\mu is the mean and σ\sigma is the standard deviation
  2. Min-Max Scaling: x′=x−min⁡max⁡−min⁡x' = \frac{x - \min}{\max - \min}

Given a 2D dataset and a method name, scale each column (feature) independently.

Use the population standard deviation: σ=1n∑(xi−μ)2\sigma = \sqrt{\frac{1}{n}\sum(x_i - \mu)^2}

Return the scaled matrix, rounded to 4 decimal places. If std = 0 or max = min, return 0.0 for that feature.

Example

Input
X = [[1, 10], [2, 20], [3, 30]]
method = "standard"
Output
[[-1.2247, -1.2247], [0.0, 0.0], [1.2247, 1.2247]]
Reasoning
  • Inputs: X is a 3×23 \times 2 dataset — 3 samples (rows), 2 features (columns) — and method is "standard", so each column is turned into Z-scores x′=x−μσx' = \frac{x - \mu}{\sigma} using only that column's own statistics.
  • Column 1 (1,2,3)(1, 2, 3): μ=1+2+33=2\mu = \frac{1+2+3}{3} = 2, squared deviations 1,0,11, 0, 1, so σ=23≈0.8165\sigma = \sqrt{\frac{2}{3}} \approx 0.8165 (divide by n=3n = 3, the population form).
  • Z-scores: 1−20.8165≈−1.2247\frac{1-2}{0.8165} \approx -1.2247, 2−20.8165=0\frac{2-2}{0.8165} = 0, 3−20.8165≈1.2247\frac{3-2}{0.8165} \approx 1.2247.
  • Column 2 (10,20,30)(10, 20, 30): μ=20\mu = 20, squared deviations 100,0,100100, 0, 100, σ=2003≈8.165\sigma = \sqrt{\frac{200}{3}} \approx 8.165, giving ±108.165≈±1.2247\frac{\pm 10}{8.165} \approx \pm 1.2247 and 00 again.
  • Output: rounding to 4 decimal places gives [[-1.2247, -1.2247], [0.0, 0.0], [1.2247, 1.2247]], same 3×23 \times 2 shape as X.
  • Notice the two columns come out identical: column 2 is just 10×10\times column 1, and Z-scores ignore units and scale. Each column now has mean 00 and standard deviation 11. A constant column would have σ=0\sigma = 0 and becomes all 0.0.

Constraints

  • X: 2D list (n_samples x n_features)
  • method: "standard" or "minmax"
  • Scale each column independently
  • Return 2D list rounded to 4 decimal places

Learn this in

More from Machine Learning 2

solution.py

Test results

Sign in to run your code against the test cases.