liveBook · Manning

Cramer's V

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

A normalized measure of association between categorical variables ranging from 0 to 1 and indicating association intensity without direction.

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

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Defines the normalized chi-square measure, interprets its range, and demonstrates calculation after discretization.
Examples
Uses MPG deciles, model year, a contingency table, and a demonstrated value of 0.855.
What it assumes
Assumes categorical features and chi-square contingency analysis, while showing how to discretize numeric values.
What you can do after
Apply it via SciPy and pandas to quantify association between categorical or discretized features.
How it connects
Connects chi-square statistics, correlation, label encoding, discretization, and collinearity.

Also explained in

Compare how 2 books explain Cramer's V →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.