17 Statistical Hypothesis Tests in Python (Cheat Sheet)
17 Statistical Hypothesis Tests in Python (Cheat Sheet)
Search...
In this post, you will discover a cheat sheet for the most popular statistical hypothesis tests for a machine learning
project with examples using the Python API.
Each statistical test is presented in a consistent way, including: Start Machine Learning
The name of the test.
What the test is checking.
The key assumptions of the test.
How the test result is interpreted.
Python API for using the test.
Note, when it comes to assumptions such as the expected distribution of data or sample size, the results of a given
test are likely to degrade gracefully rather than become immediately unusable if an assumption is violated.
Generally, data samples need to be representative of the domain and large enough to expose their distribution to
analysis.
In some cases, the data can be corrected to meet the assumptions, such as correcting a nearly normal distribution to
be normal by removing outliers, or using a correction to the degrees Start
of freedom in a statistical
Machine ×
test when samples have
Learning
differing variance, to name two examples.
You can master applied Machine Learning
Finally, there may be multiple tests for a given concern, e.g. normality. We cannot
without math orget crisp
fancy answers to questions with
degrees.
Findatout
statistics; instead, we get probabilistic answers. As such, we can arrive how in this
different free and
answers topractical
the same course.
question by
considering the question in different ways. Hence the need for multiple different tests for some questions we may have
about data. Email Address
Kick-start your project with my new book Statistics for Machine Learning, including step-by-step tutorials and the
START MY EMAIL COURSE
Python source code files for all examples.
Email Address
Tutorial Overview
This tutorial is divided into 5 parts; they are:
1. Normality Tests
This section lists statistical tests that you can use to check if your data has a Gaussian distribution.
Shapiro-Wilk Test
Start Machine Learning
Tests whether a data sample has a Gaussian distribution.
Assumptions
Interpretation
Python Code
Interpretation
Python Code
Anderson-Darling Test
Tests whether a data sample has a Gaussian distribution.
Assumptions
Start Machine Learning
Observations in each sample are independent and identically distributed (iid).
Interpretation
Python Code
2. Correlation Tests
This section lists statistical tests that you can use to check if two samples are related.
Assumptions
Interpretation
Python Code
Start Machine Learning ×
1 # Example of the Pearson's Correlation test
2 from scipy.stats import pearsonr You can master applied Machine Learning
3 data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478,
without math-1.637,
or fancy-1.869]
degrees.
4 data2 = [0.353, 3.517, 0.125, -7.545, -0.555, -1.536, 3.350, -1.578, -3.537, -1.579]
5 stat, p = pearsonr(data1, data2) Find out how in this free and practical course.
6 print('stat=%.3f, p=%.3f' % (stat, p))
7 if p > 0.05:
8 print('Probably independent') Email Address
9 else:
10 print('Probably dependent')
START MY EMAIL COURSE
More Information
Assumptions
Interpretation
Python Code
More Information
START MY EMAIL COURSE
Assumptions
Interpretation
Python Code
More Information
START MY EMAIL COURSE
Chi-Squared Test
Start Machine Learning
Tests whether two categorical variables are related or independent.
Assumptions
Interpretation
Python Code
3. Stationary Tests
Start Machine Learning
This section lists statistical tests that you can use to check if a time series is stationary or not.
Assumptions
Interpretation
More Information
Assumptions
Interpretation
Python Code
Start Machine Learning ×
1 # Example of the Kwiatkowski-Phillips-Schmidt-Shin test
2 from statsmodels.tsa.stattools import kpss You can master applied Machine Learning
3 data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4 stat, p, lags, crit = kpss(data) without math or fancy degrees.
5 print('stat=%.3f, p=%.3f' % (stat, p)) Find out how in this free and practical course.
6 if p > 0.05:
7 print('Probably not Stationary')
8 else: Email Address
9 print('Probably Stationary')
More Information
START MY EMAIL COURSE
statsmodels.tsa.stattools.kpss API.
KPSS test, Wikipedia.
Assumptions
Interpretation
More Information
Assumptions
Interpretation
More Information
Assumptions
Interpretation
H0: the means of the samples are equal. Start Machine Learning ×
H1: one or more of the means of the samples are unequal.
You can master applied Machine Learning
Python Code without math or fancy degrees.
Find out how in this free and practical course.
1 # Example of the Analysis of Variance Test
2 from scipy.stats import f_oneway
3 data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360,Email Address
-1.478, -1.637, -1.869]
4 data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
5 data3 = [-0.208, 0.696, 0.928, -1.148, -0.213, 0.229, 0.137, 0.269, -0.870, -1.204]
6 stat, p = f_oneway(data1, data2, data3) START MY EMAIL COURSE
7 print('stat=%.3f, p=%.3f' % (stat, p))
8 if p > 0.05:
9 print('Probably the same distribution')
10 else:
11 print('Probably different distributions')
More Information
Start Machine Learning
How to Calculate Parametric Statistical Hypothesis Tests in Python
scipy.stats.f_oneway
Analysis of variance on Wikipedia
Assumptions
More Information
Assumptions
Interpretation
More Information
Assumptions
Interpretation
H0: the distributions of both samples are equal. Start Machine Learning ×
H1: the distributions of both samples are not equal.
You can master applied Machine Learning
Python Code without math or fancy degrees.
Find out how in this free and practical course.
1 # Example of the Wilcoxon Signed-Rank Test
2 from scipy.stats import wilcoxon
3 data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360,Email Address
-1.478, -1.637, -1.869]
4 data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
5 stat, p = wilcoxon(data1, data2)
6 print('stat=%.3f, p=%.3f' % (stat, p)) START MY EMAIL COURSE
7 if p > 0.05:
8 print('Probably the same distribution')
9 else:
10 print('Probably different distributions')
More Information
Kruskal-Wallis H Test
Tests whether the distributions of two or more independent samples are equal or not.
Assumptions
Interpretation
H0: the distributions of all samples are equal. Start Machine Learning ×
H1: the distributions of one or more samples are not equal.
You can master applied Machine Learning
Python Code without math or fancy degrees.
Find out how in this free and practical course.
1 # Example of the Kruskal-Wallis H Test
2 from scipy.stats import kruskal
3 data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360,Email Address
-1.478, -1.637, -1.869]
4 data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
5 stat, p = kruskal(data1, data2)
6 print('stat=%.3f, p=%.3f' % (stat, p)) START MY EMAIL COURSE
7 if p > 0.05:
8 print('Probably the same distribution')
9 else:
10 print('Probably different distributions')
More Information
Friedman Test
Tests whether the distributions of two or more paired samples are equal or not.
Assumptions
Interpretation
Start Machine Learning ×
H0: the distributions of all samples are equal.
You can master applied Machine Learning
H1: the distributions of one or more samples are not equal.
without math or fancy degrees.
Python Code Find out how in this free and practical course.
Further Reading
This section provides more resources on the topic if you are looking to go deeper.
How to Reduce Variance in a Final Machine Learning Model A Gentle Introduction to SARIMA for Time Series Forecasting in Python
one note on the anderson darling test. the use of p values to determineEmail
GoF has been discouraged in some fields .
Address
Indeed, I think it was a journal of psychology that has adopted “estimation statistics” instead of hypothesis tests in
reporting results.
Start Machine Learning
REPLY
Hitesh August 17, 2018 at 3:19 pm #
REPLY
Jason Brownlee August 18, 2018 at 5:32 am #
Hi, thanks for this nice overview. You can master applied Machine Learning
without math or fancy degrees.
Some of these tests, like friedmanchisquare, expect that the quantity of events is the group to remain the same over
Find out how in this free and practical course.
time. But in practice this is not allways the case.
Lets say there are 4 observations on a group of 100 people, but the sizeEmail
of theAddress
response from this group changes over
time with n1=100, n2=95, n3=98, n4=60 respondants.
n4 is smaller because some external factor like bad weather.
What would be your advice on how to tackle this different ‘respondants’ START MY EMAIL
sizes over time? COURSE
REPLY
Jason Brownlee August 18, 2018 at 5:36 am #
REPLY
Fredrik August 21, 2018 at 5:44 am #
Shouldn’t it say that Pearson correlation measures the linear relationship between variables? I would say that
monotonic suggests, a not necessarily linear, “increasing” or “decreasing” relationship.
REPLY
Jason Brownlee August 21, 2018 at 6:23 am #
Right, Pearson is a linear relationship, nonparametric methods like Spearmans are monotonic relationships.
Start Machine Learning ×
Thanks, fixed.
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Fredrik August 23, 2018 at 8:59 pm #
Email Address
No problem. Thank you for a great blog! It has introduced me to so many interesting and useful topics.
REPLY
Jason Brownlee August 24, 2018 at 6:07 am #
(2) What about using graphical means such as a histogram of the data – is it symmetrical? What about normal plots
https://www.itl.nist.gov/div898/handbook/eda/section3/normprpl.htm if the line is straight, then with the statistical tests
described in (1), you can assess that the data may well come from a gaussian distribution.
Thank you,
Anthony of Sydney
Jason Brownlee April 26, 2020 at 6:10 am # Start Machine Learning REPLY
You’re welcome.
REPLY
Tej Yadav August 26, 2018 at 4:07 pm #
Wow.. this is what I was looking for. Ready made thing for ready reference.
REPLY
Jason Brownlee August 27, 2018 at 6:10 am #
Thanks a lot, Jason! You’re the best. I’ve been scouring the internet for a piece on practical implementation of
Email Address
Inferential statistics in Machine Learning for some time now!
Lots of articles with the same theory stuff going over and over again but none like this.
START MY EMAIL COURSE
REPLY
Jason Brownlee November 8, 2018 at 6:08 am #
Hi Jason, Statsmodels is another module that has got lots to offer but very little info on how to go about
it on the web. The documentation is not as comprehensive either compared to scipy. Have you written anything
on Statsmodels ? A similar article would be of great help.
REPLY
Jason Brownlee November 9, 2018 at 5:22 am #
Yes, I have many tutorials showing how to use statsmodels for time series:
https://machinelearningmastery.com/start-here/#timeseries
If your seeking for completeness on easy appliable hypothesis tests like those, I suggest to add the Kolmogorov-Smirnov
START MY EMAIL COURSE
test which is not that different from the Shapiro-Wilk.
– https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ks_2samp.html
– https://www.researchgate.net/post/Whats_the_difference_between_Kolmogorov-Smirnov_test_and_Shapiro-Wilk_test
REPLY
Paresh April 16, 2019 at 5:17 pm #
Which methods fits for classification or regression data sets? Which statistical tests are good for Semi-
supervised/ un-supervised data sets?
This post will help: You can master applied Machine Learning
without math or fancy degrees.
https://machinelearningmastery.com/statistical-significance-tests-for-comparing-machine-learning-algorithms/
Find out how in this free and practical course.
Email Address
REPLY
Luc May 1, 2019 at 10:01 pm #
I’m wondering how to check that “observations in each sample have the same variance” … Is there a test to check that ?
Great question.
You can calculate the mean and standard deviation for each interval.
You can also plot the series and visually look for increasing variance.
REPLY
João Antônio Martins June 2, 2019 at 4:39 am #
Is there a test similar to the friedman test? which has the same characteristics “whether the distributions of two
or more paired samples are equal or not”.
Start Machine Learning ×
You can master applied Machine Learning
without math or fancy degrees. REPLY
Jason Brownlee June 2, 2019 at 6:45 am #
Find out how in this free and practical course.
HI, Jason, Thank you for your nice blog. I have one question. I have two samples with different size (one is 102,
the other is 2482), as well as the variances are different, which statistical hypothesis method is appropriate? Thank you.
The test depends on the nature of the question you’re trying to answer.
REPLY
Adrian Olszewski February 27, 2020 at 11:32 pm #
Practically ALL assumptions and ALL interpretations are wrong in this cheatsheet. I cannot recommend this,
as if a student repeats that on a stat exam or on an interview led by a statistician, one’s likely to fail it. I am
messaged regularly by young aspiring data scientists who experienced problems after repeating texts from the
Start Machine Learning
internet, that’s why I ask you to not exposing learners to such situations. ×
1. Assumptions of the paired t-test are totally wrong, or copy-pasted. The interpretation is wrong too.
You can master applied Machine Learning
2. Anova is not a test, but OK, let’s pretend I didn’t see it. The interpretation isn’t correct. If you follow that, you may
without math or fancy degrees.
be really surprised doing the post-hoc
Find out how in this free and practical course.
3. interpretation of the RM-ANOVA is wrong
4. Mann-Whitney is described imprecisely.
Email Address
5. Paired Wilcoxon has wrong interpretation.
6. Normality tests – all is wrong. What “each sample” – in normality test? and it doesn’t tell if it’s Gaussian! It says of
the data is approximated by the normal distribution acceptably well at this sample
START size.
MY EMAIL In a minute I can give you
COURSE
examples drawn from log-normal or Weibull reported as “Gaussian” .
It’s worth noting there are over 270 tests, 50 in constant, everyday use, varying across industries and areas of
specialization. Type “100 statistical tests PDF” into Google or find the handbook of parametric and non-parametric
methods by Sheskin (also available in PDF), to get some rough idea about them. The more you know, the less you
are limited. Each of those tests has its weaknesses and strengthens you should know before the use. Always pay
attention to the null hypothesis and the assumptions. Jason Brownlee Start Machine Learning
REPLY
Jason Brownlee February 28, 2020 at 6:09 am #
REPLY
Mr.T March 1, 2020 at 9:08 am #
I am an early stage learner of all of this, and Jason’s posts have been incredibly helpful in helping me construct a
semantic tree of all the knowledge pieces. Without a lot of his posts, my knowledge pieces would be scattered.
Start
I am not certain about the accuracy as you have pointed out, but Machine
your lack Learning
of constructiveness in your comment
×
is concerning. You do not provide what you believe is the correct interpretation.
You can master applied Machine Learning
I truly hate to see a comment like this. Keep up the good workwithout
Jason! math or fancy degrees.
Find out how in this free and practical course.
Email Address
REPLY
Jason Brownlee March 2, 2020 at 6:10 am #
REPLY
MIAO June 28, 2019 at 5:50 pm #
REPLY
Jason Brownlee June 29, 2019 at 6:37 am #
Sounds like you want a classification (discrimination) model, not a statistical test?
Yeah, I think you are right. I will use SVM to classify the features. Thank you.
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Veetee August 6, 2019 at 1:04 am #
Email Address
Hi Jason, thanks for the very useful post. Is there a variant of Friedman’s test for only two sets of
measurements? I have an experiment in which two conditions were tested on the same people. I expect a semi-constant
START MY EMAIL COURSE
change between the two conditions, such that the ranks within blocks are expected to stay very similar.
REPLY
Jason Brownlee August 6, 2019 at 6:40 am #
Dear Sir,
I have one question if we take subset of the huge data,and according to the Central limit theorem the ‘samples averages
follow normal distribution’.So in that case is it should we consider Nonparametric Statistical Hypothesis Tests or
parametric Statistical Hypothesis Tests
REPLY
Jason Brownlee September 7, 2019 at 5:29 am #
I don’t follow your question sorry, please you can restate Start
it? Machine Learning ×
Generally nonparametric stats use ranking instead of gaussians. You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
gopal jamnal September 28, 2019 at 10:43 pm # Email Address
What is A-B testing, and how it can be useful in machine learning. Is it different then hypotheisis testing?
START MY EMAIL COURSE
REPLY
Jason Brownlee September 29, 2019 at 6:12 am #
Instead, in machine learning, we will evaluate the performance of different machine learning algorithms, and
compare the samples of performance estimates to see if the difference in performance between algorithms is
significant or not.
More here:
https://machinelearningmastery.com/statistical-significance-tests-for-comparing-machine-learning-algorithms/
REPLY
Peiran November 14, 2019 at 8:57 am #
Start
You can’t imagine how happy I am to find a cheat sheet like this! Machine
Thank Learning
you for the links too. ×
You can master applied Machine Learning
without math or fancy degrees.
Jason Brownlee November 14, 2019 at 1:43 pm # Find out how in this free and practical course. REPLY
Hi Jason –
Thank you for helping to bring the theory of statistics to everyday application !
I observe a library at
http://www.statsmodels.org/0.9.0/generated/statsmodels.stats.weightstats.ttost_paired.html#statsmodels.stats.weightstat
s.ttost_paired
but it doesn’t explain how to establish reasonable low and high limits.
Great suggestion, thanks Chris! You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
makis January 29, 2020 at 4:58 am #
Email Address
Hi Jason,
If I want to compare the Gender across 2 groups, is chi-square test a good choice?
I want to test for signiicant differences similarly to a t-test for a numerical variable.
REPLY
jessie June 29, 2020 at 7:10 pm #
Hi Jason,
I wanna use Nonparametric Statistical Hypothesis Tests to analysis ordinal data(good, fair, bed) or categorical data,
would i encode them to numerical data and follow the above steps? Would u give some suggestion?
Thanks.
Email Address
REPLY
Jonathan August 23, 2020 at 8:43 am #
REPLY
Jason Brownlee August 23, 2020 at 1:16 pm #
Start Machine Learning
Thanks for sharing.
REPLY
Kenny August 31, 2020 at 7:17 pm #
Hi Jason,
Thanks for the very informative Article. It looks great to see all Hypothesis tests in one article.
1) Would you be able to help saying when to use Parametric Statistical Hypothesis Tests and when to use Non-
Parametric Statistical Hypothesis Tests,please?
Knowing what to use in given situations could be a lot helpful.
2) For doing A/B Testing with varying distributions in the 2 experiments under conditions of multiple features involved,
would you recommend Parametric Statistical Hypothesis Tests or Non-Parametric Statistical Hypothesis Tests?
( I have tried Parametric Statistical Hypothesis Tests but it was getting hard to meet the statistical significance, as there
are multiple features involved)
Start Machine Learning ×
You can master applied Machine Learning
without math or fancy degrees.
REPLY
Jason Brownlee September 1, 2020 at 6:28 am # Find out how in this free and practical course.
Use a parametric test when your data is Gaussian and well behaved, use a non-parametric test otherwise.
Email Address
I don’t know about significance test for A/B testing off hand sorry. The sample distribution is discrete I would expect.
Perhaps a chi squared test would be appropriate? I’m shooting from the hip.
START MY EMAIL COURSE
REPLY
MARCILIO DE OLIVEIRA MEIRA September 4, 2020 at 1:08 pm #
Hi Jason, make any sense using an statistical hypothesis tests for image classification, with machine learning?
What method is more suitable for a problem of image classification to determine if a image
Start Machine belong to a class A or class
Learning
B?
REPLY
Jason Brownlee September 4, 2020 at 1:38 pm #
Not in this case, a machine learning model would perform this prediction for you.
REPLY
Kenny September 21, 2020 at 4:59 pm #
Hi Jason,
Thanks for the article .Its quite informative.
Start Machine Learning ×
Say if the data for some reasons has a non-monotonic relationship between the variables, would Hypothesis testing be
of much help? You can master applied Machine Learning
Doesn’t it make sense to first check the prior belief by actually verifyingwithout
if the relationship is monotonous
math or fancy degrees. or not, before
doing any specific Hypothesis tests to get further statistical insights? Find out how in this free and practical course.
Email Address
REPLY
Jason Brownlee September 22, 2020 at 6:43 am #
START MY EMAIL COURSE
It depends on the question you want to answer.
REPLY
Hugo November 11, 2020 at 2:58 am #
Start Machine Learning
Hi Jason,
Congratulations on the work you are doing with such subjects. It really helps me every time I need to get quick and
pŕecise content in this field.
I do have a question, though. About the stats.f_oneway module (ANOVA), I’m trying to run it with samples that have
different sizes, and that is returning an error “ValueError: arrays must all be same length”.
I tried to find the solution for this in the community, but I failed in finding it. Could you please help me out with this?
Should I input np.nan values to “fill” the empty spaces in the samples so they all match the same length?
Thanks in advance!
Leave a Reply
Welcome!
START MY EMAIL COURSE
I'm Jason Brownlee PhD
and I help developers get results with machine learning.
Read more
Email Address
Email Address