Z-Test vs T-Test

Maths: Statistics for machine learning

2 min read

Published Oct 22 2025, updated Oct 23 2025


40
0
0
0

Machine LearningMathsNumPyPandasPythonStatistics

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 np
from scipy import stats

# Example data
data = np.array([52, 55, 54, 53, 56, 58, 54, 52, 55, 57])

# Population mean
mu_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 = 3
n = 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.


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact