Lesson 4 of 18

Z-Scores

Standardization

A z-score measures how many standard deviations a value is from the mean:

z = rac{x - mu}{sigma}

import statistics

data = [1, 2, 3, 4, 5]
mean = statistics.fmean(data)
pstdev = statistics.pstdev(data)  # population std (ddof=0)
z = [(x - mean) / pstdev for x in data]
print([round(v, 4) for v in z])
# [-1.4142, -0.7071, 0.0, 0.7071, 1.4142]

Properties

A standardized dataset always has:

  • Mean = 0
  • Standard deviation = 1

Interpreting Z-Scores

Z-scoreMeaning
z=0z = 0Exactly at the mean
z=1z = 1One sigmasigma above the mean
z=2z = -2Two sigmasigma below the mean
$z

Use Cases

  • Compare values from different scales (e.g., height and weight)
  • Detect outliers
  • Normalize features before machine learning

Your Task

Implement z_scores(data) that returns the z-scores of the data as a list (using population standard deviation).

Pyodide loading...
Loading...
Click "Run" to execute your code.