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:
In the puzzle, we have 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!
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.)