Start learning free

LESSON 76 OF 165 · MACHINE LEARNING: ML INTERVIEW QUESTIONS

Write a Standard Scaler in Python

Practice

An interview favorite: write a standard scaler with fit and transform, numpy only. Columns come in very different sizes, small counts next to amounts in the hundreds, and a model that compares distances lets the big column win. A standard scaler puts every column on the same scale: each value becomes (value - mean) / std, how many standard deviations it sits from its column's mean.

The standard deviation (std) is the typical distance of a column's values from their mean. On a 2-D numpy array, .mean(axis=0) gives back one mean per column and .std(axis=0) gives back one std per column. Subtracting such a row of per-column numbers from a table, or dividing by it, works column by column.

A = np.array([[1, 10],
              [3, 30]])
print(A.mean(axis=0))   # [ 2. 20.]
print(A.std(axis=0))    # [ 1. 10.]

Why are fit and transform separate? fit learns the mean and std from the training rows and keeps them on the object, in self.mean and self.std. transform then scales any rows with those stored numbers: the test rows, or one new customer arriving alone. Test rows must be measured with the same ruler as the rows the model learned from.

With the lesson's 5 training rows and 2 test rows, your code and the check print:

Training means: [  6. 150.  80.]
Training stds: [ 2. 64. 32.]
Test rows, scaled with the training numbers:
[[ 1.    1.   -2.  ]
 [-1.   -1.25  0.5 ]]
One new customer [6, 150, 80]: [[0. 0. 0.]]
Fit on the 2 test rows instead:
[[ 1.  1. -1.]
 [-1. -1.  1.]]
Same as scikit-learn's StandardScaler on random rows: True

Your task: below X_test, write the class StandardScaler: a fit method that stores each column's mean and std on self, and a transform method that returns the rows it gets, scaled with those stored numbers.


Assignment
Follow the Coding Tutorial to write the StandardScaler class with its fit and transform methods.


Hint
Methods sit four spaces in, inside the class, and their bodies eight. transform uses self.mean and self.std, not the mean of the rows it gets now; put the subtraction in parentheses before you divide.


STEP 1 OF 6
    Ctrl + Enter to check

    Mlq Standard Scaler in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free