Session 25 ยท Phase 4: Statistics & Visualization

Descriptive Statistics & Outliers

The numbers that summarize a dataset โ€” center, spread, and shape โ€” and how to spot the outliers that distort them.

โฑ ~2 hrs ๐Ÿ“š Core content ๐ŸŽฏ High priority

Learning Objectives

1. Descriptive vs Inferential Statistics

DescriptiveInferential
Purposesummarize & describe the data you havedraw conclusions about a population from a sample
Examplesmean, median, chartshypothesis tests, confidence intervals

This session focuses on descriptive statistics โ€” the "what does my data look like?" toolkit.

2. Mean, Median, Mode โ€” the Center

MeasureWhat it isBest when
Meanaverage (sum รท count)data is roughly symmetric, no big outliers
Medianmiddle valuedata is skewed or has outliers
Modemost frequent valuecategorical data
import numpy as np
data = [10, 20, 30, 40, 1000]   # 1000 is an outlier

np.mean(data)    # 220  โ€” pulled way up by the outlier
np.median(data)  # 30   โ€” robust, ignores the outlier
โš ๏ธ
The key idea: the mean is sensitive to outliers; the median is robust. When data is skewed, the median is usually the better "typical value" โ€” a favourite interview point.

3. Measures of Spread

MeasureWhat it is
Rangemax โˆ’ min
Varianceaverage of squared deviations from the mean
Standard deviationsquare root of variance โ€” spread in the same units as the data
IQRQ3 โˆ’ Q1 (middle 50% of data)
np.std(data)    # standard deviation
np.var(data)    # variance

Standard deviation is the most-used: a small SD means data clusters tightly around the mean; a large SD means it's spread out.

4. Skewness

Skewness describes whether a distribution leans to one side:

Income data is a classic right-skewed example โ€” a few very high earners pull the mean above the median.

5. Outliers โ€” What They Are

An outlier is a value that is significantly different from the rest โ€” far enough to distort your statistics. Detecting them is a core analyst skill.

Method 1 โ€” IQR (Interquartile Range)

Q1 = np.percentile(data, 25)
Q3 = np.percentile(data, 75)
IQR = Q3 - Q1
lower = Q1 - 1.5 * IQR
upper = Q3 + 1.5 * IQR
outliers = [x for x in data if x < lower or x > upper]

Any value below Q1 โˆ’ 1.5ร—IQR or above Q3 + 1.5ร—IQR is an outlier. The IQR method is robust because it uses quartiles, not the mean.

Method 2 โ€” Z-score

mean = np.mean(data)
std = np.std(data)
z_scores = [(x - mean) / std for x in data]
outliers = [x for x, z in zip(data, z_scores) if abs(z) > 3]

A z-score tells you how many standard deviations a value is from the mean. A common threshold is |z| > 3.

๐Ÿ“
IQR vs z-score: IQR is robust to skewed data (uses quartiles); z-score assumes the data is roughly normal (uses mean/std, which outliers themselves distort). For skewed data, prefer IQR.

6. Handling Outliers

Once detected, you have options โ€” and the right one depends on context:

โš ๏ธ
Never delete blindly. An outlier might be your most important data point โ€” a fraudulent transaction or a whale customer. Understand why it's there before you touch it.
๐Ÿ“‹ Stable content โ€” Reviewed: August 2026

7. Interview Questions (with Model Answers)

The descriptive statistics questions interviewers ask. Self-test before revealing.

IQ1. What's the difference between descriptive and inferential statistics?

Model answer: "Descriptive statistics summarize the data you have โ€” mean, median, charts. Inferential statistics draw conclusions about a larger population from a sample โ€” like hypothesis tests."

IQ2. What's the difference between mean, median, and mode, and when do you use each?

Model answer: "Mean is the average, median the middle value, mode the most frequent. I use mean for symmetric data, median when data is skewed or has outliers, and mode for categorical data."

IQ3. Why is the median more robust to outliers than the mean?

Model answer: "Because the mean sums every value, one extreme can drag it far away. The median only cares about the middle position, so outliers barely affect it."

IQ4. Explain variance and standard deviation.

Model answer: "Both measure spread. Variance is the average of squared deviations from the mean; standard deviation is its square root, so it's in the same units as the data and easier to interpret."

IQ5. What is skewness?

Model answer: "Skewness measures asymmetry. Right-skewed data has a long right tail and the mean above the median; left-skewed has the opposite. Income is a classic right-skewed example."

IQ6. What is an outlier, and how do you detect one?

Model answer: "An outlier is a value far from the rest that can distort statistics. I detect them with the IQR method (values beyond Q1โˆ’1.5ร—IQR or Q3+1.5ร—IQR) or z-scores (|z| > 3), plus boxplots visually."

IQ7. Explain the IQR method for outlier detection.

Model answer: "Compute Q1 and Q3, then IQR = Q3 โˆ’ Q1. Any value below Q1 โˆ’ 1.5ร—IQR or above Q3 + 1.5ร—IQR is flagged. It's robust because it's based on quartiles, not the mean."

IQ8. Explain the z-score method for outlier detection.

Model answer: "A z-score is how many standard deviations a value is from the mean. Values with |z| above a threshold (often 3) are outliers. It assumes roughly normal data, since the mean and SD can be distorted by outliers themselves."

IQ9. How do you handle outliers once you find them?

Model answer: "First I investigate whether it's an error or a real extreme. Then I might remove it, cap it, or log-transform โ€” or keep it if it's meaningful. I never delete blindly."

Hands-On Project: Describe and Detect Outliers

Using a list of sales values, compute descriptive statistics and detect outliers.

Steps

  1. Create sales = [100, 120, 110, 130, 115, 900] (900 is an outlier).
  2. Compute the mean and median.
  3. Compute the standard deviation.
  4. Find outliers using the IQR method.
  5. Find outliers using the z-score method (|z| > 3).
  6. Explain which method is better here and why.
View Solution / Walkthrough
import numpy as np
sales = [100, 120, 110, 130, 115, 900]

print(np.mean(sales))    # 245.8  โ€” pulled up by 900
print(np.median(sales))  # 117.5  โ€” robust
print(np.std(sales))     # large, due to the outlier

# IQR method
Q1, Q3 = np.percentile(sales, 25), np.percentile(sales, 75)
IQR = Q3 - Q1
lower, upper = Q1 - 1.5 * IQR, Q3 + 1.5 * IQR
iqr_outliers = [x for x in sales if x < lower or x > upper]  # [900]

# z-score method
mean, std = np.mean(sales), np.std(sales)
z_outliers = [x for x in sales if abs((x - mean) / std) > 3]  # [900]

Which is better? With a single big outlier, the IQR method is more reliable โ€” the z-score method's mean and SD are themselves distorted by the 900, which can mask the outlier.

Key Takeaways

1

Mean is sensitive to outliers; median is robust โ€” use median for skewed data.

2

Standard deviation = spread in the data's own units.

3

Right-skewed โ†’ mean > median; left-skewed โ†’ mean < median.

4

IQR method is robust; z-score assumes roughly normal data.

5

Never delete outliers blindly โ€” investigate the cause first.

Objective Questions โ€” Test Your Understanding

Q1. Which measure of central tendency is most affected by outliers?

Q2. Which measure is most robust to outliers?

Q3. What does standard deviation measure?

Q4. In the IQR method, an outlier is above Q3 + 1.5ร—IQR or belowโ€ฆ

Q5. What does a z-score of +2 mean?