How to Conduct a One Sample T-Test in Python?


Introduction

One Sample T-Test is a statistical hypothesis test used to determine whether a population mean is significantly different from a hypothesised value. Python gives us the resources we need to carry out this test. In this article, we will walk through how to conduct a One Sample T-Test in Python using the SciPy library.

Conducting a One Sample T-Test

The first step in conducting a One Sample T-Test is to state the null and alternative hypotheses. The null hypothesis is the assumption that the population mean is equal to the hypothesized value. The alternative hypothesis is the opposite of the null hypothesis. It is the assumption that the population mean is not equal to the hypothesized value.

Assuming that we have a set of data and a hypothesized value for the population mean, we can perform a One Sample T-Test to determine whether the population mean is significantly different from the hypothesized value. Here are the steps to conduct a One Sample T-Test in Python using the SciPy library −

Step 1: Import Required Libraries

Importing the essential libraries will be the first step. To perform the One Sample T-Test in Python, we need to import the NumPy and SciPy libraries. While statistical operations are carried out using the SciPy library, mathematical operations are carried out using the NumPy library.

import numpy as np
from scipy.stats import ttest_1samp

Step 2: Load the Data

The data must then be loaded into Python. The loadtxt() method from the NumPy module can help us with this. The file name is passed as a parameter to the loadtxt() function, which produces an array containing the contents.

data = np.loadtxt('data.txt')

Step 3: Define the Hypothesized Value

We must specify the population mean's hypothesised value. This figure will serve as a baseline to assess whether the population mean deviates significantly from the estimated value.

hypothesized_value = 50

Step 4: Perform the One Sample T-Test

We are now prepared to run the One Sample T-Test. The SciPy library's ttest_1samp() function can be used to run the One Sample T-Test. The data and the hypothesised value are the two arguments that the ttest_1samp() function requires.

t_statistic, p_value = ttest_1samp(data, hypothesized_value)

The test statistic and the p-value are the results of the ttest_1samp() function. The t-statistic calculates the standard error of a variance from the sample mean with the hypothesised value. The p-value, under the null hypothesis, is the likelihood of generating a t-statistic that is as severe as the one observed.

Step 5: Interpret the Results

Finally, we must interpret the One Sample T-Test results. By contrasting the p-value and the significance level, we can accomplish this. The significance level is the cutoff below which the null hypothesis is rejected. If the p-value is less than 0.05, which is the conventional significance level, the null hypothesis is rejected.

if p_value <r; 0.05:
   print('Reject Null Hypothesis')
else:
   print('Fail to Reject Null Hypothesis')

We reject the null hypothesis and conclude that the population mean differs significantly from the hypothesised value if the p-value is less than 0.05. If the p-value is higher than or equal to 0.05, we are unable to reject the null hypothesis and come to the conclusion that the population mean, and the hypothesised value do not differ significantly.

The One Sample T-Test assumes that the data is normally distributed, which is important to note. We might need to employ a different statistical test, like the Wilcoxon signed-rank test, if the data are not regularly distributed. The One Sample T-Test also assumes that the data is independent and drawn at random from the population. The test findings may not be accurate if certain presumptions are not met.

Example with code and output

Here is an example of how to use the SciPy library to do a One Sample T-Test in Python −

Let's say we have a set of information that includes the weights of a sample of apples. We wish to determine if the population mean apple weight deviates significantly from 100 grammes. Using Python, we can perform a One Sample T-Test as follows −

import numpy as np
from scipy.stats import ttest_1samp

# Load the data
data = np.array([98, 102, 95, 105, 99, 101, 97, 103, 100, 98])

# Define the hypothesized value
hypothesized_value = 100

# Perform the One Sample T-Test
t_statistic, p_value = ttest_1samp(data, hypothesized_value)

# Interpret the results
if p_value < 0.05:
   print('Reject Null Hypothesis')
else:
   print('Fail to Reject Null Hypothesis')

Output

Fail to Reject Null Hypothesis

Because the p-value in this instance is higher than 0.05, we are unable to rule out the null hypothesis. We conclude that, at the 0.05 level of significance, there is no difference between the population mean weight of apples and 100 grams.

Conclusion

In conclusion, it is rather simple to perform a One Sample T-Test in Python. The SciPy library gives us the tools we need to conduct the test. Simply import the data, provide the hypothesised value, run the One Sample T-Test using the ttest_1samp() function, then compare the p-value to the significance level to interpret the findings. These stages allow us to assess whether the population mean differs considerably from the hypothesised value.

Updated on: 13-Jul-2023

620 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements