# NumPy Variance Function in Python

• Post author:
• Post category:NumPy / Python

NumPy array `var()` function in Python is used to compute the arithmetic variance of the array elements along with the specified axis or multiple axes. We get the Variance by calculating the sum of all values in a Numpy array divided by the total number of values.

By default, the variance is taken from the flattened array (from all array elements), This function calculates the average of the squared deviations from the mean, i.e., `var=mean(abs(x–x.mean())**2)e`. Mean is `x.sum()/N`, where `N=len(x)` for an array x, otherwise along with the specified axis. In this article, I will explain `numpy.var()` function syntax, usage, and how to calculate the variance for the given single-dimensional or multi-dimensional array.

## 1. Quick Examples of Variance Function

If you are in a hurry, below are some quick examples of NumPy variance function in Python.

``````
# Below are a quick examples

# Example 1: Use numpy.var() function
arr2 = np.var(arr)

# Example 2: Get the numpy var() of 1-D array
arr = np.array([2, 7, 5, 8, 9, 4])
arr1 = np.var(arr)

# Example 3: Get the var() value of an array
# with specified datatype
arr2 = np.var(arr, dtype = np.float32)

# Example 4: Get the var() of arr float64 data
arr2 = np.var(arr, dtype = np.float64)

# Example 5: Get the var() with 2-D array
arr = np.array([[3, 5, 7, 9], [2, 4, 6, 8]])
arr2 = np.var(arr)

# Example 6: Get the var() values over row
# for each of 4 columns
arr2 = np.var(arr, axis = 0)

# Example 7: Get the var() values over column
# for each of 2 rows
arr2 = np.var(arr, axis = 1)
``````

## 2. Syntax of NumPy var() Function

Following is the syntax of the `numpy.var()` function.

``````
# Syntax of numpy.var()
numpy.var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=, *, where=)
``````

## 2.1 Parameters of var()

Following are the parameters of the `var()` function.

• `arr` – array_like: An array containing elements whose variance is desired. If arr is not an array, a conversion is attempted.
• `axis `– [None or int or tuple of ints, optional]: Axis or axes along which the variance is computed. The default is to compute the variance of the flattened array. axis = 0 means variance along the column and axis = 1 means variance along the row.
• `dtype` – It is an optional parameter that specifies the data type we desire while computing the variance. Default is float64 for arrays of integer type. For arrays of float types, it is the same as the array type.
• `out` – It is an optional parameter, An alternate output array must have the same dimensions as the expected output. But the type is cast if necessary.
• `keepdims` – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr. For default value, keepdims will not be passed through to the var() method of sub-classes of ndarray.
• `where` – Elements to include in the variance, the value should be array_like of bool, optional.

## 2.2 Return Value of NumPy var()

It returns the arithmetic variance of the array (a scalar value if the axis is none) or an array with variance values along the specified axis.

## 3. Use numpy.var() Function

We can use Numpy `var()` function is used to calculate the variance of an array. This functions return the variance accurately by passing the` arr` as a parameter.

``````
import numpy as np

# create array
arr = np.arange(15)

# Use numpy.var() function
arr2 = np.var(arr)
print(arr2)

# Output
# 18.666666666666668
``````

## 4. Get NumPy var() of 1-D Array

Take a one-dimensional NumPy array and compute the variance of the array using `numpy.var()` function, for that let’s create an array using numpy.array(). For example,

``````
import numpy as np
# Create 1D array
arr = np.array([2, 7, 5, 8, 9, 4])

# Get the numpy var() of 1-D array
arr1 = np.var(arr)
print(arr1)

# Output
# 5.8055555555555545
``````

## 5. Use Datatype Param

Let’s use the `dtype` parameter to specify the result variance data type. The result has a lower resolution if you use `float32` data type rather than the default `float64`.

``````
# Get the var() value of an array
# with specified datatype
arr2 = np.var(arr, dtype = np.float32)
print(arr2)

# Output
# 5.805556

# Get the var() of arr float64 data
arr2 = np.var(arr, dtype = np.float64)
print(arr2)

# Output
# 5.8055555555555545
``````

## 6. Get NumPy var() With 2-D Array

This function is also used to compute the `var()` of the 2-D NumPy array, By default it considers all elements to calculate the variance. You can change this behavior by using the axis param.

``````
# Create 2-D array
arr = np.array([[3, 5, 7, 9], [2, 4, 6, 8]])

# Get the var() with 2-D array
arr2 = np.var(arr)
print(arr2)

# Output
# 5.25
``````

## 7. Get the Variance With 2-D NumPy Array along Axis

We can also compute the variance of a NumPy array along with a specified axis. If we want to compute the variance of each row, we will pass the` axis=0` parameter through the `var()` function. Similarly, to compute the variance of each column, use `axis=1`.

``````
# Get the var() values over row
# for each of 4 columns
arr2 = np.var(arr, axis = 0)
print(arr2)

# Output
# [0.25 0.25 0.25 0.25]

# Get the var() values over column
# for each of 2 rows
arr2 = np.var(arr, axis = 1)
print(arr2)

# Output
# [5. 5.]
``````

## 8. Conclusion

In this article, I have explain the how to calculate the arithmetic variance of NumPy array along with the specified axis and multiple axes. Also explained how to use dtype optional param to change the return data type.

Happy Learning!!

### References  