Open In App

How to Conduct a Paired Samples T-Test in Python

Last Updated : 28 Feb, 2022
Like Article

Paired sample T-test: This test is also known as the dependent sample t-test. It is a statistical concept and is used to check whether the mean difference between the two sets of observation is equal to zero.  Each entity is measured is two times in this test that results in the pairs of observations.

Syntax to install Scipy library in our system :

pip install scipy

How to conduct a paired samples T-Test in Python?

Let us consider that we want to know whether an engine oil significantly impacts the car’s mileage of different brands. In order to test this, we have 10 cars in a garage doped with original engine oil initially. We have noted their mileage for 100 kilometers each. Then, we have each of the cars doped with another engine oil (different from the original one). Then, the mileage of the cars is calculated for 100 kilometers each. To compare the difference between the mean mileage of the first and second test, we use a paired samples t-test because for each car their first test score can be paired with their second test score. Conducting paired sample T-test is a step-by-step process.

Step 1: Construct the data.

We need two arrays to hold pre and post-mileage of the cars.


# pre holds the mileage before applying
# the different engine oil
pre = [88, 82, 84, 93, 75, 78, 84, 87,
       95, 91, 83, 89, 77, 68, 91]
# post holds the mileage before applying 
# the different engine oil
post = [91, 84, 88, 90, 79, 80, 88, 90
        90, 96, 88, 89, 81, 74, 92]

Step 2: Conducting a paired-sample T-test.

Scipy library contains ttest_rel() function using which we can conduct the paired samples t-test in Python. The syntax is given below,


ttest_rel(arr1, arr2)


  • arr1: It represents an array of sample observations from group 1
  • arr2: It represents an array of sample observations from group 2



# Importing library
import scipy.stats as stats
# pre holds the mileage before 
# applying the different engine oil
pre = [30, 31, 34, 40, 36, 35,
       34, 30, 28, 29]
# post holds the mileage after 
# applying the different engine oil
post = [30, 31, 32, 38, 32, 31,
        32, 29, 28, 30]
# Performing the paired sample t-test
stats.ttest_rel(pre, post)



The test statistic comes out to be equal to 2.584 and the corresponding two-sided p-value is 0.029.

Step 3: Analyzing the output.

The paired samples t-test follows the null and alternative hypotheses:

  • H0: It signifies that the mean pre-test and post-test scores are equal
  • HA: It signifies that the mean pre-test and post-test scores are not equal

As the p-value comes out to be equal to 0.029 which is less than 0.05 hence we reject the null hypothesis. So, we have enough proof to claim that the true mean test score is different for cars before and after applying the different engine oil.

Similar Reads

How to Conduct a Two Sample T-Test in Python
In this article, we are going to see how to conduct a two-sample T-test in Python. This test has another name as the independent samples t-test. It is basically used to check whether the unknown population means of given pair of groups are equal. tt allows one to test the null hypothesis that the means of two groups are equal Assumptions Before con
7 min read
How to Conduct a Wilcoxon Signed-Rank Test in Python?
Prerequisites: Parametric and Non-Parametric Methods, Hypothesis Testing In this article, we are going to see how to conduct a Wilcoxon signed-Rank test in the Python programming language. Wilcoxon signed-rank test, also known as Wilcoxon matched pair test is a non-parametric hypothesis test that compares the median of two paired groups and tells i
3 min read
How to Conduct a One Sample T-Test in Python
In this article, we are going to see how to conduct a one sample T-Test in Python. One Sample T-Test in Python The one-sample t-test is a statistical hypothesis test that can be used to see if the mean of an unknown population differs from a given or known value. In this article let's learn how to perform a one-sample t-test. null hypothesis: the m
2 min read
How to Conduct a Sobel Test in R
In this article, we will learn How to Conduct a Sobel Test in R? Sobel testThis test is a method of testing the significance of a mediation effect. This test is a type of t-test that determines after including the mediator in the model, whether the reduction in the effect of the independent variable is a significant reduction . It also determined w
2 min read
How to Conduct a Jarque-Bera Test in R
In this article, we will discuss What is the Jarque-Bera Test and how we perform the Jarque-Bera Test in R Programming Language. What is the Jarque-Bera Test?The Jarque-Bera test is a statistical test used to assess whether a given dataset follows a normal distribution based on its skewness and kurtosis. Its purpose is to determine if the data conf
7 min read
How to Conduct an Anderson-Darling Test in R
Statistical tests are indispensable tools in data analysis, aiding researchers in determining whether observed data fits a particular distribution. Among these tests, the Anderson-Darling test stands out for its sensitivity to differences in the tails of distributions, making it particularly useful for assessing goodness-of-fit. Here we check the A
5 min read
Kolmogorov-Smirnov Test (KS Test)
The Kolmogorov-Smirnov (KS) test is a non-parametric method for comparing distributions, essential for various applications in diverse fields. In this article, we will look at the non-parametric test which can be used to determine whether the shape of the two distributions is the same or not. What is Kolmogorov-Smirnov Test?Kolmogorov–Smirnov Test
13 min read
Analysis of test data using K-Means Clustering in Python
This article demonstrates an illustration of K-means clustering on a sample random data using open-cv library. Pre-requisites: Numpy, OpenCV, matplot-lib Let's first visualize test data with Multiple Features using matplot-lib tool. # importing required tools import numpy as np from matplotlib import pyplot as plt # creating two test data X = np.ra
2 min read
Python | Create Test DataSets using Sklearn
Python's Sklearn library provides a great sample dataset generator which will help you to create your own custom dataset. It's fast and very easy to use. Following are the types of samples it provides.For all the above methods you need to import sklearn.datasets.samples_generator. C/C++ Code # importing libraries from sklearn.datasets import make_b
3 min read
How to split a Dataset into Train and Test Sets using Python
Here we will discuss how to split a dataset into Train and Test sets in Python. The train-test split is used to estimate the performance of machine learning algorithms that are applicable for prediction-based Algorithms/Applications. This method is a fast and easy procedure to perform such that we can compare our own machine learning model results
3 min read