Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Scaling Without Leaking
StandardScaler shifts each feature to mean 0 and scales it to standard deviation 1. Models that measure distance (k-NN, SVM, k-means) or use gradient descent need this, because otherwise a feature measured in thousands drowns out one measured in tenths.
from sklearn.preprocessing import StandardScaler scaler = StandardScaler() scaler.fit(X_train) # learn the mean and std X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # SAME numbers, not refitted
This is the important part. The scaler must be fitted on the training data only. Then the exact same mean and standard deviation are applied to the test data.
Fitting on everything, or fitting separately on the test set, means the test set's statistics have influenced how the training data is represented. Your validation score goes up, your real-world performance does not, and you will not find out until it is expensive. That is data leakage, and it is the most common way a promising model turns out to be worthless.
fit_transform is shorthand for fit-then-transform. Use it on train, and never on test:
X_train_scaled = scaler.fit_transform(X_train) # fine X_test_scaled = scaler.fit_transform(X_test) # bug, silent, expensive
Your task:
Write scale_train_test(X_train, X_test) that fits a StandardScaler on the training data and returns the scaled test data.
The test data is deliberately chosen so that a leaking implementation, which refits on the test set, gives a different answer from a correct one.
Example Tests
Test data is scaled with the training mean and std, so it does not come out centred on zero
Input: {"X_test":[[10],[12]],"X_train":[[0],[2],[4]]}
Expected: [[4.89898],[6.12372]]
A test point equal to the training mean scales to exactly 0
Input: {"X_test":[[5]],"X_train":[[0],[10]]}
Expected: [[0]]
The output has the same shape as the test input
Input: {"X_test":[[5,6],[7,8]],"X_train":[[1,2],[3,4]]}
Expected: [2,2]
import numpy as np
from sklearn.preprocessing import StandardScaler
def scale_train_test(X_train, X_test):
"""
Scale the test set using statistics learned from the training set.
Args:
X_train: 2-D training features
X_test: 2-D test features
Returns:
The scaled test features.
"""
# YOUR CODE HERE
passRun your code to see results
⌘↵ runs against the visible tests