t-tests
Published Jan 24, 2025
Contribute to Docs
The t-test in Statsmodels is a statistical method to determine whether the means of one or two groups differ significantly. It is commonly used to compare the mean of a sample to a specified value, the means of two independent groups, or the means of paired data.
Syntax
statsmodels.stats.weightstats.ttest_ind(x1, x2, alternative='two-sided', usevar='pooled', weights=(None, None))
x1
,x2
: Arrays containing sample data for the groups being compared.alternative
: The hypothesis to test. Options include:'two-sided'
: Default, tests for any difference.'larger'
: Tests if the mean ofx1
is greater thanx2
.'smaller'
: Tests if the mean ofx1
is less thanx2
.
usevar
: Assumptions about variance. Options include:'pooled'
: Default, assumes equal variance.'unequal'
: Does not assume equal variance.
weights
: A tuple specifying weights forx1
andx2
, used in weighted t-tests.
Example
In this example, a one-sample t-test is performed to determine whether the mean of a sample dataset is significantly different from 10:
import numpy as npfrom scipy.stats import ttest_1samp# Sample datadata = np.array([9.5, 10.1, 9.8, 10.2, 9.9, 10.0, 9.7])# Hypothesized population meanpopulation_mean = 10# Perform one-sample t-testt_stat, p_value = ttest_1samp(data, population_mean)# Output resultsprint(f"t-statistic: {t_stat}")print(f"P-value: {p_value}")# Interpretationalpha = 0.05if p_value < alpha:print("Reject the null hypothesis: The sample mean is significantly different from 10.")else:print("Fail to reject the null hypothesis: No significant difference from 10.")
The code above generates the following ouput:
t-statistic: -1.2545000963743562P-value: 0.25631545891582497Fail to reject the null hypothesis: No significant difference from 10.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours