Search...
Sign In
Statistics with Python Data Analysis Tutorial Python – Data visualization tutorial NumPy Pandas OpenCV R Machine
Z-test : Formula, Types, Examples
Last Updated : 24 Jul, 2025
A Z-test is a type of hypothesis test that compares the sample’s average to the
population’s average and calculates the Z-score and tells us how much the sample
average is different from the population average by looking at how much the data
normally varies. It is particularly useful when the sample size is large >30. This Z-Score is
also known as Z-Statistics formula is:
ˉ−μ
x
Z-Score = σ
where,
ˉ : mean of the sample.
x
μ : mean of the population.
σ : Standard deviation of the population.
Let's understand with the help of example The average family annual income in India is
200k with a standard deviation of 5k and the average family annual income in Delhi is
300k. Then Z-Score for Delhi will be.
ˉ−μ
x
Z-Score =
σ
300 − 200
=
5
= 20
This indicates that the average family's annual income in Delhi is 20 standard deviations
above the mean of the population (India).
For a z-test to provide reliable results these assumptions must be met:
1. Normal Distribution: The population from which the sample is drawn should be
approximately normally distributed.
2. Equal Variance: The samples being compared should have the same variance.
3. Independence: All data points should be independent of one another.
Steps to perform Z-test
1. First we identify the null and alternate hypotheses.
2. Then we determine the level of significance (α).
3. Next we find the critical value of Z in the z-test.
4. Then we calculate the z-test statistics using the formula :
(x−μ)
Z=
(σ/ n)
Where:
ˉ : mean of the sample.
x
μ : mean of the population.
σ : Standard deviation of the population.
n : sample size.
5. Now we compare with the hypothesis and decide whether to reject or not reject the null
hypothesis.
Type of Z-test
There are mainly two types of Z-tests. Let's understand them one by one:
1. One Sample Z test
A one-sample Z-test is used to determine if the mean of a single sample is significantly
different from a known population mean. When to Use:
The population standard deviation is known.
The sample size is large (usually n>30).
The data is approximately normally distributed.
Suppose a company claims that their new smartphone has an average battery life of 12
hours. A consumer group tests 100 phones and finds an average battery life of 11.8 hours
with a known population standard deviation of 0.5 hours.
Step 1: Hypotheses:
H0 : μ = 12:
H1 : μ =
12
Step2: Calculate the Z-Score:
We can calculate Z-score using the formula:
x−μ
z= σ
n
ˉ
Where: x = 11.8 , μ = 12, σ = 0.5 and n = 100
After putting the value we get:
11.8−12
z= 0.5 = −4
100
Step3: Decision
Since ∣Z ∣ = 4 > 1.96 (critical value forα = 0.05) we reject H0 indicate significant
evidence against the company's claim.
Now let's implement this in Python using the Statsmodels and Numpy Library:
import numpy as np
from statsmodels.stats.weightstats import ztest
data = [11.8] * 100
population_mean = 12
population_std_dev = 0.5
z_statistic, p_value = ztest(data, value=population_mean)
print(f"Z-Statistic: {z_statistic:.4f}")
print(f"P-Value: {p_value:.4f}")
alpha = 0.05
if p_value < alpha:
print("Reject the null hypothesis: The average battery life is different
from 12 hours.")
else:
print("Fail to reject the null hypothesis: The average battery life is not
significantly different from 12 hours.")
Output:
Z-Statistic: -560128131373970.2500
P-Value: 0.0000
Reject the null hypothesis: The average battery life is different from 12 hour
2. Two-sampled z-test
In this test we have provided 2 normally distributed and independent populations and we
have drawn samples at random from both populations. Here we consider u1 and u2 to be
the population mean and X1 and X2 to be the observed sample mean. Here our null
hypothesis could be like this:
H0 : μ1 − μ2 = 0 and alternative hypothesis
H1 : μ1 − μ2
=0
and the formula for calculating the z-test score:
(X1 −X2 )−(μ1 −μ2 )
Z=
σ12 σ2
n1
+ n2
2
where σ1 and σ2 are the standard deviation and n1 and n2 are the sample size of
population corresponding to u1 and u2 . Let's look at the example to understand:
Example: There are two groups of students preparing for a competition: Group A and
Group B. Group A has studied offline classes, while Group B has studied online classes.
After the examination the score of each student comes. Now we want to determine
whether the online or offline classes are better.
Group A: Sample size = 50, Sample mean = 75, Sample standard deviation = 10
Group B: Sample size = 60, Sample mean = 80, Sample standard deviation = 12
Assuming a 5% significance level perform a two-sample z-test to determine if there is a
significant difference between the online and offline classes.
Solution:
Step 1: Null & Alternate Hypothesis
Null Hypothesis: There is no significant difference between the mean score between
the online and offline classes
μ1 − μ2 = 0
Alternate Hypothesis: There is a significant difference in the mean scores between the
online and offline classes.
μ1 − μ2
=0
Step 2: Significance Level
Significance Level: 5%
α = 0.05
Step 3: Z-Score
(x1 − x2 ) − (μ1 − μ2 )
Z-score =
σ12 σ22
n1 + n1
(75 − 80) − 0
=
102 122
50
+ 60
−5
=
2 + 2.4
−5
=
2.0976
= −2.384
Step 4: Check to Critical Z-Score value in the Z-Table for alpha/2 = 0.025
Critical Z-Score = 1.96
Step 5: Compare with the absolute Z-Score value
absolute(Z-Score) > Critical Z-Score
Sow we reject the null hypothesis and there is a significant difference between the
online and offline classes.
Now we will implement the two sampled z-test using numpy and scipy.
import numpy as np
import scipy.stats as stats
n1 = 50
x1 = 75
s1 = 10
n2 = 60
x2 = 80
s2 = 12
D = 0
alpha = 0.05
z_score = ((x1 - x2) - D) / np.sqrt((s1**2 / n1) + (s2**2 / n2))
print('Z-Score:', np.abs(z_score))
z_critical = stats.norm.ppf(1 - alpha/2)
print('Critical Z-Score:',z_critical)
if np.abs(z_score) > z_critical:
print("Reject the null hypothesis.")
else:
print("Fail to reject the null hypothesis.")
Output:
Z-Score: 2.3836564731139807
Critical Z-Score: 1.959963984540054
Reject the null hypothesis.
So, There is a significant difference between the online and offline classes.
The Z-Table
Z-Table
Solved examples
Problem 1: A company claims that the average battery life of their new smartphone is
12 hours. A consumer group tests 100 phones and finds the average battery life to be
11.8 hours with a population standard deviation of 0.5 hours. At a 5% significance
level, is there evidence to refute the company's claim?
Solution:
Step 1: State the hypotheses
H0 : μ = 12 (null hypothesis)
H1 : μ =
12 (alternative hypothesis)
Step 2: Calculate the Z-score
ˉ−μ
x
Z= σ
n
11.8−12
= 0.5
100
−0.2
= 0.05
= −4
Step 3: Find the critical value (two-tailed test at 5% significance)
Z0.025 = ±1.96
Step 4: Compare Z-score with critical value
|-4| > 1.96, so we reject the null hypothesis.
Conclusion: There is sufficient evidence to refute the company's claim about battery
life.
Problem 2: A researcher wants to compare the effectiveness of two different
medications for reducing blood pressure. Medication A is tested on 50 patients,
resulting in a mean reduction of 15 mmHg with a standard deviation of 3 mmHg.
Medication B is tested on 60 patients, resulting in a mean reduction of 13 mmHg with a
standard deviation of 4 mmHg. At a 1% significance level, is there a significant
difference between the two medications?
Solution:
Step 1: State the hypotheses
H0 : μ1 − μ2 = 0 (null hypothesis)
H1 : μ1 − μ2
=0 (alternative hypothesis)
Step 2: Calculate the Z-score
ˉ1 −x
x ˉ2
Z=
σ2 σ2
1 + n2
n1 2
15−13
= 32 2
50
+ 460
2
= 0.18+0.2667
2
= 0.6455
= 3.10
Step 3: Find the critical value (two-tailed test at 1% significance)
Z0.005 = ±2.576
Step 4: Compare Z-score with critical value
3.10 > 2.576, so we reject the null hypothesis.
Hello students. In this video, we are going to discuss
Z-test : Formula, Types, Examples
Comment More info
Explore
Introduction to Machine Learning
Python for Machine Learning
Introduction to Statistics
Feature Engineering
Model Evaluation and Tuning
Data Science Practice
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Company Explore Tutorials Courses Videos Preparation
About Us POTD Programming IBM Certification DSA Corner
Legal Job-A-Thon Languages DSA and Placements Python Aptitude
Privacy Policy Community DSA Web Development Java Puzzles
Contact Us Blogs Web Technology Programming C++ GfG 160
Advertise with us Nation Skill Up AI, ML & Data Science Languages Web Development DSA 360
GFG Corporate DevOps DevOps & Cloud Data Science System Design
Solution CS Core Subjects GATE CS Subjects
Campus Training Interview Preparation Trending
Program GATE Technologies
Software and Tools
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved