AI Hints
Statistics & Math for ML · Lesson 19 of 138

Histograms

The shape of data.

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

Numbers like the mean and standard deviation are summaries. But to really understand a column, you need to see it. The most useful picture is a histogram. It shows how many values fall into each range.

Histograms
From the video: Histograms
  • Summaries hide details
  • See the data
  • A histogram

Choosing the bins

A histogram splits the number line into bins, and draws a bar for how many values land in each. The number of bins matters. On the left, two bins tell us almost nothing. On the right, forty bins show the real shape. Run it. This data actually has two bumps, which two bins completely hid.

bins.pypython
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(7)
x = np.concatenate([rng.normal(40, 6, 600), rng.normal(70, 6, 400)])

fig, ax = plt.subplots(1, 2, figsize=(9, 4.5))
ax[0].hist(x, bins=2); ax[0].set_title("2 bins")
ax[1].hist(x, bins=40); ax[1].set_title("40 bins")
plt.tight_layout(); plt.savefig("bins.png")

Reading a histogram

Look for four things. Where's the centre? How wide is the spread? Is it symmetric, or does it have a long tail? And how many bumps, or peaks, does it have? Two peaks often means two different groups are mixed together, like two kinds of customers.

keyvalue
Centrewhere's the middle?
Spreadhow wide?
Shapesymmetric or tailed?
Peaksone group or two?

Summary

A histogram shows the shape of your data. Try a few bin counts, and look for the centre, spread, tails, and peaks. Next, skewness, and long tails.

Key takeaways
  • plt.hist(x, bins=...)
  • Try different bins
  • Look for peaks and tails
Next up: Skewness — long tails