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

SciPy · Function

stats.chi2_contingency

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.

How it works

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.

Syntax

scipy.stats.chi2_contingency(observed, correction=True, lambda_=None, *, method=None)

Parameters

NameTypeDescription
observedarray_likeThe contingency table containing observed frequencies.
correctionbool, optionalWhether to apply Yates' continuity correction when degrees of freedom equal one.
lambda_float or str, optionalSelect a statistic from the Cressie–Read power-divergence family; omitted means Pearson's chi-squared statistic.
methodPermutationMethod or MonteCarloMethod, optionalA resampling method for inference, when supported by the current API.

Returns

A result containing statistic, pvalue, dof, and expected_freq.

Key points

  • Use it to test independence among categorical variables represented by a contingency table.
  • The primary outputs quantify the test statistic and the evidence against independence.
  • Expected counts come from the table's marginal totals while assuming independence.
  • The result reports the degrees of freedom used by the test.
  • Expected frequencies preserve the input table's dimensional structure.

Common mistakes

  • Use a table with at least two dimensions; a one-dimensional input does not represent an independence test.
  • The default is Pearson's statistic; set lambda_ when a Cressie–Read power-divergence statistic is required.

Example: Test independence in a contingency table

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.

When to use it

Categorical independence testing

Assess whether two categorical variables are statistically independent from a two-dimensional count table.

Expected-frequency diagnostics

Inspect model-based counts alongside the test result to understand which frequencies differ from independence expectations.

Alternative divergence statistics

Use an alternative Cressie–Read power-divergence statistic instead of the default Pearson statistic.

Interview questions

QWhat does chi2_contingency test?

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.

QHow are expected frequencies obtained?

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.

QWhat do correction and lambda_ control?

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_freq
  • scipy.stats.power_divergence

Source: Chi2 Contingency. Checked 2026-10-15.