Before you start
Implement the two scalers every pipeline starts with.
Standardisation centres each feature and divides by its standard deviation:
z = (x - mean) / std
Min-max scaling squashes each feature into [0, 1]:
z = (x - min) / (max - min)
Both operate per column of an (n, d) array, so each feature is scaled
independently.
Scaling is not cosmetic. Gradient descent on features measured in different units zig-zags badly, and any distance based method such as k-NN or k-means is dominated by whichever feature happens to have the largest range.
Use the population standard deviation, dividing by n (which is what np.std
gives you by default).
Handle constant columns: if a column has zero standard deviation, or its min equals its max, return zeros for that column instead of dividing by zero.
def standardize(X):
def min_max_scale(X):
Input
X = np.array([[1.0], [2.0], [3.0]])
standardize(X)
min_max_scale(X)
Output
array([[-1.22474487], [ 0. ], [ 1.22474487]])
array([[0. ], [0.5], [1. ]])
Reasoning
The mean is 2 and the population standard deviation is sqrt(2/3) = 0.8165, so the first row becomes -1 / 0.8165 = -1.2247. The min is 1 and the max is 3, so min-max scaling maps the rows to 0, 0.5 and 1.
n).Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell