Calculate Z-Score in Python

by Sudipta Deb | Dec 18, 2024 | Data Science, Data Visualization, Probability, Python | 0 comments

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

A Z-score, or standard score, indicates the distance of a data point from the mean. More formally, it quantifies the number of standard deviations a raw score deviates from the population mean.

A Z-score can be positioned on a normal distribution curve. To calculate a Z-score, it is essential to know the mean μ and the population standard deviation σ. These scores range from -3 standard deviations (which would fall to the far left of the normal distribution curve) up to +3 standard deviations (which would fall to the far right of the normal distribution curve).

Z-scores are a way to compare results to a “normal” population. Results from tests or surveys have thousands of possible results and units; those results can often seem meaningless. For example, knowing that someone’s earning is 100 K dollar might be good information, but if you want to compare it to the “average” person’s earning, looking at a vast table of data can be overwhelming. The scores can tell you where that person’s earning is compared to the average population’s mean earning.

The form you have selected does not exist.

Sample Code

I normally put all my codes in my personal Github repository, so that I can easily share with others. Here is the Github Code Repo where you can find the code samples used in this blog post.

Z-Score.ipynb

Let's Understand The Problem Statement

Suppose we know that the GRE scores are normally distributed with mean 157 and standard deviation 17 and IELTS scores are normally distributed with mean 6.8 and standard devialtion 1.2.

A university provides admission to their courses only on the basis of GRE and IELTS scores. The college admission committee decided to give the top performer scholarship to the student who has performed the best among all applicants. The highest score received from applicants who appeared for GRE is 182 and the highest score received from applicants who appeared for IELTS is 9.

What should be the strategy for the college to find the best candidate!

Let's First Visualize The Data

# Plot the two distribution for GRE and IELTS
from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(1,2,figsize=(15,5))
x=np.linspace(20,300,500)
ax1.plot(x,norm.pdf(x,157,17),color="b")
ax1.set_title("GRE")
ax1.set_xlabel("GRE Score")
ax1.set_ylabel("Probability")
ax1.axvline(182,linestyle="--",color="green")
x1=np.linspace(2,10,500)
ax2.plot(x1,norm.pdf(x1,6.8,1.2),color="r")
ax2.set_title("IELTS")
ax2.set_xlabel("IELTS Score")
ax2.set_ylabel("Probability")
ax2.axvline(9,linestyle="--",color="green")
plt.show()

Now The Question Is - How Far From The Mean The Two High Scores Are? i.e. Z-Score

We can find out the Z-Scores in the below way. Remember the formula to find out the Z-Score is - z = (x – μ) / σ

  • μ - Mean Value
  • σ - Standard Deviation
# Find the Z-Score of the hihgest score in GRE and IELTS among all applicants
top_gre = (182 - 157) / 17
print("The Z-Score of the highest score in GRE among all applicants: ",top_gre);
top_ielts = (9 - 6.8) / 1.2
print("The Z-Score of the highest score in IELTS among all applicants: ",top_ielts);

Here is the result -

The Z-Score of the highest score in GRE among all applicants: 1.4705882352941178
The Z-Score of the highest score in IELTS among all applicants: 1.8333333333333335

Now comparing the Z-Scores, it is very much clear that student scored highest in IELTS is definitely higher that the student scored highest in GRE. But let's visualize this in a graph.

Visualize The Z-Score

fig, ax = plt.subplots()
x = np.linspace(-2,2,50)
ax.plot(x,norm.pdf(x,0,1),color="b")
ax.set_title("Standard Normal Distribution")
ax.set_xlabel("Z-Score")
ax.set_ylabel("Probability")
ax.axvline(top_gre,linestyle="--",color="green")
ax.axvline(top_ielts,linestyle="--",color="red")
plt.show()

From the graph also it is very clear that student who scored highest in IELTS is definitly the student who should receive the scholarship from the University.

Interpretation Of Z-Score

  • Z-Score = 0: A Z-score of 0 indicates that the data point is exactly at the mean of the distribution.
  • Positive Z-Score: A positive Z-score indicates that the data point is above the mean. For example, a Z-score of 2.5 means the data point is 2.5 standard deviations above the mean.
  • Negative Z-Score: A negative Z-score indicates that the data point is below the mean. For example, a Z-score of -3.7 means the data point is 3.7 standard deviations below the mean.
  • Magnitude of Z-Score: The magnitude of the Z-score shows how far away the data point is from the mean. A larger absolute value of the Z-score indicates that the data point is farther from the mean, while a smaller absolute value indicates it is closer.

What Is 68-95-99.7 Rule In Relation To Z-Scores?

The 68-95-99.7 Rule, also known as the Empirical Rule, states that:

  • About 68% of data falls within 1 standard deviation from the mean.
  • About 95% falls within 2 standard deviations.
  • About 99.7% falls within 3 standard deviations.

Conclusion

Z-score is an essential statistical tool that provides a standardized measure of how a data point deviates from the mean of a distribution. Its is employed in different industries, including finance, education, health, and engineering, making it a versatile tool for data analysis. By translating raw data into a standardized scale, Z-scores assist the comparison of diverse datasets and the identification of outliers. This uniformity is vital for statistical testing, quality control, and risk management. Understanding how to compute and understand Z-scores enables for more effective data analysis and decision-making.

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

0 Comments

Leave a Reply

Written by Sudipta Deb

Enterprise Cloud Architect, Content Creator, 20x Salesforce Certified, 1x Google Cloud Certified, 2x Copado Certified

Related Posts

4 Important Probability Distributions – Normal, Binomial, Poisson, Bernoulli

4 Important Probability Distributions – Normal, Binomial, Poisson, Bernoulli

A probability distribution specifies the likelihood of all possible outcomes of a random variable. The distribution may be classified as either continuous or discrete based on the values assumed by a random variable. Various forms of probability distributions exist. In this post, I will examine Normal, Binomial, Poisson and Bernoulli distribution and demonstrate how to utilize Python to plot it.

read more...
Detect Outliers Using Z-Score In Python

Detect Outliers Using Z-Score In Python

In a Normal Distribution, it is estimated that 68% of the data points lie between +/- 1 standard deviation, 95% of the data points lie between +/- 2 standard deviation, 99.7% of the data points lie between +/-3 standard deviation.

Now if the Z-Score of a data point is more than 3, it indicates that the data point is quite different from rest of the data points. Such a data points are called Outliers.

read more...

0 Comments

Leave a Reply