Python Central HubSign in

Data Analytics with Python / Phase 8: Statistics for Data Analytics

Descriptive Statistics (mean, median, variance)

Summarize data using central tendency and spread; learn when each metric is appropriate.

Central tendency

Mean

  • Sensitive to outliers
  • Good for symmetric distributions
Python
import numpy as np
 
x = np.array([10, 12, 12, 13, 12, 11, 100])
print(np.mean(x))

Median

  • Robust to outliers
Python
import numpy as np
 
x = np.array([10, 12, 12, 13, 12, 11, 100])
print(np.median(x))

Mode

Useful for categorical data.

Python
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)
Python
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.

Python
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

diagram Choosing a central tendency measure mermaid
A quick decision path for picking mean, median, or 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:

sketch Mean vs. median on a number line p5.js
The mean shifts toward the outlier; the median stays anchored near the middle of the data.

Check your understanding

quizCheck yourself
  1. For x = 10, 12, 12, 13, 12, 11, 100, which is closest to the mean and median?

    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.

  2. Why does the lesson call ddof=1 in np.var and np.std?

    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.

  3. For that same data, what is the IQR from np.percentile at 25 and 75?

    Show answer

    A โ€” 1.0 โ€” Q1 is 11.5 and Q3 is 12.5, so the IQR is 1.0, which ignores the outlier.

  4. Which pair does the checklist recommend when outliers exist?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Variance and standard deviation

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ IQR to spot spread

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Next

Continue to Probability Basics to build the foundation for confidence intervals and hypothesis tests.

Was this page helpful?

Let us know how we did