API · SciPy
SciPy stats.zscore Function
This tutorial will tell what is stats z-score function by using the Scipy module in Python
On this page
The z-score standardizes a value by measuring its distance from the data mean in standard deviation units:
z = (X - μ) / σ
Here, X is a value, μ is the mean, and σ is the standard deviation. A z-score describes how far a value lies from the mean and is useful for comparing observations on the same scale. This guide shows how to calculate z-scores with SciPy for arrays and a pandas DataFrame.
Using scipy.stats.zscore
The scipy.stats.zscore function computes standardized scores from array-like input using that input’s mean and standard deviation. Its call signature is scipy.stats.zscore(a, axis=0, ddof=0, nan_policy='propagate').
| Parameter | Purpose |
|---|---|
a |
Required array-like input containing the observations. |
axis |
Axis along which to calculate the mean and standard deviation. The default is 0; for a one-dimensional array this uses all values, while for a multidimensional array it calculates separately along axis 0. Use axis=None to calculate over all values together. |
ddof |
Delta degrees of freedom used by the standard deviation calculation. The default 0 uses the population standard deviation; set ddof=1 for the sample standard deviation. |
nan_policy |
Controls how NaN values are handled. propagate (the default) lets NaNs affect the results along their calculation axis; raise reports an error if NaNs are present; omit excludes NaNs from the calculation and leaves their output positions as NaN. |
Only a is required. Choose axis based on whether each row, each column, or the entire input should share a mean and standard deviation.
Calculate z-scores for a one-dimensional array
For a one-dimensional array, the default axis=0 uses the full set of values. Run this example with NumPy and SciPy installed:
import numpy as np
from scipy import stats
values = np.array([10, 20, 30, 40, 50])
scores = stats.zscore(values)
print(np.round(scores, 3))
This example is provided as runnable code; its output has not been executed in this writing step. A negative score means the value is below the mean, a positive score means it is above the mean, and a score of zero marks the mean. The magnitude gives the distance in standard deviation units.
Calculate z-scores for a multidimensional array
For a two-dimensional array, the default axis=0 computes each column’s scores across its rows. The result keeps the input shape. Pass axis=1 to calculate separately across each row, or axis=None to use one mean and standard deviation for the entire array.
import numpy as np
from scipy import stats
data = np.array([[1, 10], [2, 20], [3, 30]])
scores = stats.zscore(data, axis=0)
print(np.round(scores, 3))
The code illustrates column-wise standardization and is unexecuted here; no output is asserted.
Calculate z-scores for a pandas DataFrame
DataFrame.apply() applies a function to each column by default (axis=0). Applying stats.zscore this way standardizes each column independently. This fixed DataFrame makes the input reproducible:
import pandas as pd
from scipy import stats
data = pd.DataFrame({"height": [150, 160, 170], "weight": [50, 60, 70]})
scores = data.apply(stats.zscore)
print(scores.round(3))
This example is unexecuted here, so no displayed result is claimed. Each column has its own mean and standard deviation; use data.apply(stats.zscore, axis=1) when the intended calculation is across each row instead.