How to perform a two-sample t-test in Python?

王林
Release: 2023-08-31 21:49:03
forward
1052 people have browsed it

How to perform a two-sample t-test in Python?

Introduction

Use a two-sample t-test to statistically compare the means of two groups to see if there is a significant difference between them. This test is often used in scientific research to determine whether two groups are significantly different on the basis of a continuous variable. In this article, we will learn how to perform a two-sample t-test using Python’s scipy.stats module.

Perform two-sample T-test

Before proceeding with the implementation, let us first understand the theoretical basis of the two-sample t-test. This test assumes that the two sample populations are normally distributed and have similar variances. The null hypothesis is that the means of the two groups are equal, and the alternative hypothesis is that the means of the two groups are not equal. The test statistic is calculated by dividing the difference in means between two groups by the difference in standard errors. We reject the null hypothesis and conclude that if the estimated t-value is above the critical value, then the means of the two groups are significantly different.

Let’s take a look at how to perform a two-sample t test in Python. We will need the scipy.stats module, which helps provide a function called ttest_ind. It takes as input two arrays representing two samples and returns t and p values.

Step 1: Import the required libraries

Importing the necessary libraries will be the first step. To perform a two-sample t-test in Python, we need to import the NumPy and SciPy libraries. Statistical operations were performed using the SciPy library, while mathematical operations were performed using the NumPy library.

import NumPy as np
from scipy.stats import ttest_ind
Copy after login

Step 2: Generate variables

Next let’s create two random samples with the same mean and standard deviation -

np.random.seed(42)
sample1 = np.random.normal(loc=10, scale=2, size=100)
sample2 = np.random.normal(loc=10, scale=2, size=100)
Copy after login

Here, we use the np.random.normal function to generate two samples of size 100 each, with a mean of 10 and a standard deviation of 2. We set the random seed to 42 to ensure reproducible results.

Now, let’s do the t-test -

t_stat, p_value = ttest_ind(sample1, sample2)
Copy after login

Step 3: Interpret the results

ttest_ind function returns two values ​​with codes: t value and p value. The t-value measures the difference between two sample means, while the p-value measures the statistical significance of the difference.

Finally, let’s print the results -

print("t-value: ", t_stat)
print("p-value: ", p_value)
Copy after login

This will output the t value and p value -

t-value: 0.086
p-value: 0.931
Copy after login

Since the t values ​​in this code are small, we can conclude that the means of the two samples are fairly comparable. Because the p-value is too large, the difference between the two values ​​is not equally significant.

Remember that the t-test assumes that the variances of the two groups are equal. If this assumption is broken, you can use Welch's t-test, which is a variation of the t-test that does not assume equal variances. The ttest_ind_from_stats method for Welch's t-test is also available in the scipy.stats module. The mean, standard deviation, and sample size of the two groups are the inputs to this function.

Let’s see how to perform Welch’s t-test in Python

mean1, std1, size1 = 10, 2, 100
mean2, std2, size2 = 10, 3, 100
t_stat, p_value = ttest_ind_from_stats(mean1, std1, size1, mean2, std2, size2, equal_var=False)
print("t-value: ", t_stat)
print("p-value: ", p_value)
Copy after login

This will output the t value and p value -

t-value: -0.267
p-value: 0.790
Copy after login

According to the data, the t value in this example is negative, indicating that the mean of sample 1 is slightly lower than the mean of sample 2. However, a very high p-value indicates that the difference in means is not statistically significant.

in conclusion

In summary, the two-sample t-test is an effective statistical tool that allows us to compare the means of two groups and determine whether they are significantly different. Python has many libraries and functions for performing t-tests, including the scipy.stats module we use in this article. The t-test makes various assumptions, including normality and equal variances, which should be verified before the test is run. Furthermore, the specific research question under consideration and the limitations of the study should always be considered when interpreting results.

The above is the detailed content of How to perform a two-sample t-test in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!