Central tendency
Mean
- Sensitive to outliers
- Good for symmetric distributions
import numpy as np
x = np.array([10, 12, 12, 13, 12, 11, 100])
print(np.mean(x))Median
- Robust to outliers
import numpy as np
x = np.array([10, 12, 12, 13, 12, 11, 100])
print(np.median(x))Mode
Useful for categorical data.
import numpy as np
from scipy import stats
x = np.array([1, 1, 2, 2, 2, 3])
print(stats.mode(x, keepdims=True))Spread (variability)
- Range: max - min (very sensitive)
- Variance: average squared distance from mean
- Standard deviation (std): sqrt(variance)
import numpy as np
x = np.array([10, 12, 12, 13, 12, 11, 100])
print("var:", np.var(x, ddof=1))
print("std:", np.std(x, ddof=1))IQR (interquartile range)
Robust measure of spread.
import numpy as np
x = np.array([10, 12, 12, 13, 12, 11, 100])
q1 = np.percentile(x, 25)
q3 = np.percentile(x, 75)
print("IQR:", q3 - q1)Quick checklist
- Use median/IQR when outliers exist
- Use mean/std when distribution is roughly symmetric
- Always visualize (histogram/boxplot) before trusting summary stats
How mean and median relate
flowchart TD A["Do you have outliers
or a skewed shape?"] -->|"Yes"| B["Use median
(robust)"] A -->|"No"| C["Use mean
(uses every value)"] D["Is the data categorical?"] --> E["Use mode"]
Visualize it
The mean is pulled toward extreme values because it uses every number in the calculation. The median only cares about the middle position, so it barely moves when one point is far away. Watch the outlier drag the mean marker to the right while the median stays put:
Check your understanding
For x = 10, 12, 12, 13, 12, 11, 100, which is closest to the mean and median?
The sum is 170, so the mean is 170 / 7, about 24.3. The sorted middle value is 12, and the outlier 100 drags only the mean.
Show answer
B โ Mean about 24.3, median 12 โ The sum is 170, so the mean is 170 / 7, about 24.3. The sorted middle value is 12, and the outlier 100 drags only the mean.
Why does the lesson call ddof=1 in np.var and np.std?
ddof=1 gives the sample variance and standard deviation, which is the usual choice when estimating from a sample.
Show answer
B โ It uses the sample formula, dividing by n - 1 โ ddof=1 gives the sample variance and standard deviation, which is the usual choice when estimating from a sample.
For that same data, what is the IQR from np.percentile at 25 and 75?
Q1 is 11.5 and Q3 is 12.5, so the IQR is 1.0, which ignores the outlier.
Show answer
A โ 1.0 โ Q1 is 11.5 and Q3 is 12.5, so the IQR is 1.0, which ignores the outlier.
Which pair does the checklist recommend when outliers exist?
Median and IQR are robust. Mean and std suit roughly symmetric data. Range is very sensitive.
Show answer
C โ Median and IQR โ Median and IQR are robust. Mean and std suit roughly symmetric data. Range is very sensitive.
๐งช Try It Yourself
Exercise 1 โ Mean vs. median with an outlier
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Variance and standard deviation
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ IQR to spot spread
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Next
Continue to Probability Basics to build the foundation for confidence intervals and hypothesis tests.