Measure spread
Summarizing the dispersion of a numeric vector or multidimensional dataset.
NumPy · Function
Computes variance, the spread measured by the average squared deviation from the mean. It reduces the flattened input by default or operates along specified axes.
np.var computes the average squared deviation from the mean, either across all elements or independently along selected axes. Parameters such as dtype, ddof, keepdims, where, and a precomputed mean control numerical type, divisor, output shape, selection, and reuse of the mean.
numpy.var(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 | Array-like input containing the values whose variance is requested. |
axis | None, int, or tuple of ints | Axis or tuple of axes over which to calculate variance; None uses the flattened array. |
dtype | data-type, optional | Data type used during the calculation. |
out | ndarray, optional | Existing array receiving the result. |
ddof | int or float, optional | Degrees-of-freedom adjustment; the divisor is N - ddof. |
keepdims | bool, optional | Whether reduced axes remain as dimensions of length one. |
where | array_like of bool, optional | Boolean selection of values included in the calculation. |
mean | array_like, optional | Precomputed mean used by the variance calculation. |
correction | int or float, optional | Array API-compatible name for the degrees-of-freedom adjustment. |
A scalar or ndarray containing the variance, with shape determined by the selected axes and keepdims; if out is supplied, the returned object references that output array.
np.var(a) to measure spread through squared deviations from the mean.axis reduces every element; supplying an axis performs the reduction along that dimension.out when the result should be written into an existing compatible array.dtype to control the accumulation and calculation type.import numpy as np
values = np.array([[1, 2, 3], [4, 5, 6]])
print(np.var(values))
print(np.var(values, axis=0, ddof=1))Computes overall population variance, then sample variance independently for each column.
Summarizing the dispersion of a numeric vector or multidimensional dataset.
Calculating independent variances across rows, columns, or multiple dimensions.
Estimating sample variance by adjusting the divisor with ddof=1.
Computing variance for only elements selected by a boolean condition.
np.var returns the average squared deviation from the mean. With the default axis=None, it computes over the flattened input; an integer or tuple of axes restricts the reduction.
Follow-up: How would you preserve reduced dimensions? Pass keepdims=True, which leaves the reduced axes with size one.
ddof means delta degrees of freedom. The calculation divides by N - ddof, where N is the number of included elements, so ddof=0 gives the population form and ddof=1 gives the common sample form.
Follow-up: What happens when the effective divisor is zero? NumPy emits a warning and returns NaN for that slice.
where selects the elements included in the calculation, and mean can supply a previously computed mean to avoid recomputing it. The supplied mean must have the shape produced by keepdims=True for the same reduction.
Follow-up: Which parameter can replace ddof? correction is an Array API-compatible alternative name, but specifying both is an error.
numpy.stdnumpy.meannumpy.nanvarnumpy.nanmeannumpy.ndarray.var