简体   繁体   中英

Chi Square Test of Independence in Python

Under Ubuntu 10.04.4, using Python 2.6.5, NumPy and SciPy, is it possible to do a chi square test of independence? In R , this is achieved with the following:

> 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)

How can I do this in Python?

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)

Output:

(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]]))

R Output:

        Pearson's Chi-squared test

data:  data.table 
X-squared = 25.086, df = 4, p-value = 4.835e-05

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM