+1 vote
in Programming Languages by (6k points)
I want to compute chi-square and p-value for a 2x2 matrix. What Python library should I use to calculate these values?

1 Answer

0 votes
by (11.3k points)

You can perform either a Fisher exact test or Chi-square test to calculate p-value. For chi-square, you need to perform Chi-square test. Scipy library has functions for both of these tests.
Fisher exact test on a 2x2 contingency table

>>> obs
array([[60, 14],
       [19, 80]])
>>> from scipy.stats import fisher_exact
>>> oddsratio, pvalue = fisher_exact(obs)
>>> pvalue
>>> oddsratio

Chi-square test of independence of variables in a contingency table.

>>> obs
array([[60, 14],
       [19, 80]])
>>> chi, pvalue, dof, ex = chi2_contingency(obs)
>>> chi
>>> pvalue
>>> dof