Categorical independence testing
Assess whether two categorical variables are statistically independent from a two-dimensional count table.
SciPy · Function
Tests independence among variables in a contingency table by comparing observed frequencies with frequencies expected under independence. It returns the test statistic, p-value, degrees of freedom, and expected frequencies.
The function compares observed frequencies in a contingency table with frequencies expected under independence. It returns the test statistic, p-value, degrees of freedom, and expected frequencies; optional continuity correction or a Cressie–Read statistic can change the calculation.
scipy.stats.chi2_contingency(observed, correction=True, lambda_=None, *, method=None)| Name | Type | Description |
|---|---|---|
observed | array_like | The contingency table containing observed frequencies. |
correction | bool, optional | Whether to apply Yates' continuity correction when degrees of freedom equal one. |
lambda_ | float or str, optional | Select a statistic from the Cressie–Read power-divergence family; omitted means Pearson's chi-squared statistic. |
method | PermutationMethod or MonteCarloMethod, optional | A resampling method for inference, when supported by the current API. |
A result containing statistic, pvalue, dof, and expected_freq.
import numpy as np
from scipy.stats import chi2_contingency
observed = np.array([[10, 20, 30], [6, 9, 17]])
result = chi2_contingency(observed)
print(result.statistic, result.pvalue, result.dof)
print(result.expected_freq)Computes the Pearson chi-square test, then accesses the statistic, p-value, degrees of freedom, and expected frequencies.
Assess whether two categorical variables are statistically independent from a two-dimensional count table.
Inspect model-based counts alongside the test result to understand which frequencies differ from independence expectations.
Use an alternative Cressie–Read power-divergence statistic instead of the default Pearson statistic.
It tests whether categorical variables are independent by comparing observed counts with expected counts under independence. The returned p-value evaluates the independence hypothesis using the selected statistic.
Follow-up: How are degrees of freedom computed? For an observed array, they are observed.size - sum(observed.shape) + observed.ndim - 1.
The expected frequencies are derived from the table's marginal sums under the independence assumption. The result includes them in an array with the same shape as observed.
Follow-up: What shape should the expected-frequency output have? It matches the shape of the observed contingency table.
With correction=True, Yates' continuity correction is applied when the degrees of freedom equal one. Setting correction=False disables that adjustment; lambda_ can instead select a Cressie–Read statistic.
Follow-up: What is the default statistic? Pearson's chi-squared statistic is used when lambda_ is omitted.
scipy.stats.contingency.expected_freqscipy.stats.power_divergence