我试图使用Cramer的统计数据计算二元变量之间的相关性:
def cramers_corrected_stat(confusion_matrix):
chi2 = ss.chi2_contingency(confusion_matrix)[0]
n = confusion_matrix.sum()
phi2 = chi2/n
r,k = confusion_matrix.shape
phi2corr = max(0, phi2 - ((k-1)*(r-1))/(n-1))
rcorr = r - ((r-1)**2)/(n-1)
kcorr = k - ((k-1)**2)/(n-1)
return np.sqrt(phi2corr / min( (kcorr-1), (rcorr-1)))
但是,我不知道如何在我的数据集中应用上述代码:
CL UP NS P CL_S
480 1 0 1 0 1
1232 1 0 1 0 1
2308 1 1 1 0 1
1590 1 0 1 0 1
497 1 1 0 0 1
... ... ... ... ... ...
1066 1 1 1 0 1
1817 1 0 1 0 1
2411 1 1 1 0 1
2149 1 0 1 0 1
1780 1 0 1 0 1
我将感谢你对我的指导
您创建的函数不适合您的数据集
因此,使用下面给出的follow函数
cramers_V(var1,var2)
使用该函数的示例代码如下所示
如果要计算数据集的所有可能对,请使用以下代码
相关问题 更多 >
编程相关推荐