How to Perform Levene’s Test in Python

Spread the love


Levene’s test is a statistical procedure used to assess the equality of variances in different samples. The test is robust against departures from normality. Levene’s test essentially checks if your data meet the assumption of homogeneity of variances, which is required for several parametric tests such as Analysis of Variance (ANOVA), t-tests, and many regression analyses.

Python’s SciPy library includes an easy-to-use function for performing Levene’s test. This article will demonstrate how to use this function to perform Levene’s test in Python.

Hypothetical Scenario

Suppose you are an agricultural scientist testing three types of fertilizer to see which is most effective at promoting plant growth. You collect data on plant heights (in centimeters) after a specified period. You want to compare the variances of the plant heights in the three groups to ensure they’re similar before performing an ANOVA.

Implementing Levene’s Test

Start by importing the necessary libraries.

import numpy as np
import pandas as pd
from scipy.stats import levene

Assume you have the plant height data stored in three Python lists:

fertilizerA = [30, 32, 31, 29, 30, 31, 32, 31]
fertilizerB = [33, 35, 34, 33, 34, 35, 36, 35]
fertilizerC = [32, 34, 33, 32, 33, 34, 35, 33]

You can perform Levene’s test using the levene function from scipy.stats:

stat, p_value = levene(fertilizerA, fertilizerB, fertilizerC)

The levene function calculates Levene’s test for equal variances and returns the test statistic and the associated p-value.

Interpreting the Results

After performing the test, you can print the results:

print('Test statistic:', stat)
print('P-value:', p_value)

The test statistic is a value that the test uses to determine whether to reject the null hypothesis. The p-value is the probability of obtaining the observed data (or data more extreme) if the null hypothesis were true.

In the context of Levene’s test, the null hypothesis is that all input samples are from populations with equal variances. Therefore, a small p-value (typically ≤ 0.05) indicates strong evidence that the variances are not equal. Conversely, a large p-value (> 0.05) indicates weak evidence against the null hypothesis, so you fail to reject the null hypothesis of equal variances.


Levene’s test is a valuable tool in a data scientist’s or researcher’s toolkit for validating the assumption of equal variances across different groups. Python, along with the SciPy library, provides an accessible and efficient way to perform Levene’s test.

However, statistical tests are not a substitute for careful experimental design and data analysis. Even if the variances in your data are equal, there may still be other assumptions that are violated, or there may be outliers or other data anomalies that impact your results. Always remember to carefully consider the characteristics and quality of your data before performing any statistical tests.

Leave a Reply