How to Calculate Cramer’s V in Python


Cramer’s V is a measure of the strength of association between two nominal variables.

It ranges from 0 to 1 where:

  • 0 indicates no association between the two variables.
  • 1 indicates a strong association between the two variables.

It is calculated as:

Cramer’s V = √(X2/n) / min(c-1, r-1)

where:

  • X2: The Chi-square statistic
  • n: Total sample size
  • r: Number of rows
  • c: Number of columns

This tutorial provides a couple examples of how to calculate Cramer’s V for a contingency table in Python.

Example 1: Cramer’s V for a 2×2 Table

The following code shows how to calculate Cramer’s V for a 2×2 table:

#load necessary packages and functions
import scipy.stats as stats
import numpy as np

#create 2x2 table
data = np.array([[7,12], [9,8]])

#Chi-squared test statistic, sample size, and minimum of rows and columns
X2 = stats.chi2_contingency(data, correction=False)[0]
n = np.sum(data)
minDim = min(data.shape)-1

#calculate Cramer's V 
V = np.sqrt((X2/n) / minDim)

#display Cramer's V
print(V)

0.1617

Cramer’s V turns out to be 0.1617, which indicates a fairly weak association between the two variables in the table.

Example 2: Cramer’s V for Larger Tables

Note that we can use the CramerV function to calculate Cramer’s V for a table of any size.

The following code shows how to calculate Cramer’s V for a table with 2 rows and 3 columns:

#load necessary packages and functions
import scipy.stats as stats
import numpy as np

#create 2x2 table
data = np.array([[6,9], [8, 5], [12, 9]])

#Chi-squared test statistic, sample size, and minimum of rows and columns
X2 = stats.chi2_contingency(data, correction=False)[0]
n = np.sum(data)
minDim = min(data.shape)-1

#calculate Cramer's V 
V = np.sqrt((X2/n) / minDim)

#display Cramer's V
print(V)

0.1775

Cramer’s V turns out to be 0.1775.

Note that this example used a table with 2 rows and 3 columns, but this exact same code works for a table of any dimensions.

Additional Resources

Chi-Square Test of Independence in Python
Chi-Square Goodness of Fit Test in Python
Fisher’s Exact Test in Python

Leave a Reply

Your email address will not be published.