Skip to Content

Numpy – Get Variance of Array Values

The Numpy library in Python comes with a number of useful built-in functions for computing common descriptive statistics like mean, median, standard deviation, variance, etc. In this tutorial, we will look at how to get the variance of a Numpy array with the help of some examples.

How do you get the variance of an array in Numpy?

You can use the Numpy var() function to get the variance of the values in a Numpy array. Pass the array as an argument.

The following is the syntax –

# variance of all values in array
numpy.var(ar)

It returns the variance taking into account all the values in the array. For multi-dimensional arrays, you can specify the axis along which you want to compute the variance (see the examples below).

Examples

Let’s now look at some examples of using the above syntax on single and multi-dimensional arrays.

Example 1 – Variance of a one-dimensional Numpy array

get the variance of array values in numpy

Let’s first create a one-dimensional Numpy array.

import numpy as np

# create numpy array
ar = np.array([1, 2, 3, 4, 5])
# display the array
print(ar)

Output:

[1 2 3 4 5]

Here, we used the numpy.array() function to create a one-dimensional array containing some numeric values.

Let’s now get the variance of all the values in the above array.

# variance of array
print(np.var(ar))

Output:

2.0

We get the variance as 2.

Example 2 – Variance of multi-dimensional Numpy array

First, let’s create a 2-D Numpy array.

# create 2-D numpy array
ar = np.array([[1, 2, 3],
               [2, 1, 1]])
# display the array
print(ar)

Output:

[[1 2 3]
 [2 1 1]]

Here, we used the numpy.array() function to create a Numpy array with two rows and three columns.

If you use the Numpy var() function on an array without specifying the axis, it will return the variance taking into account all the values inside the array.

# variance of array
print(np.var(ar))

Output:

0.5555555555555556

We get the variance of all the values inside the 2-D array.

Use the numpy.var() function with axis=1 to get the variance for each row in the array.

# variance of each row in array
print(np.var(ar, axis=1))

Output:

[0.66666667 0.22222222]

We get the variance of each row in the above 2-D array. The variance of the values in the first row (1, 2, 3) is 0.66 and the variance of the values in the second row (2, 1, 1) is 0.22.

Use the numpy.var() function with axis=0 to get the variance of each column in the array.

# variance of each column in array
print(np.var(ar, axis=0))

Output:

[0.25 0.25 1.  ]

We get the variance of each column in the above 2-D array. The variance of the values – in the first column (1, 2) is 0.25, in the second column (2, 1) is 0.25, and in the third column (3, 1) is 1.

Summary

In this tutorial, we looked at how to use the numpy.var() function to get the variance of values in an array. The following are the key takeaways from this tutorial.

  • Use the numpy.var() function without any arguments to get the variance of all the values inside the array.
  • For multi-dimensional arrays, use the axis parameter to specify the axis along which to compute the variance. For example, for a 2-D array –
    • Pass axis=1 to get the variance of each row.
    • Pass axis=0 to get the variance of each column.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.