Contents/ Part II · Describing Data/ Chapter 13

Frequency Distributions

Before you can chart data or model it, you count it. The frequency table is the quiet workhorse behind every histogram, and the bridge from raw numbers to probability.

⏱️ ~12 min read
🐍 Notebook included
📊 Chapter 13

Hand someone 500 exam scores and they see noise. Sort those scores into a few ranges and count how many land in each, and a picture appears. That table of counts is a frequency distribution.

f
A frequency distribution pairs each value, or each class of values, with its frequency: the count of how often it occurs.
1

Grouped vs. Ungrouped

Which kind of table you build depends on how many distinct values you have.

Ungrouped

Lists every distinct value with its count. Best for discrete data with few values: dice rolls, star ratings, number of children.

Grouped

Bins values into class intervals and counts each bin. Best for continuous data or many distinct values: heights, incomes, test scores.

Here is a small ungrouped table for 20 customer star-ratings, with a relative-frequency column (count divided by the total) that must sum to 1:

RatingFrequencyRelativePercent
1 ★20.1010%
2 ★30.1515%
3 ★50.2525%
4 ★60.3030%
5 ★40.2020%
Total201.00100%
2

Building a Grouped Table

Grouping continuous data into classes brings a small vocabulary. The one trap worth slowing down for: class limits are the values you write down, while class boundaries close the half-unit gap between classes. Boundaries, not limits, are what you use for histogram edges and ogives.

9.5 19.5 class boundaries (close the gap) 10 19 class limits (what you record) 14.5 midpoint class width = 10
Anatomy of the class "10–19": limits 10 and 19, boundaries 9.5 and 19.5, midpoint 14.5, width 10.
📏
How many classes, how wide?

Aim for roughly 5 to 20 classes. A common guideline is Sturges' rule, k = 1 + 3.322·log₁₀(n). Then set class width = range / number of classes, rounded up (rounding down can leave the largest value with no home). Classes should be equal-width, non-overlapping, and cover every value.

Sturges' rule assumes roughly normal data and tends to use too few classes for large datasets. Robust alternatives like Freedman-Diaconis (which uses the IQR) often work better, the same bin-width judgment call you met with histograms in the Shape of a Distribution chapter.

Add the running columns and the table tells a fuller story. Cumulative frequency is a running total; cumulative relative frequency is that total over n, and it must end at 1.0:

Class (score)BoundariesFreq (f)RelativeCumulativeCum. relative
50–5949.5–59.540.1040.10
60–6959.5–69.5100.25140.35
70–7969.5–79.5140.35280.70
80–8979.5–89.580.20360.90
90–9989.5–99.540.10401.00
Total401.00
3

The Ogive: Cumulative Frequency

Plot the cumulative-frequency column against the class boundaries and you get an ogive (say "OH-jive"). The less-than ogive rises; the more-than ogive falls. Where they cross is the median, the value with half the data on each side.

nn/20 median less-than (rises to n) more-than (falls) class boundaries →
To read a quartile, find n/4, n/2, or 3n/4 on the cumulative axis, cross to the rising ogive, then drop to the value below.
📍
Plot at boundaries, not midpoints

This is the classic ogive error. The less-than ogive is plotted at each class's upper boundary (and starts at the first lower boundary with a height of 0). Midpoints are for the frequency polygon, a different graph.

A relative-frequency ogive (cumulative relative frequency on the y-axis, ending at 1.0) is the empirical version of a cumulative distribution function, which you will meet properly in the probability chapters.

4

The Weighted Mean

A plain mean treats every value equally. A weighted mean lets some values count more than others, and it is exactly what a grouped table computes under the hood.

w  =  Σ wi xiΣ wi

Each value xi carries a weight wi. Equal weights give back the ordinary mean.

🎓
GPA is a weighted mean

Grades weighted by credit hours: an A (4.0) in a 4-credit course and a C (2.0) in a 1-credit course do not count equally. With grade points 4, 3, 4, 2 and credits 4, 4, 3, 1, the GPA is Σ(w·x)/Σw = 42 / 12 = 3.50, not the unweighted 3.25.

🔗
The grouped mean is a weighted mean of midpoints

Once data is grouped you have lost the exact values, so you estimate the mean by treating every value in a class as its midpoint, weighted by the class frequency: mean ≈ Σ(f·m) / Σf. The grouped median works the same spirit by interpolation: L + ((n/2 − CF) / f)·h, where L is the lower boundary of the median class, CF is the cumulative frequency before it, f its frequency, and h the width.

🤖
Why this matters for data science

Frequency tables and binning are the data structure behind every histogram and the first step of exploratory analysis (value_counts, pd.cut). Grouping trades exact detail for readability, so grouped statistics are approximations. And a relative-frequency table is an empirical probability distribution, the hands-on preview of the probability chapters ahead.

5

Frequency & Binning in Machine Learning & AI

Grouping values into classes is not just for tidy tables. The same idea, turning a continuous variable into counted buckets, is a standard feature-engineering move, and frequency itself becomes a feature.

IdeaWhere it shows up in MLWhy
Binning / discretizationKBinsDiscretizer, and how decision trees split a numeric featureBucketing a continuous feature can expose non-linear patterns a linear model would miss
Frequency encodingReplacing a category with how often it appearsA compact numeric stand-in for a high-cardinality categorical, when one-hot would explode
Class frequencyDetecting imbalanced target classes before trainingA frequency table of the label is the first check for the imbalance that skews a classifier
🤖
A histogram is a density estimate

The frequency histogram in this chapter is the simplest form of density estimation, an approximation of the underlying distribution from a sample. Its smoother cousin, the kernel density estimate, is the same idea refined, and both are everywhere in exploratory analysis and probabilistic modeling.

6

Real-World Example: Binning Customer Ages

A frequency distribution turns a list of 500 raw ages into a story you can read at a glance. Grouped into ten-year bands, the biggest group is customers in their thirties (about 30%), and the cumulative column shows how quickly the customer base ages. The companion notebook builds the full frequency table, counts, relative frequency, and cumulative frequency, and plots the histogram.

📂 Dataset · frequency-distributions--customer_ages.xlsx

One row per customer: customer_id, age in years (the variable to bin), and membership tier (Basic, Plus, Premium) for a categorical count. Grouping the ages into classes is what turns raw values into a readable frequency distribution.

🐍

Bring it to life in Python

The companion notebook builds ungrouped and grouped frequency tables in pandas, adds the relative and cumulative columns, draws a histogram, frequency polygon, and ogive side by side, and computes weighted and grouped means, comparing the grouped estimate to the true mean.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly (no setup). Open in Colab runs & edits it live in your browser. To run locally, install numpy, pandas, matplotlib and launch jupyter notebook.

🎓 Key Takeaways

  • A frequency distribution pairs values or classes with counts; use ungrouped for few values, grouped for continuous data.
  • Class limits are recorded values; class boundaries close the gap and drive histograms and ogives.
  • Relative frequency sums to 1; cumulative relative frequency ends at 1.0, a built-in check.
  • The ogive plots cumulative frequency at boundaries; less-than and more-than ogives cross at the median.
  • The weighted mean Σ(w·x)/Σw powers GPA and the grouped mean (a weighted mean of midpoints, hence approximate).
7

Practice Challenges

Five short challenges, beginner to intermediate. Try them on paper or in Python before checking the solutions.

1

Build a table

For [3, 5, 3, 2, 5, 5, 3, 4, 2, 5], build a frequency table with a relative-frequency column, and confirm the relative column sums to 1.

Hint: count each value, divide by n = 10.
2

Class anatomy

A table has classes 0–9, 10–19, 20–29, 30–39 with frequencies 4, 11, 9, 6. Find the class width, each midpoint, and the cumulative-frequency column.

Hint: width = gap between consecutive lower limits; midpoint = (lower + upper)/2.
3

Weighted grade

A course is 20% homework, 30% midterm, 50% final. A student earns 95, 78, and 88 on those. What is the weighted course grade?

Hint: np.average(scores, weights=...); weights sum to 1 here.
4

Grouped mean

Using the table from Challenge 2 (midpoints 4.5, 14.5, 24.5, 34.5; frequencies 4, 11, 9, 6), estimate the mean of the grouped data.

Hint: mean ≈ Σ(f·m)/Σf, i.e. np.average(mids, weights=freq).
5

Grouped median

For the same table, find the median with L + ((n/2 − CF)/f)·h. Be careful to use class boundaries.

Hint: n/2 = 15 lands in 10–19; L = 9.5 (the boundary), CF = 4, f = 11, h = 10.
Check your work

A fully-worked solutions notebook walks through all five challenges in the same visual style. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
8

Quiz: Test Yourself

Eight quick questions on frequency distributions. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.