我正在研究试图查看两个变量之间的关联的数据,并在Python的Scipy包中使用了卡方分析。
这是两个变量的交叉表结果:
pd.crosstab(data['loan_default'],data['id_proofs'])
结果:
id_proofs 2 3 4 5
loan_default
0 167035 15232 273 3
1 46354 4202 54 1
如果我在相同的数据上应用卡方,则会看到一个错误,提示ValueError:内部计算的期望频率表在(0,)处有一个零元素。
代码:
from scipy.stats import chi2_contingency
stat,p,dof,expec = chi2_contingency(data['loan_default'],data['id_proofs'])
print(stat,p,dof,expec)
错误报告:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-154-63c6f49aec48> in <module>()
1 from scipy.stats import chi2_contingency
----> 2 stat,p,dof,expec = chi2_contingency(data['loan_default'],data['id_proofs'])
3 print(stat,p,dof,expec)
~/anaconda3/lib/python3.6/site-packages/scipy/stats/contingency.py in chi2_contingency(observed, correction, lambda_)
251 zeropos = list(zip(*np.where(expected == 0)))[0]
252 raise ValueError("The internally computed table of expected "
--> 253 "frequencies has a zero element at %s." % (zeropos,))
254
255 # The degrees of freedom
ValueError: The internally computed table of expected frequencies has a zero element at (0,).
该问题可能是什么原因?我该如何克服?
答案 0 :(得分:1)
再看看chi2_contingency
的文档字符串。第一个参数observed
必须是列联表。您必须计算列联表(就像对pd.crosstab(data['loan_default'],data['id_proofs'])
所做的一样)并将其传递给chi2_contingency
。