PIXELBANKv9.1.0
Menu

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:
  • The given dataset is X = [[1, 10], [2, 20], [3, 30]] and the method is "standard", which implies Standardization (Z-score).
  • To apply standardization, we calculate the mean (μ\mu) and standard deviation (σ\sigma) for each column: for the first column, μ=1+2+33=2\mu = \frac{1+2+3}{3} = 2 and σ=13((1−2)2+(2−2)2+(3−2)2)=13(1+0+1)=23\sigma = \sqrt{\frac{1}{3}((1-2)^2 + (2-2)^2 + (3-2)^2)} = \sqrt{\frac{1}{3}(1+0+1)} = \sqrt{\frac{2}{3}}; for the second column, μ=10+20+303=20\mu = \frac{10+20+30}{3} = 20 and σ=13((10−20)2+(20−20)2+(30−20)2)=13(100+0+100)=2003\sigma = \sqrt{\frac{1}{3}((10-20)^2 + (20-20)^2 + (30-20)^2)} = \sqrt{\frac{1}{3}(100+0+100)} = \sqrt{\frac{200}{3}}.
  • We then apply the standardization formula x′=x−μσx' = \frac{x - \mu}{\sigma} to each element in the columns: for the first column, x′=x−223x' = \frac{x - 2}{\sqrt{\frac{2}{3}}}, and for the second column, x′=x−202003=x−2023⋅10=x−201023x' = \frac{x - 20}{\sqrt{\frac{200}{3}}} = \frac{x - 20}{\sqrt{\frac{2}{3}} \cdot 10} = \frac{x - 20}{10\sqrt{\frac{2}{3}}}.
  • After calculating the standardized values for each element and rounding to 4 decimal places, we get the output [[-1.2247, -1.2247], [0.0, 0.0], [1.2247, 1.2247]].

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
solution.py

Test Results

0/0
Run code to see test results.
Feature Scaling - Easy | PixelBank