Shapiro-Wilk Test Python

Table of Contents

				
					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).")
				
			

Free Community

Join 1,000+ AI Automation Builders

Weekly tutorials, live calls & direct access to Ryan & Matt.

Join Free →

Keep Learning

Streamlit Async

Streamlit runs Python scripts top-to-bottom when ever a user interacts with widget.Streamlit is synchronous by default, meaning each function waits for the...

Streamlit Caching

Streamlit runs your script from top to bottom whenever you interact with the app.This execution model makes development super easy. But it...

Streamlit Tutorial

Streamlit can help businesses automate a ton of tasks in a short amount of time. It essentially is a quick UI you...

Gradient boosting classifier

Gradient Boosting is an ensemble technique that builds a strong model by combining multiple weak decision trees. While it may seem similar...