Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.var

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.

How it works

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.

Syntax

numpy.var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>, mean=<no value>, correction=<no value>)

Parameters

NameTypeDescription
aarray_likeArray-like input containing the values whose variance is requested.
axisNone, int, or tuple of intsAxis or tuple of axes over which to calculate variance; None uses the flattened array.
dtypedata-type, optionalData type used during the calculation.
outndarray, optionalExisting array receiving the result.
ddofint or float, optionalDegrees-of-freedom adjustment; the divisor is N - ddof.
keepdimsbool, optionalWhether reduced axes remain as dimensions of length one.
wherearray_like of bool, optionalBoolean selection of values included in the calculation.
meanarray_like, optionalPrecomputed mean used by the variance calculation.
correctionint or float, optionalArray API-compatible name for the degrees-of-freedom adjustment.

Returns

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.

Key points

  • Use np.var(a) to measure spread through squared deviations from the mean.
  • Omitting axis reduces every element; supplying an axis performs the reduction along that dimension.
  • A tuple of axis integers lets one call reduce across several dimensions.
  • Use out when the result should be written into an existing compatible array.
  • The default reduction produces a scalar for a scalar-like input or a scalar result from an ordinary multidimensional array.
  • Set dtype to control the accumulation and calculation type.

Common mistakes

  • Integer inputs use float64 by default, while floating-point inputs retain their floating-point dtype; specify dtype when you need explicit control.
  • Changing ddof changes the divisor, so use the default for population variance and a positive adjustment when estimating sample variance.

Example: Overall and axis-specific variance

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.

When to use it

Measure spread

Summarizing the dispersion of a numeric vector or multidimensional dataset.

Axis-wise reduction

Calculating independent variances across rows, columns, or multiple dimensions.

Sample statistics

Estimating sample variance by adjusting the divisor with ddof=1.

Masked selection

Computing variance for only elements selected by a boolean condition.

Interview questions

QWhat does np.var compute, and what is the default axis behavior?

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.

QHow does ddof affect the result?

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.

QHow can you restrict values or reuse a mean?

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.std
  • numpy.mean
  • numpy.nanvar
  • numpy.nanmean
  • numpy.ndarray.var

Source: numpy.var. Checked 2026-10-15.