Shapiro-Wilk Test Python

				
					import numpy as np
from scipy.stats import shapiro
import seaborn as sns
				
			
				
					alpha = 0.05
				
			
				
					np.random.seed(11)
				
			
#Uniform Example General
				
					rolls = np.random.randint(1, 7, size=30)
				
			
				
					sns.histplot(rolls)
				
			
				
					stat, shapiro_p_value = shapiro(rolls)
				
			
				
					print(shapiro_p_value)
				
			
				
					if shapiro_p_value > alpha:
    print("The data is likely normally distributed (fail to reject H0).")
else:
    print("The data is NOT normally distributed (reject H0).")
				
			
#Example with a Paired T Test
				
					ticket_sales_before = np.array([240000, 270000, 255000, 264000, 258000, 252000, 246000, 243000])
				
			
				
					ticket_sales_after = np.array([540000, 600000, 585000, 630000, 615000, 660000, 645000, 690000])
				
			
				
					ticket_sales_diff = ticket_sales_after - ticket_sales_before
				
			
				
					stat, shapiro_p_value = shapiro(ticket_sales_diff)
				
			
				
					if shapiro_p_value > alpha:
    print("The data is likely normally distributed (fail to reject H0).")
else:
    print("The data is NOT normally distributed (reject H0).")
				
			

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 *