Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.zscore

Computes standardized scores for sample values by comparing each with the sample mean and standard deviation. The axis, degrees-of-freedom correction, and NaN policy are configurable.

How it works

zscore converts values into standardized scores by subtracting the mean and dividing by the standard deviation along the selected axis. Use ddof to apply a degrees-of-freedom correction and nan_policy to control missing-value handling.

Syntax

scipy.stats.zscore(a, axis=0, ddof=0, nan_policy='propagate')

Parameters

NameTypeDescription
aarray_likeArray-like object containing the sample data.
axisint or None, optionalAxis along which to operate; use None to operate over the whole array. Default: 0.
ddofint, optionalDegrees of freedom correction in the standard-deviation calculation. Default: 0.
nan_policy{'propagate', 'raise', 'omit'}, optionalHow to handle NaNs: 'propagate', 'raise', or 'omit'. Default: 'propagate'.

Returns

array_like: z-scores standardized by the mean and standard deviation of input array a.

Key points

  • Each output expresses a sample value relative to its sample mean and standard deviation.
  • a accepts array-like sample data, including ordinary sequences that can be interpreted as an array.
  • axis chooses the dimension used for the standardization; its default is 0.
  • Use axis=None to calculate the mean and standard deviation over the complete input.
  • ddof adjusts the standard-deviation calculation for a chosen degrees-of-freedom correction.
  • The result is an array-like collection of z-scores standardized using the input's mean and standard deviation.

Common mistakes

  • Choose nan_policy deliberately: the default propagates missing values, raise reports them as errors, and omit excludes them from the calculations.
  • With nan_policy='omit', missing entries remain missing in the result even though they do not affect scores for non-missing entries.

Example: Standardize a one-dimensional sample

import numpy as np
from scipy.stats import zscore

samples = np.array([10.0, 12.0, 14.0, 16.0])
standardized = zscore(samples)
print(standardized)

Standardizes each value relative to the sample mean and population standard deviation.

When to use it

Put numeric features on a comparable standardized scale.

Feature preprocessing

Identify observations unusually far from a sample mean in standard-deviation units.

Outlier screening

Standardize columns, rows, or the entire array by selecting axis.

Multidimensional data

Interview questions

QWhat does scipy.stats.zscore compute, and what are its defaults?

The function computes each value’s standardized distance from the sample mean in units of standard deviation. By default it operates along axis 0, uses ddof=0, and propagates NaNs.

Follow-up: How would you standardize the entire array instead of each column? Pass axis=None so the calculation uses all values in a.

QHow does axis affect z-score calculation?

axis selects the dimension along which means and standard deviations are calculated. Setting axis=None flattens the operation conceptually over the whole input array.

Follow-up: What changes when axis=1 for a two-dimensional array? Each row is standardized independently across its columns.

QCompare the nan_policy options.

nan_policy controls missing values: propagate returns NaNs, raise throws an error, and omit ignores NaNs while computing non-missing scores. Missing positions still produce NaN with omit.

Follow-up: When would raise be preferable to omit? Use raise when missing data indicates invalid input that should stop processing rather than be silently excluded.

  • scipy.stats.gzscore
  • scipy.stats.mstats.zscore

Source: zscore — SciPy v1.18.0 Manual. Checked 2026-10-15.