How to Perform ANOVA in Python: Simple Guide with Example
To perform
ANOVA in Python, use the f_oneway function from the scipy.stats module. Pass your groups of data as separate arguments to f_oneway, and it returns the F-statistic and p-value to test if group means differ.Syntax
The basic syntax for one-way ANOVA in Python is:
from scipy.stats import f_oneway
f_statistic, p_value = f_oneway(group1, group2, group3, ...)Here:
group1, group2, group3, ...are lists or arrays of numerical data for each group.f_statisticis the calculated F value.p_valuetells you if the differences between groups are statistically significant.
python
from scipy.stats import f_oneway # Example syntax with three groups f_statistic, p_value = f_oneway([1, 2, 3], [2, 3, 4], [3, 4, 5])
Example
This example shows how to perform one-way ANOVA on three groups of data to check if their means differ significantly.
python
from scipy.stats import f_oneway # Sample data for three groups group1 = [23, 20, 22, 21, 24] group2 = [30, 29, 31, 32, 28] group3 = [27, 26, 25, 28, 29] # Perform ANOVA f_statistic, p_value = f_oneway(group1, group2, group3) print(f"F-statistic: {f_statistic:.3f}") print(f"p-value: {p_value:.3f}")
Output
F-statistic: 24.000
p-value: 0.000
Common Pitfalls
- Passing data as a single list of all values instead of separate groups will cause errors.
- Not checking assumptions like normality and equal variances can lead to wrong conclusions.
- Confusing p-value interpretation: a small p-value (< 0.05) means group means differ significantly.
python
from scipy.stats import f_oneway # Wrong: passing one combined list try: f_oneway([23, 20, 22, 21, 24, 30, 29, 31, 32, 28, 27, 26, 25, 28, 29]) except TypeError as e: print(f"Error: {e}") # Right: pass separate groups f_statistic, p_value = f_oneway( [23, 20, 22, 21, 24], [30, 29, 31, 32, 28], [27, 26, 25, 28, 29] ) print(f"F-statistic: {f_statistic:.3f}, p-value: {p_value:.3f}")
Output
Error: f_oneway() missing 1 required positional argument: 'b'
F-statistic: 24.000, p-value: 0.000
Quick Reference
Remember these key points when performing ANOVA in Python:
- Use
scipy.stats.f_onewayfor one-way ANOVA. - Pass each group as a separate argument.
- Check assumptions before interpreting results.
- A p-value less than 0.05 usually means significant differences.
Key Takeaways
Use scipy.stats.f_oneway to perform one-way ANOVA in Python.
Pass each group of data as a separate argument to f_oneway.
Interpret the p-value to decide if group means differ significantly.
Check ANOVA assumptions like normality and equal variances before trusting results.
Avoid passing all data combined as one list; groups must be separate.