Open In App

How to Perform a One-Way ANOVA in Python

Last Updated : 05 May, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

One-Way ANOVA is a statistical test used to check if there are significant differences between the means of three or more groups i.e analysis of variance. It helps us to find whether the variations in data are due to different treatments or random chance.

Hypotheses in One-Way ANOVA

One-way ANOVA has null and alternative hypotheses:

  • H0 (null hypothesis): μ1 = μ2 = μ3 = … = μk i.e it implies that the means of all the population are equal.
  • H1 (null hypothesis): It states that there will be at least one population mean that differs from the rest.

Example:

Imagine we want to compare the performance of four different engine oils used in cars. The cars drive 100 kilometers with each type of oil and their performance is recorded. We want to check if the performance differs depending on the oil used. Now we will use One-Way ANOVA test in Python with following steps:

Step 1: Install Required Libraries

We need to install the SciPy library in our system. We can install it using below command in terminal:

pip3 install scipy

Step 2: Create Data Groups

We create arrays representing the performance of cars with each engine oil.

Python
performance1 = [89, 89, 88, 78, 79]
performance2 = [93, 92, 94, 89, 88]
performance3 = [89, 88, 89, 93, 90]
performance4 = [81, 78, 81, 92, 82]

Step 3: Perform One-Way ANOVA

Now we use the f_oneway() function from SciPy to conduct the One-Way ANOVA test.

Python
from scipy.stats import f_oneway

f_statistic, p_value = f_oneway(performance1, performance2, performance3, performance4)

print(f"F-statistic: {f_statistic}")
print(f"P-value: {p_value}")

Output:

anova
Result

Step 4: Analyze the Result

The output will provide two important values:

  • F-statistic: It measures the variance between the groups relative to the variance within the groups.
  • P-value: It helps determine if the results are statistically significant.

Since p-value is less than 0.05 hence we would reject the null hypothesis. This implies that we have sufficient proof to say that there exists a difference in performance among four different engine oils.


Similar Reads