Menu
CoddyTech

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:

Two bars of what each feature adds to the distance between the two customers: age, a 36 year gap, adds 1,296, a sliver too thin to see; income, a $1,000 gap, adds 1,000,000, a long bar

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.  ]
0

The 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 icon

Challenge

Medium

A 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:

  1. Squared gaps: each feature's gap, squared.
  2. Distance: rounded to 2 decimals.
  3. 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: energy

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler