bundles / scipy 1.18.0 / scipy / stats / _stats_py / zscore
function
scipy.stats._stats_py:zscore
source: /scipy/stats/_stats_py.py:2671
Signature
def zscore(a, axis = 0, ddof = 0, nan_policy = propagate)Summary
Compute the z score.
Extended Summary
Compute the z score of each value in the sample, relative to the sample mean and standard deviation.
Parameters
a: array_likeAn array like object containing the sample data.
axis: int or None, optionalAxis along which to operate. Default is 0. If None, compute over the whole array
a.ddof: int, optionalDegrees of freedom correction in the calculation of the standard deviation. Default is 0.
nan_policy: {'propagate', 'raise', 'omit'}, optionalDefines how to handle when input contains nan. 'propagate' returns nan, 'raise' throws an error, 'omit' performs the calculations ignoring nan values. Default is 'propagate'. Note that when the value is 'omit', nans in the input also propagate to the output, but they do not affect the z-scores computed for the non-nan values.
Returns
zscore: array_likeThe z-scores, standardized by mean and standard deviation of input array
a.
Notes
This function preserves ndarray subclasses, and works also with matrices and masked arrays (it uses asanyarray instead of asarray for parameters).
Array API Standard Support
zscore has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ✅ JAX ✅ ✅ Dask ✅ n/a ==================== ==================== ====================
zscore also accepts MArrays backed by the backends indicated above; masked values will be treated as though they were not present.
See dev-arrayapi for more information.
Examples
import numpy as np a = np.array([ 0.7972, 0.0767, 0.4383, 0.7866, 0.8091, 0.1954, 0.6307, 0.6599, 0.1065, 0.0508]) from scipy import stats✓
stats.zscore(a)
✗array([ 1.12725, -1.247 , -0.05543, 1.09232, 1.16646, -0.85585,
0.57858, 0.67481, -1.1488 , -1.33234])
Computing along a specified axis, using n-1 degrees of freedom
(``ddof=1``) to calculate the standard deviation:
b = np.array([[ 0.3148, 0.0478, 0.6243, 0.4608], [ 0.7149, 0.0775, 0.6072, 0.9656], [ 0.6341, 0.1403, 0.9759, 0.4064], [ 0.5918, 0.6948, 0.904 , 0.3721], [ 0.0921, 0.2481, 0.1188, 0.1366]])✓
stats.zscore(b, axis=1, ddof=1)
✗array([[-0.19265, -1.28415, 1.0726 , 0.4042 ],
[ 0.33048, -1.37381, 0.04251, 1.00081],
[ 0.26796, -1.12598, 1.23283, -0.37481],
[-0.22095, 0.24469, 1.19043, -1.21416],
[-0.8278 , 1.44574, -0.43868, -0.17926]])
An example with ``nan_policy='omit'``:
x = np.array([[25.11, 30.10, np.nan, 32.02, 43.15], [14.95, 16.06, 121.25, 94.35, 29.81]])✓
stats.zscore(x, axis=1, nan_policy='omit')
✗array([[-1.13491, -0.3783 , nan, -0.08718, 1.6004 ],
[-0.91612, -0.89091, 1.4983 , 0.88732, -0.5786 ]])
See also
- numpy.mean
Arithmetic average
- numpy.std
Arithmetic standard deviation
- scipy.stats.gzscore
Geometric standard score
Aliases
scipy.stats.zscore