One-sample test for proportion - python

953 views python

I want to do "One-sample test for proportion" with Python. I found this document one sample proportion ztest example but I don't understand how to use it. For example, what are count and nobs. In the 2 examples, example1 gives single number for count and nobs, however, example2 gives 2 numbers.

For result, I'd like to know the p-value that the event happen rate is higher than 60%


>>> count = 5
>>> nobs = 83
>>> value = .05
>>> stat, pval = proportions_ztest(count, nobs, value)
>>> print('{0:0.3f}'.format(pval))


>>> import numpy as np
>>> from statsmodels.stats.proportion import proportions_ztest
>>> count = np.array([5, 12])
>>> nobs = np.array([83, 99])
>>> stat, pval = proportions_ztest(counts, nobs)
>>> print('{0:0.3f}'.format(pval))

My data looks like this

Yes No
1   0
1   0
1   0
0   1
0   1
1   0
1   0
0   1
0   1
0   1
0   1
0   1

Can you help explain how to use it and give some examples?

Thank you!

answered question

1 Answer


In case of example 1:

nobs is the total number of trials, i.e. the number of rows in your list.

count is the number of successful trials, i.e. the number of Yes events in your list.

value is the proportion to test against, i.e. 0.6 based on your question text.

The null hypothesis here is that the single sample given by these values was drawn from a distribution with proportion equal to the specified value.

In case of example 2:

There are two independent samples, the first entry of the nobs and count vectors represent the first sample, the second ones the second sample. value is then omitted and the null hypothesis will be that the two samples have equal true proportion.

posted this

Have an answer?


Please login first before posting an answer.