Tutorials

One-Way ANOVA with SciPy

One-way ANOVA is used when you want to compare the means of three or more independent groups. Instead of running many pairwise t-tests, ANOVA gives you a single overall test for whether at least one group mean differs from the others.

### Basic One-Way ANOVA

import numpy as np
from scipy import stats

np.random.seed(10)

group_a = np.random.normal(loc=68, scale=4, size=30)
group_b = np.random.normal(loc=73, scale=4, size=30)
group_c = np.random.normal(loc=79, scale=4, size=30)

f_statistic, p_value = stats.f_oneway(group_a, group_b, group_c)

print(f"Mean of group A: {group_a.mean():.2f}")
print(f"Mean of group B: {group_b.mean():.2f}")
print(f"Mean of group C: {group_c.mean():.2f}")
print(f"F-statistic: {f_statistic:.3f}")
print(f"p-value: {p_value:.6f}")
Mean of group A: 68.79
Mean of group B: 73.47
Mean of group C: 78.71
F-statistic: 47.591
p-value: 0.000000
- **`stats.f_oneway(...)`** tests the null hypothesis that all group means are equal.
- A small p-value suggests at least one group differs.

### Interpreting the Result

import numpy as np
from scipy import stats

np.random.seed(10)

group_a = np.random.normal(loc=68, scale=4, size=30)
group_b = np.random.normal(loc=73, scale=4, size=30)
group_c = np.random.normal(loc=79, scale=4, size=30)

f_statistic, p_value = stats.f_oneway(group_a, group_b, group_c)

if p_value < 0.05:
    print("Reject the null hypothesis: at least one group mean is different.")
else:
    print("Fail to reject the null hypothesis: the data do not show a significant difference.")
Reject the null hypothesis: at least one group mean is different.
### Visualizing the Groups

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(10)

group_a = np.random.normal(loc=68, scale=4, size=30)
group_b = np.random.normal(loc=73, scale=4, size=30)
group_c = np.random.normal(loc=79, scale=4, size=30)

plt.figure(figsize=(9, 5))
plt.boxplot([group_a, group_b, group_c], tick_labels=["A", "B", "C"])
plt.ylabel("Score")
plt.title("Three Independent Groups")
plt.grid(axis="y", linestyle="--", alpha=0.4)
plt.show()
### Practical Example: Comparing Three Training Programs

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

np.random.seed(21)

program_a = np.random.normal(loc=71, scale=5, size=25)
program_b = np.random.normal(loc=75, scale=5, size=25)
program_c = np.random.normal(loc=81, scale=5, size=25)

result = stats.f_oneway(program_a, program_b, program_c)

print(f"Program means: {[round(x.mean(), 2) for x in [program_a, program_b, program_c]]}")
print(f"F-statistic: {result.statistic:.3f}")
print(f"p-value: {result.pvalue:.6f}")
print("Conclusion: significant difference across programs." if result.pvalue < 0.05 else "Conclusion: no significant difference detected.")

plt.figure(figsize=(9, 5))
plt.violinplot([program_a, program_b, program_c], showmeans=True)
plt.xticks([1, 2, 3], ["Program A", "Program B", "Program C"])
plt.ylabel("Outcome")
plt.title("Training Program Outcomes")
plt.grid(axis="y", linestyle=":", alpha=0.4)
plt.show()
Program means: [np.float64(69.9), np.float64(75.93), np.float64(81.67)]
F-statistic: 41.941
p-value: 0.000000
Conclusion: significant difference across programs.
### Conclusion

One-way ANOVA is a compact way to test whether three or more group means differ. Pairing the test with a chart makes the group differences much easier to interpret.