Cramer's V measures association between categorical variables when ordinary correlation is inappropriate. It normalizes a chi-square result to a 0–1 scale: values near 0 indicate unrelated features, while values near 1 indicate strong association. The measure shows intensity, not direction, and is unchanged when the features are swapped. section 2.4.3
Use Cramer's V to:
- Compare categorical features without assigning misleading numerical meaning through label encoding. section 2.4.3
- Identify highly associated or potentially collinear features that may play similar roles in prediction. section 2.4.3
- Compare a categorical feature with a numeric feature after discretizing the numeric values, for example into deciles. section 2.4.3
Vehicle data
Discretize MPG into ten deciles, cross-tabulate the deciles against model year, calculate a chi-square statistic, and apply Cramer's V. The demonstrated comparison produces 0.855, indicating strong association. section 2.4.3
Listing 2.8 Using Cramer’s V to detect association
from scipy.stats import chi2_contingency #1
def cramerV(chi2, table): #2
n = table.values.sum() #3
minimum_dimension = min(table.shape)-1 #4
result = ((chi2 / n) / minimum_dimension)**0.5 #5
return result
deciles = pd.qcut(data.mpg, q=10, labels=False) #6
table = pd.crosstab(data.model_year, deciles) #7
chi2, p, dof, expected = chi2_contingency(observed=table) #8
print(cramerV(chi2, table)) #9
A Python listing implements the statistic with SciPy's chi-square contingency calculation, pandas discretization, and a cross-tabulated feature table. section 2.4.3

