Min-Max Scaling
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 48 of 64.
Min-max scaling gives every feature a fair say: in each column, the smallest value becomes 0, the largest 1, the rest land between.
Add a 40-year-old earning $80,000, then scale all four:
Broadcasting does it in one line, with each column's own smallest value and range:
X = np.array([[25, 52000],
[61, 53000],
[26, 55000],
[40, 80000]])
lows = X.min(axis=0)
highs = X.max(axis=0)
scaled = (X - lows) / (highs - lows)Now age counts too. Customer 0's nearest neighbor is no longer the 61-year-old:
One extreme value becomes 1 and squeezes the rest near 0, so min-max suits columns without outliers.
Challenge
MediumA roaster matches coffees that brew alike. Each row of beans is a coffee: water temperature (C), brew time (s) and caffeine (mg). The program reads positions i and j; the starter's distance(a, b) measures two samples.
- Min-max scale
beans. - Print
Every column from 0 to 1:, then the scaled table to 2 decimals. - Print their distance before and after scaling, as below.
For three other coffees, positions 0 and 1:
Every column from 0 to 1:
[[1. 1. 0.67]
[0.5 0.5 0. ]
[0. 0. 1. ]]
Coffees 0 and 1: 63.28 apart before scaling, 0.97 afterTry it yourself
import numpy as np
# Returns the distance between two samples (from Why Scale Features)
def distance(a, b):
return np.sqrt(((a - b) ** 2).sum())
# One row per coffee: water temperature in C, brew time in seconds, caffeine in mg
beans = np.array([[88, 120, 60],
[96, 300, 30],
[86, 90, 70],
[94, 240, 120],
[90, 180, 90],
[84, 60, 50]])
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