Menu
CoddyTech

Z-Scores

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 11 of 64.

A student scored 78 in chemistry and 85 in history. Which is better? Measure each score in its own class's std:

Two number lines marked in std steps. Chemistry, mean 70 and std 4: the score 78 sits 2 std above the mean. History, mean 80 and std 10: the score 85 sits only 0.5 std above

That distance is the z-score, z = (x - mean) / std. Chemistry: (78 - 70) / 4 is 2.0. History: (85 - 80) / 10 is 0.5. The 78 wins.

On a whole array, a negative z means below the mean:

temps = np.array([12, 15, 9, 18, 16])
print(np.round((temps - temps.mean()) / temps.std(), 2))

This prints:

[-0.63  0.32 -1.58  1.26  0.63]

Z-scores share one scale, mean 0 and std 1, whatever the unit. Age and income then weigh alike in a model, however big their numbers.

challenge icon

Challenge

Easy

An arcade's puzzle and racing games score on very different scales. puzzle and racing hold 12 regulars' scores on each. A newcomer played both: the program reads their puzzle score, then their racing score.

  1. Print each score's z-score against its game, rounded to 2 decimals (use .std()).
  2. Print which result is more impressive.

For the input 790 and 16900:

Puzzle z: 1.45
Racing z: 2.0
More impressive: Racing

Try it yourself

import numpy as np

puzzle = np.array([640, 720, 580, 810, 690, 750, 600, 700, 660, 730, 620, 780])
racing = np.array([12400, 15800, 13900, 11200, 16500, 14100,
                   12800, 15100, 13300, 14700, 12100, 14600])
p = int(input())
r = 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