Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
How to Perform a Shapiro-Wilk Test in Python
Next article icon

How to Perform a Shapiro-Wilk Test in Python

Last Updated : 30 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will be looking at the various approaches to perform a Shapiro-wilk test in Python.

Shapiro-Wilk test is a test of normality, it determines whether the given sample comes from the normal distribution or not. Shapiro-Wilk’s test or Shapiro test is a normality test in frequentist statistics. The null hypothesis of Shapiro’s test is that the population is distributed normally.

Shapiro-Wilk test using shapiro() function

In this approach, the user needs to call the shapiro() function with the required parameters from the scipy.stats library to conduct the Shapiro-Wilk test on the given data in the python programming language.

Syntax: shapiro(x)

Parameters:

  • x: Array of sample data.

Returns:

   statistic: The test statistic.

   p-value: The p-value for the hypothesis test.

This is a hypotheses test and the two hypotheses are as follows:

  • Ho(Accepted): Sample is from the normal distributions.(Po>0.05)
  • Ha(Rejected): Sample is not from the normal distributions.

Example 1: Shapiro-Wilk test on the normally distributed sample in Python

In this example, we will be simply using the shapiro() function from the scipy.stats library to Conduct a Shapiro-Wilk test on the randomly generated data with 500 data points in python.

Python3
# import useful library import numpy as np from scipy.stats import shapiro from numpy.random import randn  # Create data gfg_data = randn(500)  # conduct the  Shapiro-Wilk Test shapiro(gfg_data) 

Output:

(0.9977102279663086, 0.7348126769065857)

Output Interpretation:

Since in the above example, the p-value is 0.73 which is more than the threshold(0.05) which is the alpha(0.05) then we fail to reject the null hypothesis i.e. we do not have sufficient evidence to say that sample does not come from a normal distribution.

Example 2: Shapiro-Wilk test on not normally distributed sample in Python

In this example, we will be simply using the shapiro() function from the scipy.stats library to Conduct a Shapiro-Wilk test on the randomly generated data from the passion distribution data with 100 data points in Python.

Python3
# import useful library import numpy as np from numpy.random import poisson from numpy.random import seed from scipy.stats import shapiro from numpy.random import randn  seed(0) # Create data gfg_data = poisson(5, 200)  # conduct the  Shapiro-Wilk Test shapiro(gfg_data) 

Output:

(0.966901957988739, 0.00011927181185455993)

Output Interpretation:

Since in the above example, the p-value is 0.0001 which is less than the alpha(0.05) then we reject the null hypothesis i.e. we have sufficient evidence to say that sample does not come from a normal distribution.


Next Article
How to Perform a Shapiro-Wilk Test in Python

G

geetansh044
Improve
Article Tags :
  • Machine Learning
  • Geeks Premier League
  • AI-ML-DS
  • Geeks-Premier-League-2022
  • python
Practice Tags :
  • Machine Learning
  • python

Similar Reads

    How to Perform Dunn’s Test in Python
    Dunn's test is a statistical procedure used for multiple comparisons following a Kruskal-Wallis test. Here's a breakdown of what it does and when it's used: Table of Content Dunn’s TestWhat is the Kruskal-Wallis test?Key points about Dunn's testHow to Perform Dunn’s Test with PythonStep-by-Step Guid
    6 min read
    How to Perform an F-Test in Python
    In statistics, Many tests are used to compare the different samples or groups and draw conclusions about populations. These techniques are commonly known as Statistical Tests or hypothesis Tests. It focuses on analyzing the likelihood or probability of obtaining the observed data that they are rando
    10 min read
    How to Perform a Wald Test in R
    In this article, we will discuss What is the Wald Test and How to Perform a Wald Test in R Programming Language. What is the Wald Test?The Wald test is a statistical hypothesis test used to assess whether parameters in a statistical model are significantly different from hypothesized values. It is w
    8 min read
    How to Perform a Kruskal-Wallis Test in Python
    Kruskal-Wallis test is a non-parametric test and an alternative to One-Way Anova. By non-parametric we mean, the data is not assumed to become from a particular distribution. The main objective of this test is used to determine whether there is a statistical difference between the medians of at leas
    2 min read
    How to Perform Welch’s ANOVA in Python
    When the assumption of equal variances is violated, Welch's ANOVA is used as an alternative to the standard one-way ANOVA. A one-way ANOVA ("analysis of variance") is used to see if there is a statistically significant difference in the means of three or more independent groups. Steps to perform Wel
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences