Descriptive Statistics & Distributions
df.describe(). Mean, median, std — a handful of summary numbers reveal most common data problems: skew, outliers, scale mismatches, broken pipelines. The fanciest deep network can't save you from skipping this step. Statistics isn't homework — it's the first line of defense against silently broken ML.After this lesson, you will be able to:
- Calculate the average, middle value, most common value, and spread of a dataset, and know when to use each one
- Describe the bell curve by its mean and standard deviation, work out a variance by hand, and convert a score into a z-score
- Understand why the bell curve shows up everywhere in nature and AI, thanks to a powerful math rule called the Central Limit Theorem
- Apply the 68-95-99.7 rule to identify outliers and perform z-score standardization, the preprocessing step that makes gradient descent converge faster
Before you start
#Describing What You Have
Statistics is not about math — it is about storytelling with numbers. Every time you check your average screen time, compare your test scores to the class, or wonder if a game's matchmaking is fair, you are already doing statistics. This lesson just makes you precise about it.
This lesson covers the foundational statistics you need before diving into probability, inference, and machine learning. If you can add, subtract, and find a square root, you are ready. Every new word and symbol is explained the first time it appears, and every formula comes after a small example worked with real numbers.
Try it! Open the Python REPL (bottom-right of the screen: click Quick Actions, then Python) and type these lines yourself.
#Measures of Center: Where Is the Middle?
#Mean: The Balance Point
It is the "balance point" of the data: put the three scores on a seesaw and it balances exactly at 80, because 70 is 10 below it and 90 is 10 above it.
Now the formula, which says the same thing in symbols. Write n for how many values you have, and x₁, x₂, … for the values themselves. The mean is written x-bar (a letter x with a bar over it): x-bar is the average of the sample you actually have. The big symbol Σ (capital sigma) simply means "add these up". It is a different symbol from the lowercase σ you will meet shortly.
The mean has a second job besides being the balance point. If you must replace every value by one single number, and you score the replacement by the total squared miss, the mean is the best choice. Here is why, using only sums and algebra.
#Median: The Middle Value
If the mean salary at a company is $500K, does that mean most employees earn around $500K?
The median has its own best-guess property, with a different score. If you score a single replacement number by the total absolute miss instead of the squared miss, the median wins. This also explains why one huge value cannot drag it.
Now see both results move. Drag the guess along the number line, which starts on the values 2, 3, 5, 8 and 100, and watch the squared total bottom out at the mean while the absolute total bottoms out at the median. Then push the largest point to the right: the mean's valley slides away and the median's stays put.
#Mode: The Most Common Value
Which summary statistic should you report for the typical home price in a US city?
#Measures of Spread: How Spread Out Are the Scores?
#Variance and Standard Deviation
We want one number that says "how far, typically, are the values from the mean?" Work it out by hand for the same three scores, 70, 80 and 90, whose mean is 80.
| Score | Distance from the mean (80) | Distance squared |
|---|---|---|
| 70 | 70 − 80 = −10 | 100 |
| 80 | 80 − 80 = 0 | 0 |
| 90 | 90 − 80 = 10 | 100 |
| Total | 0 | 200 |
Look at the middle column: the distances add up to exactly 0, because the mean is the balance point and the pulls on each side cancel. Adding raw distances would always give 0 and tell us nothing. Squaring each distance fixes that: every square is positive, and big distances count for more. The squares add up to 200.
Now average the squares, which means dividing the 200:
- Divide by 3 (the number of scores) and you get 66.7. Use this when your three scores are the whole group you care about, such as every student in the class.
- Divide by 2 (one fewer than the number of scores) and you get 100. Use this when your three scores are only a sample, a part you collected from a bigger group. Almost all ML data is a sample, so this is the one you will use most often. The Bessel's-correction DeepDive below explains the "one fewer".
Now the symbols. Here are four of them in words before you see the formula:
- μ (mu) is the Greek letter for the true average of the whole group.
- x-bar is the average of the sample you actually have. It is our estimate of μ.
- σ (sigma, lowercase) is the true standard deviation of the whole group; σ² is the true variance.
- s is the standard deviation you compute from your sample, and s² is the sample variance.
ddof stands for "delta degrees of freedom", which is just the number you subtract from n before dividing: ddof=1 means divide by n−1.Squaring each distance and summing is awkward to do by hand. A rearrangement lets you get the same variance from the values and their mean, and it is the sample version of the shortcut you will meet again for random variables.
The next derivation shows that dividing by n really does come out too small on average, by exactly the factor (n - 1) / n. It uses the identity from the mean derivation above and one fact about averages, both stated where they are used.
To see the shortfall happen, draw thousands of small samples and watch two averages. Keep n = 3, then try n = 2 and n = 30. The divide-by-n average lands below the true variance, by the factor (n - 1) / n, while the divide-by-(n - 1) average lands on it, and the gap fades as n grows.
#The z-score: Counting in Standard Deviations
A standard deviation gives you a ruler for "how unusual is this value?". Suppose a class has a mean of 80 and a standard deviation of 5, and you scored 90.
- You are 90 − 80 = 10 points above the mean.
- One standard deviation is 5 points, so 10 points is 10 / 5 = 2 standard deviations above the mean.
Standardizing is used so often in ML that it helps to see why it always lands on mean 0 and standard deviation 1.
#The 68-95-99.7 Rule
For data that follows a bell-shaped (normal) distribution, z-scores connect to probabilities through a powerful shortcut:
- About 68% of values have a z-score between −1 and 1, which means they fall within 1 standard deviation of the mean
- About 95% fall within 2 standard deviations
- About 99.7% fall within 3 standard deviations
#Compute the full summary in Python
df.describe() is the first thing a working ML engineer types on a new dataset. The cell below builds the core of that output by hand from a class of test scores: the mean, median, mode, standard deviation and range. It then plots the histogram and overlays the mean, the median and a one-standard-deviation band so you can see whether mean and median diverge. Note ddof=1: it is the "divide by n−1" sample version you just learned.Run as-is and you should see n = 30, mean = 81.133, median = 81.500, mode = 88 (appears 2 times), std = 8.982 and a range of 33 (63 to 96). Mean and median are close, so the scores are roughly symmetric. Now append 5, 8 and 12 and re-run: the mean falls to 74.515 while the median only slips to 80.0.
#Probability Distributions: The Shape of Randomness
#Normal (Gaussian) Distribution: The Bell Curve
Nobody evaluates this formula by hand. It is here so you can see that μ and σ are the only two dials. Why does the normal distribution show up everywhere? Heights, test scores, measurement errors, stock returns (roughly) — the answer is the Central Limit Theorem, which we will get to shortly.
The 68-95-99.7 areas were stated earlier. Why do they not depend on the particular mean and standard deviation of the data? Because everything reduces to the z-score, and there is a short derivation of that.
#Explore the bell curve and its cousins
The viz below shows Normal, Binomial and Poisson side by side. Pick a tab, move the sliders, and read the Mean, Variance and Std Dev tiles under the chart.
#Interactive: Explore Distributions
This second explorer offers four other shapes: Normal, Uniform, Exponential and Beta. Pick one with the Distribution menu; each has its own sliders.
#The Central Limit Theorem: Why Normal Is Normal
#Step 1: Start With Any Distribution
Take any distribution — flat, lopsided, even a completely weird custom one. It does not matter what shape it is. The Central Limit Theorem works on all of them.
#Step 2: Take Random Samples
Draw a random sample of n values from that distribution. Compute the mean of your sample. This is one "sample mean." It is a single number — the average of your random draw.
#Step 3: Repeat Many Times
Do this again and again. Each time, draw n fresh values and compute the sample mean. After hundreds or thousands of repetitions, you have a collection of sample means.
#Step 4: The Magic
See it happen. Pick a skewed source in the viz below, draw batches of 30 values, and watch the batch averages pile into a bell shape even though the source is lopsided.
#How Descriptive Statistics Power ML
#Data Understanding
Before training any model, you compute descriptive statistics on every feature: mean, median, standard deviation, min and max. These numbers tell you whether features are on the same scale (if not, you need normalization), whether there are outliers (which might need clipping), and whether the mean and median agree (a first hint that a feature is roughly symmetric, which some algorithms assume).
#Feature Scaling
#Anomaly Detection
Values more than 3 standard deviations from the mean are rare (the 68-95-99.7 rule). This is the simplest anomaly detector: compute the mean and standard deviation, flag anything beyond 3 sigmas. Credit card fraud detection, server monitoring, and quality control all start here.
#Model Assumptions
Linear regression assumes normally distributed errors. Naive Bayes often assumes Gaussian features. Understanding what distribution your data follows, and what happens when it does not — is the difference between a model that works and one that silently fails.
#Try It Yourself
Now compute everything from this lesson on the 30 class scores, with no library hiding the arithmetic. Fill in the TODOs, run, and compare with the solution. Note that the standard deviation must divide by n−1, because these 30 scores are a sample.
Tests: Verify the printed values: Mean 81.13, Median 81.50, Std Dev (n-1) 8.98, matches np.std(ddof=1) True, Std Dev with n 8.83, within 1 std 19 of 30 (63.3%), within 2 std 29 of 30 (96.7%), z of 96 is 1.66, z of 63 is -2.02, and with a 15 the mean is 79.00 (moved -2.13) while the median is 81.00 (moved -0.50).
np.std(scores) with its default. Of the 30 scores, 19 (63.3%) sit within 1 standard deviation and 29 (96.7%) within 2: a little under 68% and a little over 95%, which is about what the rule predicts for 30 values that are only roughly bell-shaped. The top score, 96, has z = 1.66 and the bottom score, 63, has z = −2.02, so the lowest score just sits outside the 2-standard-deviation band. Finally, one score of 15 drags the mean down by 2.13 (to 79.00) but moves the median by only 0.50 (to 81.00).#Key Takeaways
- Mean, median, and mode measure the center differently. The mean is sensitive to outliers, the median is robust, and the mode identifies the most common value; always check whether mean and median diverge, which signals skewed data
- Variance is the average squared distance from the mean (divide by n−1 for a sample), and standard deviation is its square root. It measures consistency: small means tightly clustered data, large means widely spread
- A z-score counts how many standard deviations a value is above or below the mean: z = (x − mean) / std
- For bell-shaped data the 68-95-99.7 rule gives instant intuition: about 68%, 95% and 99.7% of values fall within 1, 2 and 3 standard deviations of the mean. It applies only to normal distributions
- The normal distribution is defined by its mean and standard deviation, and the Central Limit Theorem explains why it is everywhere: averages of many independent values form a bell curve whatever shape the individual values had
#Quick Check
Three scores are 2, 4 and 6 (mean 4). Treating them as a sample, what is the standard deviation?