T-Tests (One-sample, Two-sample independent & Paired)0%

T-Tests (One-sample, Two-sample independent & Paired)

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

T-Tests Implementation in Python (scipy.stats)

Reading Time: 8 Mins | Level: Intermediate


1. When to Use a T-Test?

A Student's t-test determines whether the means of two groups are statistically different from each other.

The 3 Variations of T-Tests:

  1. 1
    One-Sample T-Test: Compares the mean of a single group against a known population benchmark $\mu_0$.
  2. 2
    Two-Sample Independent T-Test: Compares the means of two distinct, unrelated groups (e.g. Website Variant A vs Variant B).
  3. 3
    Paired T-Test: Compares measurements taken on the same subjects before and after an intervention (e.g. Employee skills test Before vs After training).

2. Python Implementation with scipy.stats

Python
import numpy as np
from scipy import stats
 
# Variant A (Control group conversions)
group_A = np.array([45, 52, 48, 55, 49, 53, 50, 47, 51, 54])
# Variant B (New feature group conversions)
group_B = np.array([58, 62, 59, 65, 57, 63, 61, 60, 64, 59])
 
# Perform Two-Sample Independent T-Test
# equal_var=False performs Welch's t-test (best practice!)
t_stat, p_val = stats.ttest_ind(group_A, group_B, equal_var=False)
 
print(f"T-Statistic: {t_stat:.4f}")
print(f"P-Value: {p_val:.6f}")
 
if p_val < 0.05:
print("Conclusion: Reject H0! Variant B produces statistically significantly higher conversions.")
else:
print("Conclusion: Fail to reject H0.")

Output:

Output
T-Statistic: -8.8415
P-Value: 0.000000
Conclusion: Reject H0! Variant B produces statistically significantly higher conversions.

Interactive Quiz

Multiple Choice Questions

Question 1
Which function in scipy.stats conducts an independent two-sample t-test between two groups?
Question 2
Why is setting equal_var=False recommended when running stats.ttest_ind()?
Question 3
Which t-test is appropriate when testing the test scores of the same 30 students before and after a training workshop?
Question 4
What test statistic is evaluated against Student's t-distribution in a t-test?
Question 5
If the p-value returned by stats.ttest_1samp() is 0.42, what is the proper conclusion at $alpha = 0.05$?

Next Lesson

Chi-Square Test of Independence & ANOVA Basics

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext