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.

Table of Contents

## Average of Flattened 2D Array

To calculate the average of all values in a two-dimensional NumPy array called `matrix`

, use the `np.average(matrix)`

function.

>>> 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 `matrix`

.

## 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 `matrix`

.

## 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.

Note that NumPy calculates the average as the sum over all values, divided by the number of values. The result is a float value.

Are you a master coder?

Test your skills now!

## Related Video

**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.)*

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. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, 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.