AI Hints
Home›Statistics & Math for ML Tutorial›Choosing an Average
Statistics & Math for ML · Lesson 11 of 138

The Weighted Mean

Some count more.

⏱ 2 min read▶ Video at 11:02🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

Your final grade is homework, a midterm, and a final exam. But they don't count equally. The final is worth half your grade. A plain mean would treat them all the same. We need a weighted mean.

The Weighted Mean
From the video: The Weighted Mean
  • Homework, midterm, final
  • They don't count equally
  • A weighted mean

Weights

Each score gets a weight. Twenty percent, thirty percent, and fifty percent. Multiply each score by its weight, and add them up. NP dot average does it for us, with the weights argument. Run it. Seventy nine, compared with eighty for a plain mean.

weighted.pypython
import numpy as np

scores = np.array([90, 70, 80])
weights = np.array([0.2, 0.3, 0.5])

print((scores * weights).sum())
print(np.average(scores, weights=weights))
print(scores.mean())
Output
79.0
79.0
80.0

Weights in machine learning

Weighted means are everywhere in machine learning. A model's prediction is often a weighted sum of its features. And when one class is rare, like fraud, we give those examples a bigger weight, so the model pays attention to them. If the weights don't add up to one, divide by their total.

keyvalue
Weighted meanΣ weight × value ÷ Σ weight
np.averageweights=...
Modelsweighted sums of features
Rare classesget bigger weights

Summary

A weighted mean multiplies each value by how much it counts. It's the idea behind grades, and behind many models. Next, a quick quiz on averages.

Key takeaways
  • Multiply by weights
  • np.average(x, weights=w)
  • Everywhere in ML
Next up: Averages Quiz — test yourself