Table of Contents
The one sample and two sample Z-tests are statistical methods used to compare a sample mean to a known population mean, or to compare the means of two independent samples. These tests are commonly performed in Python using the “stats” module from the “scipy” library. To perform a one sample Z-test, the sample mean, population mean, and standard deviation must be calculated and input into the “stats.ttest_1samp()” function. For a two sample Z-test, the means and standard deviations of both samples are needed and can be input into the “stats.ttest_ind()” function. The output of these functions will provide the test statistic and p-value, which can be used to determine if there is a significant difference between the means. Additionally, visualizations can be created using the “matplotlib” library to further analyze the results.
Perform One Sample & Two Sample Z-Tests in Python
You can use the ztest() function from the package to perform one sample and two sample z-tests in Python.
This function uses the following basic syntax:
statsmodels.stats.weightstats.ztest(x1, x2=None, value=0)
where:
- x1: values for the first sample
- x2: values for the second sample (if performing a two sample z-test)
- value: mean under the null (in one sample case) or mean difference (in two sample case)
The following examples shows how to use this function in practice.
Example 1: One Sample Z-Test in Python
Suppose the IQ in a certain population is normally distributed with a mean of μ = 100 and standard deviation of σ = 15.
A researcher wants to know if a new drug affects IQ levels, so he recruits 20 patients to try it and records their IQ levels.
The following code shows how to perform a one sample z-test in Python to determine if the new drug causes a significant difference in IQ levels:
from statsmodels.stats.weightstatsimport ztest as ztest
#enter IQ levels for 20 patients
data = [88, 92, 94, 94, 96, 97, 97, 97, 99, 99,
105, 109, 109, 109, 110, 112, 112, 113, 114, 115]
#perform one sample z-test
ztest(data, value=100)
(1.5976240527147705, 0.1101266701438426)
The test statistic for the one sample z-test is 1.5976 and the corresponding p-value is 0.1101.
Since this p-value is not less than .05, we do not have sufficient evidence to reject the null hypothesis. In other words, the new drug does not significantly affect IQ level.
Example 2: Two Sample Z-Test in Python
Suppose the IQ levels among individuals in two different cities are known to be normally distributed with known standard deviations.
A researcher wants to know if the mean IQ level between individuals in city A and city B are different, so she selects a of 20 individuals from each city and records their IQ levels.
The following code shows how to perform a two sample z-test in Python to determine if the mean IQ level is different between the two cities:
from statsmodels.stats.weightstatsimport ztest as ztest
#enter IQ levels for 20 individuals from each city
cityA = [82, 84, 85, 89, 91, 91, 92, 94, 99, 99,
105, 109, 109, 109, 110, 112, 112, 113, 114, 114]
cityB = [90, 91, 91, 91, 95, 95, 99, 99, 108, 109,
109, 114, 115, 116, 117, 117, 128, 129, 130, 133]
#perform two sample z-test
ztest(cityA, cityB, value=0)
(-1.9953236073282115, 0.046007596761332065)
The test statistic for the two sample z-test is -1.9953 and the corresponding p-value is 0.0460.
Since this p-value is less than .05, we have sufficient evidence to reject the null hypothesis. In other words, the mean IQ level is significantly different between the two cities.
The following tutorials explain how to perform other common statistical tests in Python:
Cite this article
stats writer (2024). How do you perform one sample and two sample Z-tests in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-perform-one-sample-and-two-sample-z-tests-in-python/
stats writer. "How do you perform one sample and two sample Z-tests in Python?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-do-you-perform-one-sample-and-two-sample-z-tests-in-python/.
stats writer. "How do you perform one sample and two sample Z-tests in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-perform-one-sample-and-two-sample-z-tests-in-python/.
stats writer (2024) 'How do you perform one sample and two sample Z-tests in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-perform-one-sample-and-two-sample-z-tests-in-python/.
[1] stats writer, "How do you perform one sample and two sample Z-tests in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How do you perform one sample and two sample Z-tests in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
