Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.kruskal

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.

How it works

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.

Syntax

scipy.stats.kruskal(*samples, nan_policy='propagate', axis=0, keepdims=False)

Parameters

NameTypeDescription
samplesarray_likeTwo or more arrays containing the measurements for the independent groups; samples must be one-dimensional.
nan_policy{'propagate', 'raise', 'omit'}, optionalControls NaN handling: propagate returns NaN, raise raises an error, and omit ignores NaNs.
axisint or None, optionalAxis along which to compute the test; None ravels the input before computation.
keepdimsbool, optionalIf True, retains reduced axes with size one in the output.

Returns

A result containing statistic, the Kruskal-Wallis H statistic corrected for ties, and pvalue, the p-value based on a chi-square distribution.

Key points

  • Use kruskal for a rank-based comparison of independent groups when a parametric ANOVA is not appropriate.
  • Pass at least two independent sample arrays; unequal group lengths are supported.
  • Each group is supplied as a separate positional sample argument.
  • Provide one-dimensional inputs for each sample rather than matrices or higher-dimensional arrays.
  • SciPy computes the reported p-value as the chi-square survival probability at the observed H statistic.

Common mistakes

  • The chi-square approximation can be unreliable for very small groups; the documentation gives five measurements per sample as a typical rule.
  • A significant result only indicates that at least one group differs; use post-hoc comparisons to identify the differing groups.

Example: Test two independent samples

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.

When to use it

Non-parametric group comparison

Comparing measurements from two or more independent groups without assuming normally distributed data.

Unequal-sized samples

Testing groups whose sample counts are not equal.

Array-oriented testing

Processing array data along a selected dimension while controlling missing-value behavior.

Interview questions

QWhat problem does scipy.stats.kruskal solve?

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.

QWhat does the returned p-value represent?

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.

QHow are missing values handled?

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_oneway
  • scipy.stats.mstats.kruskal

Source: kruskal. Checked 2026-10-15.