Variance of NumPy Array in Python (3 Examples)
This article shows how to apply the np.var function in the Python programming language.
The tutorial contains these contents:
If you want to learn more about these content blocks, keep reading.
Example Data & Libraries
First, we have to load the NumPy library:
import numpy as np # Import NumPy library
We’ll use the following data as a basis for this Python tutorial:
my_array = np.array([[1, 2, 7, 2, 3], # Create example array [7, 1, 1, 5, 6], [5, 2, 5, 5, 8]]) print(my_array) # Print example array # [[1 2 7 2 3] # [7 1 1 5 6] # [5 2 5 5 8]]
The previous output of the Python console shows the structure of our example data – We have created a NumPy array containing 15 values in five columns and three rows.
Example 1: Variance of All Values in NumPy Array
Example 1 explains how to compute the variance of all values in a NumPy array.
In order to achieve this, we can use the var function of the NumPy library as shown in the following Python code:
print(np.var(my_array)) # Get variance of all array values # 5.466666666666667
The previous output shows our result, i.e. the variance of our NumPy array is 5.47.
Note that this result reflects the population variance. In case you want to calculate the sample variance, you would have to set the ddof argument to be equal to 1.
Example 2: Variance of Columns in NumPy Array
We can also use the var function to calculate the variance of each column in a NumPy array.
The Python code below illustrates how to do this using the var function and the axis argument:
print(np.var(my_array, axis = 0)) # Get variance of array columns # [6.22222222 0.22222222 6.22222222 2. 4.22222222]
Example 3: Variance of Rows in NumPy Array
In this section, I’ll demonstrate how to get the variance for each row of our NumPy array.
This time, we have to set the axis argument to be equal to 1 (instead of 0 as in the previous example):
print(np.var(my_array, axis = 1)) # Get variance of array rows # [4.4 6.4 3.6]
Video, Further Resources & Summary
Do you need more info on the Python code of this article? Then you may want to watch the following video on my YouTube channel. In the video, I illustrate the Python syntax of this tutorial.
Furthermore, you could have a look at the other articles on this homepage. You can find a selection of articles below:
- Variance in Python
- Variance by Group in Python
- Standard Deviation in Python
- Convert pandas DataFrame Index to List & NumPy Array in Python
- Convert pandas DataFrame to NumPy Array in Python
- Get Median of Array with np.median Function of NumPy Library in Python
- Introduction to Python Programming
To summarize: You have learned in this tutorial how to use the np.var function to get the variance of an array in Python. If you have further questions, let me know in the comments section below. Furthermore, don’t forget to subscribe to my email newsletter in order to receive regular updates on the newest articles.