Why Scale Features
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 47 of 64.
Which customer is most like a new one? Many models measure the distance between samples: square each feature's gap, add them up, take the square root.
A 25-year-old earning $52,000 against a 61-year-old earning $53,000:
For a whole table, axis=1 sums each row's squares and .argmin() finds the nearest row:
a = np.array([25, 52000]) # age, income
X = np.array([[61, 53000], # the 61-year-old
[26, 55000]]) # a 26-year-old earning $55,000
dists = np.sqrt(((X - a) ** 2).sum(axis=1))
print(np.round(dists, 2))
print(dists.argmin())This prints:
[1000.65 3000. ]
0The 61-year-old is nearest: dollars come in bigger numbers, so $1,000 outweighs 36 years. Unscaled, the biggest units decide.
This chapter gives every feature a fair say.
Challenge
MediumA shop compares washing machines by price ($), energy (kWh a year) and water (liters a wash), named in features. The program reads two machine positions, i and j. Print:
Squared gaps:each feature's gap, squared.Distance:rounded to 2 decimals.Dominant feature:the name of the feature with the largest squared gap.
For two other machines:
Squared gaps: [2500 6400 1]
Distance: 94.35
Dominant feature: energyTry it yourself
import numpy as np
features = ["price", "energy", "water"]
# One row per washing machine: price in dollars, energy in kWh per year, water in liters per wash
machines = np.array([[499, 160, 48],
[349, 210, 55],
[899, 120, 42],
[529, 250, 50],
[279, 230, 60],
[749, 140, 45]])
i = int(input())
j = int(input())
# 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