Standard Scaler Class

Start Timer

0:00:00

Upvote
6
Downvote
Save question
View comments

Write a class StandardScaler that standardizes each column of a 2D list of numbers. It needs three methods:

  • fit(X) computes and stores the mean and standard deviation of each column.
  • transform(X) returns a new 2D list where every value has had its column mean subtracted and been divided by its column standard deviation.
  • fit_transform(X) does both in one call.

Rules - Use the population standard deviation (divide by n). - transform must use the statistics stored during fit, not recomputed from its own input. - If all values in a column are equal during fit, store 1 as that column’s standard deviation, so fit_transform turns that column into all 0s.

Example:

Input:

X = [[1, 10], [2, 20], [3, 30]]
scaler = StandardScaler()

Output:

scaler.fit_transform(X) -> [[-1.224744871391589, -1.224744871391589],
                            [0.0, 0.0],
                            [1.224744871391589, 1.224744871391589]]
.
.
.
.
.


Comments

Loading comments