Z-Test vs T-Test
Maths: Statistics for machine learning
2 min read
Published Oct 22 2025, updated Aug 17 2026
Guide Sections
Guide Comments
Both Z-tests and T-tests are statistical hypothesis tests used to determine whether there’s a significant difference between sample data and a known or assumed population parameter.
In simple terms:
“Both tests check if your sample mean is far enough from what you expect —
the Z-test uses the population standard deviation, while the T-test uses the sample standard deviation.”
The Z-Test
When to Use:
- The population standard deviation (σ) is known
- The sample size is large (n ≥ 30)
- The data are normally distributed (or approximately normal via CLT)
Example:
“Is the average test score of 50 students different from the known population mean of 75 (σ = 10)?”
Use Z-test because σ is known and sample is large.
The T-Test
When to Use:
- The population standard deviation (σ) is unknown
- You have to estimate σ using the sample standard deviation (s)
- The sample size is small (n < 30)
- Data are approximately normally distributed
The t-distribution has heavier tails than the normal distribution, accounting for extra uncertainty from estimating σ.
Distribution Used
Test | Distribution | Description |
Z-test | Standard Normal (Z) | Bell-shaped, fixed shape |
T-test | Student’s t | Similar to normal, but heavier tails; shape changes with sample size (degrees of freedom) |
As n increases, the t-distribution approaches the normal distribution, meaning the T-test ≈ Z-test for large samples.
Typical Use Cases
Scenario | Use | Why |
Population σ is known | Z-test | We can use exact population variability |
Population σ is unknown | T-test | Must estimate variability from sample |
Small sample (n < 30) | T-test | Accounts for higher uncertainty |
Large sample (n ≥ 30) | Z-test (or T-test, both valid) | CLT ensures normal approximation |
Comparing two means | Two-sample T-test | Usually σ unknown |
Paired data (before/after) | Paired T-test | Works on differences |
Comparing proportions | Z-test | Uses population or large sample proportions |
Example in Python
Example: Compare Sample Mean to Population Mean
import numpy as npfrom scipy import stats# Example datadata = np.array([52, 55, 54, 53, 56, 58, 54, 52, 55, 57])# Population meanmu_0 = 50# One-sample T-test (σ unknown)t_stat, p_val = stats.ttest_1samp(data, mu_0)print(f"T-test → t={t_stat:.2f}, p={p_val:.4f}")# Approximate Z-test (σ assumed known)sigma = 3n = len(data)z_stat = (np.mean(data) - mu_0) / (sigma / np.sqrt(n))p_val_z = 2 * (1 - stats.norm.cdf(abs(z_stat)))print(f"Z-test → z={z_stat:.2f}, p={p_val_z:.4f}")Both give similar results when n is large, but the T-test is more robust when σ is unknown.