Overall dispersion
Summarize variability in a complete numeric array.
NumPy · Function
Computes the standard deviation of array elements, measuring distribution spread. It reduces the flattened input by default or operates along specified axes.
np.std computes the square root of the mean squared deviation from the mean. It reduces all elements by default, or reduces selected axes, and can preserve reduced dimensions with keepdims; where selects the elements included in the calculation.
numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>, mean=<no value>, correction=<no value>)| Name | Type | Description |
|---|---|---|
a | array_like | Input array or array-like values whose standard deviation is calculated. |
axis | None, int, or tuple of ints, optional | Axis or tuple of axes to reduce; None reduces the flattened input. |
dtype | dtype, optional | Data type used during calculation. |
out | ndarray, optional | Output array receiving the result; if supplied, the returned value references it. |
ddof | int, optional | Delta degrees of freedom subtracted from the number of included elements in the divisor. |
keepdims | bool, optional | If true, reduced axes remain as dimensions of length one. |
where | array_like of bool, optional | Boolean mask or broadcastable condition selecting values included in the calculation. |
mean | array_like, optional | Precomputed mean used by the standard-deviation calculation. |
correction | float, optional | Alternative name for ddof; cannot be used together with ddof. |
If out is None, a new ndarray containing the standard deviation; otherwise, a reference to the supplied output array.
import numpy as np
values = np.array([[1, 2, 3], [4, 5, 6]])
print(np.std(values))
print(np.std(values, axis=0))
print(np.std(values, ddof=1))Computes the overall, column-wise, and sample-style standard deviations.
Summarize variability in a complete numeric array.
Compare variability independently across rows, columns, or other dimensions.
Keep dimensions compatible with later broadcasting operations.
Calculate dispersion over a selected subset using a mask.
It returns the standard deviation of array elements, reducing the flattened array when axis=None. The calculation is the square root of the average squared deviation from the mean, and ddof controls the divisor.
Follow-up: How would you compute standard deviation across rows while retaining a two-dimensional result? Use axis=1 and keepdims=True.
axis selects one axis, multiple axes as a tuple, or all axes when None. The output shape is reduced along the selected axes unless keepdims=True.
Follow-up: How can selected observations be excluded without copying the array? Pass a boolean condition through where.
Integer inputs use float64 by default, while floating inputs retain their dtype unless dtype is supplied. The out argument can receive the result and is returned by reference.
Follow-up: Why might dtype=float64 be useful for float32 input? It can improve accumulation accuracy at the cost of additional computation and storage.
numpy.varnumpy.meannumpy.nanmeannumpy.nanstdnumpy.nanvar