Python Central HubSign in

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

t-test (independent and paired)

Compare means between groups using independent and paired t-tests with practical examples.

When to use t-tests

Use t-tests when you want to compare means.

  • Independent (two-sample): two different groups
  • Paired: before/after measurements on the same subjects

Independent two-sample t-test

Example: average spend differs between two groups.

Python
import numpy as np
from scipy import stats
 
A = np.array([120, 140, 130, 150, 160])
B = np.array([100, 90, 110, 95, 105])
 
t_stat, p = stats.ttest_ind(A, B, equal_var=False)  # Welch t-test
print("t:", t_stat)
print("p:", p)

Welch vs pooled variance

  • Welch (equal_var=False) is safer when variances differ.
  • Pooled assumes equal variances.

Paired t-test

Example: same users before/after a change.

Python
import numpy as np
from scipy import stats
 
before = np.array([10, 12, 11, 9, 13])
after  = np.array([11, 12, 12, 10, 14])
 
t_stat, p = stats.ttest_rel(after, before)
print("t:", t_stat)
print("p:", p)

Assumptions (important)

  • Data points are independent (except paired case)
  • Approximately normal within each group (CLT helps with larger n)
  • No extreme outliers

If assumptions are shaky, consider non-parametric alternatives.

Which t-test do you need?

diagram Choosing between t-tests mermaid
The decision hinges on whether the two groups share the same subjects.

Check your understanding

quizCheck yourself
  1. When should you use a paired t-test?

    Show answer

    B — Before and after measurements on the same subjects — Paired data has the same users measured twice, which is stats.ttest_rel in the lesson.

  2. What does equal_var=False in stats.ttest_ind do?

    Show answer

    A — Runs the Welch t-test, which is safer when variances differ — Welch does not assume equal variances. The pooled test does.

  3. Group A has values 120 to 160 (mean 140) and B has 90 to 110 (mean 100). What does the independent t-test compare?

    Show answer

    A — The two means, 140 against 100 — t-tests compare means. Here the difference of 40 is judged against the spread within each group.

  4. Which assumption belongs on the t-test list?

    Show answer

    A — Approximate normality within each group, with no extreme outliers — The CLT helps with larger n, and shaky assumptions point to non-parametric alternatives.

🧪 Try It Yourself

Exercise 1 – Independent (Welch) t-test

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 – Paired t-test (before/after)

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 – Picking the right test

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 ANOVA (one-way) to compare means across three or more groups at once.

Was this page helpful?

Let us know how we did