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.
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:
| Rating | Frequency | Relative | Percent |
|---|---|---|---|
| 1 ★ | 2 | 0.10 | 10% |
| 2 ★ | 3 | 0.15 | 15% |
| 3 ★ | 5 | 0.25 | 25% |
| 4 ★ | 6 | 0.30 | 30% |
| 5 ★ | 4 | 0.20 | 20% |
| Total | 20 | 1.00 | 100% |
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.
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) | Boundaries | Freq (f) | Relative | Cumulative | Cum. relative |
|---|---|---|---|---|---|
| 50–59 | 49.5–59.5 | 4 | 0.10 | 4 | 0.10 |
| 60–69 | 59.5–69.5 | 10 | 0.25 | 14 | 0.35 |
| 70–79 | 69.5–79.5 | 14 | 0.35 | 28 | 0.70 |
| 80–89 | 79.5–89.5 | 8 | 0.20 | 36 | 0.90 |
| 90–99 | 89.5–99.5 | 4 | 0.10 | 40 | 1.00 |
| Total | 40 | 1.00 |
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.
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.
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.
Each value xi carries a weight wi. Equal weights give back the ordinary 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.
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.
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.
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.
| Idea | Where it shows up in ML | Why |
|---|---|---|
| Binning / discretization | KBinsDiscretizer, and how decision trees split a numeric feature | Bucketing a continuous feature can expose non-linear patterns a linear model would miss |
| Frequency encoding | Replacing a category with how often it appears | A compact numeric stand-in for a high-cardinality categorical, when one-hot would explode |
| Class frequency | Detecting imbalanced target classes before training | A frequency table of the label is the first check for the imbalance that skews a classifier |
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.
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.
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 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).
Practice Challenges
Five short challenges, beginner to intermediate. Try them on paper or in Python before checking the solutions.
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.
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.
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?
np.average(scores, weights=...); weights sum to 1 here.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.
np.average(mids, weights=freq).Grouped median
For the same table, find the median with L + ((n/2 − CF)/f)·h. Be careful to use class boundaries.
A fully-worked solutions notebook walks through all five challenges in the same visual style. Try them yourself first, then compare.
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.