StandardScaler
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 50 of 64.
A model must scale every new sample exactly as it scaled its training data. scikit-learn's StandardScaler standardizes and remembers how:
In code, each job is windows cleaned and minutes taken:
from sklearn.preprocessing import StandardScaler
X_train = np.array([[2, 8], [10, 25], [6, 20], [14, 35]])
scaler = StandardScaler()
scaler.fit(X_train)
X_new = np.array([[8, 30]]) # one sample: a table with one row
print(np.round(scaler.transform(X_new), 2))This prints:
[[0. 0.82]]fit learns each column's mean and std (dividing by n, like np.std), kept in scaler.mean_ and scaler.scale_. transform applies them.
Never fit on new data: the job would be its own mean and become [[0. 0.]]. On training data, fit_transform does both in one call.
Challenge
MediumA sorter learned from the avocados in X_train: weight, firmness and days since harvest. The program reads a new avocado into the one-row table new_avocado, but the starter's scaling prints [[0. 0. 0.]] for every avocado.
- Fix it: scale the new avocado with the training means and stds.
- Count its measurements more than 2 std from the training mean, either way, and print as below.
For another batch:
New avocado: [[2.98 0. 0.09]]
1 of 3 measurements are more than 2 std from the training avocadosTry it yourself
import numpy as np
from sklearn.preprocessing import StandardScaler
# Training avocados, one row each: weight in grams, firmness from 1 to 10, days since harvest
X_train = np.array([[210, 8, 2],
[185, 5, 6],
[240, 7, 3],
[170, 3, 9],
[225, 6, 5],
[195, 4, 7]])
# A new avocado on the belt
weight = float(input())
firmness = float(input())
days = float(input())
new_avocado = np.array([[weight, firmness, days]])
# This prints [[0. 0. 0.]] for every avocado: fix it
scaler = StandardScaler()
scaled = scaler.fit_transform(new_avocado)
print("New avocado:", np.round(scaled, 2))
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and Sun9Feature Scaling
Why Scale FeaturesMin-Max ScalingStandardizationStandardScalerRecap: Climbing PartnersPractice on your own: Online Python compiler