Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › Data Analysis

SciPy stats.zscore: Calculate and Use Z-Scores

By Freedom251 Team · · 2 min read

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use scipy.stats.zscore to express each value as its distance from a selected mean in standard-deviation units. The key choices are which axis defines the comparison group, whether to use the default population-style standard deviation or the ddof=1 correction, and how to handle NaNs.

Calculate z-scores with scipy.stats.zscore

The function computes standardized scores from an array-like input. For each value, it subtracts the relevant mean and divides by the relevant standard deviation. A positive score is above that mean; a negative score is below it.

As an Amazon Associate I earn from qualifying purchases.

import numpy as np
from scipy import stats

a = np.array([10, 12, 14, 16, 18])
z = stats.zscore(a)

print(z)

This uses the documented defaults: axis=0, ddof=0, and nan_policy='propagate'. The output contains z-scores corresponding to the input values. See the SciPy zscore API reference for the signature and examples.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the axis that matches the comparison group

For a multidimensional array, the axis determines which slices are standardized. Decide which observations should share a mean and standard deviation before choosing the axis; axis is a statistical choice, not just an output-shape setting.

#1 Best Overall
  • axis=0 (the default) computes statistics along axis 0, standardizing values within the corresponding slices.
  • axis=1 computes along axis 1. The SciPy reference demonstrates this setting with ddof=1.
  • axis=None computes the statistics over the entire array as one collection.

For example, if rows represent different people and columns represent measurements, choose the axis according to whether each measurement should be compared across people or each person’s measurements should be compared with one another.

Set ddof for the standard-deviation convention you need

ddof adjusts the degrees of freedom used in the standard deviation calculation. Its default is 0. When you intend the sample standard deviation convention using n−1 degrees of freedom, set ddof=1:

Rank #2
Sale
Statistics Laminate Reference Chart: Parameters, Variables, Intervals, Proportions (Quickstudy: Academic )
  • This guide is a perfect overview for the topics covered in introductory statistics courses.
z_sample = stats.zscore(a, ddof=1)

Changing ddof changes the standard deviation used as the divisor and therefore changes the resulting scores. Make the choice explicit when it matters to interpreting or reproducing an analysis.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose how NaN values are handled

The default nan_policy='propagate' propagates NaNs through the calculation. The API also supports two alternatives:

Rank #3
  • nan_policy='raise' raises an error if NaNs are present.
  • nan_policy='omit' excludes NaNs from the calculations for non-NaN values. NaN positions in the output remain NaN.
a_with_nan = np.array([10.0, 12.0, np.nan, 16.0])
z_omit = stats.zscore(a_with_nan, nan_policy='omit')

Select a policy deliberately: omission allows scores for observed values to be calculated without the missing entries affecting those calculations, while preserving the missing positions in the result.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Data AnalysisPythonSciPyStatistics

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.