Sitemap

Member-only story

ANOVA Using Python

3 min readJun 21, 2026

--

ANOVA (Analysis of Variance) helps determine whether the means of multiple groups are significantly different. Instead of focusing on theory, this article focuses entirely on Python code, visualisations, and actual outputs.

Step 1: Import Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import f_oneway
from scipy.stats import shapiro
from scipy.stats import levene
import statsmodels.api as sm
from statsmodels.formula.api import ols
from statsmodels.stats.multicomp import pairwise_tukeyhsd

Step 2: Create Sample Data

We create three groups with different means.

np.random.seed(42)
group_A = np.random.normal(65, 5, 30)
group_B = np.random.normal(72, 5, 30)
group_C = np.random.normal(80, 5, 30)

Step 3: Check Group Means

print("Group A Mean:", round(group_A.mean(),3))
print("Group B Mean:", round(group_B.mean(),3))
print("Group C Mean:", round(group_C.mean(),3))

Actual Output

Group A Mean: 64.059
Group B Mean: 71.394
Group C Mean: 80.064

The means already suggest that the groups are different.

Step 4: Visualise the Data

Box Plot

plt.figure(figsize=(8,5))

--

--

Avinash
Avinash

Written by Avinash

Software Architect with over a decade of engineering experience.