7

在 Ubuntu 10.04.4 下,使用 Python 2.6.5、NumPy 和 SciPy,是否可以进行卡方独立性检验?在R中,这是通过以下方式实现的:

> row1 = c(91,90,51)
> row2 = c(150,200,155)
> row3 = c(109,198,172)
> data.table = rbind(row1,row2,row3)
> chisq.test(data.table)

我怎样才能在 Python 中做到这一点?

4

1 回答 1

7
from scipy.stats import chi2_contingency

row1 = [91,90,51]
row2 = [150,200,155]
row3 = [109,198,172]
data=[row1,row2,row3]
print chi2_contingency(data)

输出:

(25.085973274234959, 4.8346447416999636e-05, 4, array([[  66.77631579,   93.10526316,   72.11842105],
       [ 145.35361842,  202.66447368,  156.98190789],
       [ 137.87006579,  192.23026316,  148.89967105]]))

输出:

        Pearson's Chi-squared test

data:  data.table 
X-squared = 25.086, df = 4, p-value = 4.835e-05
于 2013-10-24T10:40:55.700 回答