Non-parametric group comparison
Comparing measurements from two or more independent groups without assuming normally distributed data.
SciPy · Function
Computes the Kruskal-Wallis H-test for two or more independent samples. It tests whether all groups have equal population medians and supports unequal sample sizes.
The function ranks observations pooled across two or more independent samples, then evaluates whether the groups plausibly share the same population median. It returns an H statistic with tie correction and a p-value approximated from a chi-square distribution; axis and NaN-policy options control multidimensional input handling.
scipy.stats.kruskal(*samples, nan_policy='propagate', axis=0, keepdims=False)| Name | Type | Description |
|---|---|---|
samples | array_like | Two or more arrays containing the measurements for the independent groups; samples must be one-dimensional. |
nan_policy | {'propagate', 'raise', 'omit'}, optional | Controls NaN handling: propagate returns NaN, raise raises an error, and omit ignores NaNs. |
axis | int or None, optional | Axis along which to compute the test; None ravels the input before computation. |
keepdims | bool, optional | If True, retains reduced axes with size one in the output. |
A result containing statistic, the Kruskal-Wallis H statistic corrected for ties, and pvalue, the p-value based on a chi-square distribution.
import numpy as np
from scipy.stats import kruskal
control = np.array([1, 2, 3, 4, 5])
treatment = np.array([3, 4, 5, 6, 7])
result = kruskal(control, treatment)
print(result.statistic, result.pvalue)Runs the Kruskal-Wallis test on two independent samples and prints the H statistic and p-value.
Comparing measurements from two or more independent groups without assuming normally distributed data.
Testing groups whose sample counts are not equal.
Processing array data along a selected dimension while controlling missing-value behavior.
It is a non-parametric test for two or more independent samples. Its null hypothesis is that the population medians of all groups are equal, and it can accept samples of different sizes.
Follow-up: What does a significant result identify? It indicates that not all groups are consistent with the null, but additional post-hoc comparisons are needed to locate the differences.
The function returns the Kruskal-Wallis H statistic, corrected for ties, and a p-value. The p-value uses the assumption that H follows a chi-square distribution.
Follow-up: Why should group sizes matter? The chi-square approximation depends on sufficient observations; very small samples can make the p-value less reliable.
The signature accepts sample arrays plus nan_policy, axis, and keepdims. nan_policy can propagate NaNs, raise an error, or omit NaN values from calculations.
Follow-up: What shape restriction applies to samples? The documentation states that samples must be one-dimensional.
scipy.stats.f_onewayscipy.stats.mstats.kruskal