Put numeric features on a comparable standardized scale.
Feature preprocessing
SciPy · Function
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.
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.
scipy.stats.zscore(a, axis=0, ddof=0, nan_policy='propagate')| Name | Type | Description |
|---|---|---|
a | array_like | Array-like object containing the sample data. |
axis | int or None, optional | Axis along which to operate; use None to operate over the whole array. Default: 0. |
ddof | int, optional | Degrees of freedom correction in the standard-deviation calculation. Default: 0. |
nan_policy | {'propagate', 'raise', 'omit'}, optional | How to handle NaNs: 'propagate', 'raise', or 'omit'. Default: 'propagate'. |
array_like: z-scores standardized by the mean and standard deviation of input array a.
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.
Feature preprocessing
Outlier screening
Multidimensional data
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.
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.
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.gzscorescipy.stats.mstats.zscore