NumPy is a popular Python library for data science focusing on arrays, vectors, and matrices. This article introduces the
np.average() function from the NumPy library.
When applied to a 1D array, this function returns the average of the array values. When applied to a 2D array, NumPy simply flattens the array. The result is the average of the flattened 1D array. Only if you use the optional
axis argument, you can average along the rows or columns of the 2D array.
Here’s a visual overview first—we’ll discuss details later:
Let’s start with the simple, flat case first.
Average of Flattened 2D Array
To calculate the average of all values in a two-dimensional NumPy array called
matrix, use the
>>> import numpy as np >>> matrix = np.array([[1, 0, 2], [1, 1, 1]]) >>> np.average(matrix) 1.0
This calculates the average of the flattened out matrix, i.e., it’s the same as calling
np.average([1, 0, 2, 1, 1, 1]) without the two-dimensional structuring of the data.
Column Average of 2D Array
To calculate the average separately for each column of the 2D array, use the function call
np.average(matrix, axis=0) setting the axis argument to 0.
>>> np.average(matrix, axis=0) array([1. , 0.5, 1.5])
The resulting array has three average values, one per column of the input
Row Average of 2D Array
To calculate the average separately for each row of the 2D array, call
np.average(matrix, axis=1) setting the axis argument to 1.
>>> np.average(matrix, axis=1) array([1., 1.])
The resulting array has two average values, one per row of the input
NumPy Puzzle Average
To test your skills and train your understanding of the np.average() function, here’s a code puzzle you may enjoy:
import numpy as np # stock prices (3x per day) # [morning, midday, evening] solar_x = np.array( [[2, 3, 4], # day 1 [2, 2, 5]]) # day 2 print(np.average(solar_x))
What is the output of this puzzle?
*Beginner Level* (solution below)
You can solve this code puzzle interactively on our Finxter.com puzzle app here:
The puzzle has a matrix with two rows and three columns. The matrix gives the stock prices of the
solar_x stock. Each row represents the prices for one day. The first column specifies the morning price, the second the midday price, and the third the evening price.
Are you a master coder?
Test your skills now!
Note that the following tutorial may be interesting to you too:
👉 Recommended Tutorial: How to Get the Length of a 2D NumPy Array
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.