python one sample z test

				
					from statsmodels.stats.weightstats import ztest
import numpy as np
from scipy.stats import norm
				
			
				
					alpha = 0.05
				
			

Example 1 More Manual

				
					shoe_distance = [
     370, 395, 400, 405, 390, 385, 410, 395, 400, 380, 390, 400, 410, 415, 395, 405, 390, 400, 420, 375, 400, 385, 390, 395, 410, 405, 400, 395, 380, 400
]
				
			
				
					population_mean = 400
				
			
				
					population_std = 15
				
			
				
					sample_mean = np.mean(shoe_distance)
				
			
				
					print(sample_mean)
				
			
				
					sample_size = len(shoe_distance)
				
			
				
					print(sample_size)
				
			
				
					z_score = (sample_mean - population_mean) / (population_std / np.sqrt(sample_size))
				
			
				
					print(z_score)
				
			
				
					if p_value < alpha:
    print("Reject the null hypothesis")
else:
    print("Fail to reject the null hypothesis")

				
			
				
					#example 2 Faster with statsmodels
#This code is performing a z-test on a dataset of marathon completion times to determine
#if the sample mean is significantly different from the population mean

#Contains 50 individual marathon completion times (assumed in minutes).
#These represent the sample drawn from the population

Null hypothesis (H₀): The sample mean is equal to the population mean (270)

Alternative hypothesis (Hₐ): The sample mean is different from the population mean (270)

				
			
				
					marathon_data = [
    250, 260, 245, 255, 240, 265, 270, 260, 275, 255, 245, 250, 265, 255, 260, 245, 250, 275, 260, 255,
    245, 250, 265, 255, 260, 275, 250, 260, 255, 245, 260, 265, 255, 250, 275, 260, 255, 245, 250, 265,
    260, 270, 255, 245, 260, 265, 250, 260, 255, 245
]
				
			
				
					population_mean = 270  #  average
				
			
				
					population_std = 30   # Assumed standard deviation of marathon runners
				
			
				
					z_score, p_value = ztest(marathon_data, value=population_mean, alternative='two-sided')
				
			
				
					print('Z-test Score:', z_score,'P-value:', p_value)
				
			
				
					if p_value < alpha:
    print("Reject the null hypothesis: The sample mean is significantly different from the population mean.")
else:
    print("Fail to reject the null hypothesis: No significant difference between the sample mean and population mean.")
				
			

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *