def cramers_v_corrected_stat(confusion_matrix):
"""Calculate Cramérs V statistic for categorial-categorial association.
Uses correction from Bergsma and Wicher, Journal of the Korean Statistical
Society 42 (2013): 323-328.
"""
chi2 = stats.chi2_contingency(confusion_matrix)[0]
n = confusion_matrix.sum()
phi2 = chi2 / n
r, k = confusion_matrix.shape
phi2_corr = max(0, phi2 - ((k-1)*(r-1)) / (n-1))
r_corr = r - ((r-1)**2) / (n-1)
k_corr = k - ((k-1)**2) / (n-1)
return math.sqrt(phi2_corr / min((r_corr-1), (k_corr-1)))
评论列表
文章目录