How to Calculate Variance of Python NumPy Arrays?

Rate this post

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)
# False

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


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)

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!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

๐Ÿš€ If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar โ€œHow to Build Your High-Income Skill Pythonโ€ and learn 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