How to Calculate Variance of Python NumPy Arrays?

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. Did you already learn something new today? Let’s master the popular variance function in NumPy!

Problem: How to calculate the variance of a NumPy array?

Solution: To calculate the variance of a Python NumPy array x, use the function np.var(x).

Here is an example:

import numpy as np

# Goals in five matches
goals_croatia = np.array([0,2,2,0,2])
goals_france = np.array([1,0,1,1,0])

c = np.var(goals_croatia)
f = np.var(goals_france)
print(c<f)
# False

What is the output of this puzzle?
*Intermediate Level* (solution here)


Explanation:

This puzzle introduces a new feature of the NumPy library: the variance function. The variance is the average squared deviation from the mean of the values in the array.

When applied to a 1D numpy array, this function returns the variance of the array values.

In the puzzle, the variance of the goals of the last five games of Croatia is 0.96 and of France is 0.24. But you do not need to know the exact values to see that the variance of goals shot by Croatia is larger.

Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

Coffee Break NumPy

Syntax NumPy Variance

Syntax: numpy.var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)

ArgumentType/PropertyDescription
aarray_likeArray of values for which to compute variance. NumPy converts it to an array if it isn’t already one (broadcasting).
axisint, optionalAxis along which you calculate the variance. Default: variance of flattened array.
dtypedata-type, optionalData type of variance values. Default: float32 for integers.
outndarray, optionalStore the result in this array (overwrite) –> array shape must be the same.
ddofint, optionalddof == Delta Degrees of Freedom. Given n elements, use divisor n - ddof. Default: ddof=0.
keepdimsbool, optionalIf True, reduced axes are left with size one.
Return ValueType/PropertyDescription
variancendarray, see dtype parameter aboveIf out=None, returns a new array containing the variance; otherwise, a reference to the output array is returned.

How to Calculate the Row Variance of a Numpy 2D Array?

You can play with the following interactive Python code to calculate the variance of a 2D array (total, row, and column variance).

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Leave a Comment

Your email address will not be published. Required fields are marked *