The Weighted Mean
Some count more.
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.

- 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.
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())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.
| key | value |
|---|---|
| Weighted mean | Σ weight × value ÷ Σ weight |
| np.average | weights=... |
| Models | weighted sums of features |
| Rare classes | get 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