Menu
CoddyTech

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:

Four customers' ages and incomes as a table, an arrow, then the same table scaled: in each column the smallest value became 0.00 and the largest 1.00, shaded darker, the others in between

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:

The scaled customers as dots on a square from 0 to 1, with lines from customer 0: the 26-year-old is nearest at 0.11, the 61-year-old 1.00 away, the $80,000 earner 1.08

One extreme value becomes 1 and squeezes the rest near 0, so min-max suits columns without outliers.

challenge icon

Challenge

Medium

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

  1. Min-max scale beans.
  2. Print Every column from 0 to 1:, then the scaled table to 2 decimals.
  3. 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 after

Try 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
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