Python Central HubSign in

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

ANOVA (one-way)

Compare means across 3+ groups using one-way ANOVA and interpret results correctly.

Why ANOVA

If you compare 3+ groups with many t-tests, you increase false positives.

ANOVA tests:

  • H0: all group means are equal
  • H1: at least one mean differs

Example

Python
import numpy as np
from scipy import stats
 
A = np.array([10, 12, 11, 13, 12])
B = np.array([7, 8, 9, 8, 7])
C = np.array([14, 15, 13, 16, 14])
 
f_stat, p = stats.f_oneway(A, B, C)
print("F:", f_stat)
print("p:", p)

If ANOVA is significant

ANOVA says “some difference exists” but not where.

Next steps:

  • Post-hoc tests (e.g., Tukey HSD)
  • Pairwise comparisons with correction

Assumptions

  • Independence
  • Normality within groups (approx)
  • Homogeneity of variances

If variances differ a lot, consider Welch ANOVA or non-parametric alternatives.

Why not just run many t-tests?

diagram ANOVA vs. repeated t-tests mermaid
Running many pairwise t-tests inflates false positives; ANOVA tests all groups at once.

Check your understanding

quizCheck yourself
  1. What is the null hypothesis of one-way ANOVA?

    Show answer

    B — All group means are equal — H0 says all group means are equal and H1 says at least one differs.

  2. Why not run many t-tests for three or more groups?

    Show answer

    B — Many tests increase false positives — Each extra test adds a chance of a false positive, which ANOVA avoids with a single test.

  3. ANOVA on groups A, B and C gives a small p-value. What do you know?

    Show answer

    B — Some difference exists, but not where — ANOVA does not say where the difference is. Follow with post-hoc tests such as Tukey HSD or corrected pairwise comparisons.

  4. Which scipy function runs the one-way ANOVA in the lesson?

    Show answer

    D — stats.f_oneway — stats.f_oneway(A, B, C) returns the F statistic and p-value.

🧪 Try It Yourself

Exercise 1 – Run a one-way ANOVA

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 – Reading the result

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 – Group means and variances

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 Chi-Square Test to compare categorical variables instead of numeric means.

Was this page helpful?

Let us know how we did