NumPy var()

The numpy.var() method computes the variance along the specified axis.

Example


var() Syntax

The syntax of the numpy.var() method is:

numpy.var(array, axis = None, dtype = None, out = None, ddof = 0, keepdims = <no value>, where = <no value>)

var() Arguments

The numpy.var() method takes the following arguments:

  • array - array containing numbers whose variance is desired (can be array_like)
  • axis (optional) - axis or axes along which the variances are computed (int or tuple of int)
  • dtype (optional) - the data type to use in the calculation of variance (datatype)
  • out (optional) - output array in which to place the result (ndarray)
  • ddof (optional) - delta degrees of freedom (int)
  • keepdims (optional) - specifies whether to preserve the shape of the original array (bool)
  • where (optional) - elements to include in the variance (array of bool)

Notes: The default values of numpy.var() impy the following:

  • axis = None - the variance of the entire array is taken.
    • dtype = None - in the case of integers, float is taken; otherwise variance is of the same data type as the elements
    • By default, keepdims and where will not be passed.

var() Return Value

The numpy.var() method returns the variance of the array.


Example 1: Find the variance of an ndArray

Output

variance of the entire array: 5.25

variance across axis 0:
 [[4. 4.]
 [4. 4.]]

variance across axis 0 and 1: [5. 5.]

Example 2: Specifying the Data Type of Variance of an ndArray

The dtype parameter can be used to control the data type of the output array.

Output

Float variance: 2.9166666666666665
Integer variance: 3

Note: Using a lower precision dtype, such as int, can lead to a loss of accuracy.


Example 3: Using Optional keepdims Argument

If keepdims is set to True, the resultant variance array is of the same number of dimensions as the original array.

Output

Dimensions in original array: 2
Without keepdims: [2.25 2.25 2.25] with dimensions 1
With keepdims: [[2.25 2.25 2.25]] with dimensions 2

Example 4: Using Optional where Argument

The optional argument where specifies which elements to include in the variance.

Output

variance of entire array: 2.9166666666666665
variance of only even elements: 2.6666666666666665
variance of  numbers greater than 3: 0.6666666666666666

Example 5: Using Optional out Argument

The out parameter allows us to specify an output array where the result will be stored.

Output

variance: [2.25 2.25 2.25]

Frequently Asked Questions

What is variance?

Variance is the average of the squared deviation from the mean. It is the measure of the spread of values around the mean in the given array.

Mathematically,

var = sum((array1 - arr.mean())** 2) / (N - 1)

In NumPy,

Output

variance with np.var(): 8.0
variance without np.var(): 8.0
What is the ddof parameter in numpy.var() used for?

The ddof (Delta Degrees of Freedom) parameter in numpy.var() allows adjusting the divisor used in the calculation of variance. The default value is 0, which corresponds to dividing by N, the number of elements.

In the above formula of var,

var = sum((array1 - arr.mean())** 2) / (N - ddof)

Let's look at an example.

Output

variance (default ddof = 0): 2.0
variance (ddof = 1): 2.5