How to calculate the element-wise mean across multiple numpy arrays in Python ?


Introduction

An element-wise mean refers to calculating the average value for each individual element in an array. It differs from a traditional mean or average, which calculates the overall average value of an entire array.

In this tutorial, we will learn how to get the element-wise mean of a numpy array in python.

Using Numpy mean()

Example with 1D arrays

Let's consider the following three arrays:

import numpy as np

A1 = np.array([10, 40, 10, 1])
A2 = np.array([50, 10, 15, 1])
A3 = np.array([30, 10, 40, 1])

Please be aware that all arrays possess identical dimensions here.

To calculate the element-wise mean of a numpy array in python, we can use the mean() function from the NumPy library:

np.mean([A1, A2, A3], axis=0)

Note that the axis parameter allows us to specify which axis we want to calculate the mean along. By default, it is set to None, which means the function will calculate the mean of the flattened array (the entire array).

If we have a multidimensional array, we can specify the axis as 0 for calculating the mean along the columns, and 1 for calculating along the rows.

Output:

array([30.        , 20.        , 21.66666667,  1.        ])

As we can see, the output is an array with each element having its own value.

Example with 2D arrays

import numpy as np

A1 = np.array( [ [10, 40],[ 10, 1] ] )
A2 = np.array( [ [50, 10],[ 15, 1] ] )
A3 = np.array( [ [30, 10],[ 40, 1] ] )

Output A1:

array([[10, 40],
       [10,  1]])

Output A2:

array([[50, 10],
       [15,  1]])

Output A3:

array([[30, 10],
       [40,  1]])

Then

np.mean([A1, A2, A3], axis=0)

gives

array([[30.        , 20.        ],
       [21.66666667,  1.        ]])

Using broadcasting

A more efficient approach for calculating the mean across multiple arrays is to utilize NumPy broadcasting.

NumPy broadcasting allows for operations to be applied to arrays with different dimensions. This makes it easier and faster to perform element-wise operations on large arrays.

For example:

import numpy as np

A1 = np.array([10, 40, 10, 1])
A2 = np.array([50, 10, 15, 1])
A3 = np.array([30, 10, 40, 1])

with broadcasting

( A1 + A2 + A3 ) / 3

also returns:

array([30.        , 20.        , 21.66666667,  1.        ])

Please note that we divided by 3 in this case, as there are 3 arrays involved.

Additional features

Rounding

There are a few different rounding methods available in numpy, each serving a specific purpose. The most commonly used ones are np.around(), np.floor(), np.ceil() and np.trunc(). For example:

R = np.mean([A1, A2, A3], axis=0)

np.around(R,2)

Output:

array([[30.  , 20.  ],
       [21.67,  1.  ]])

Other statistical tools

import numpy as np

A1 = np.array([10, 40, 10, 1])
A2 = np.array([50, 10, 15, 1])
A3 = np.array([30, 10, 40, 1])

Getting maximum value element-wise

np.max([a, b, c], axis=0)

Output

array([50, 20, 40])

Getting minimum value element-wise

np.min([a, b, c], axis=0)

Output

array([10, 20, 20])

Getting standard deviation value element-wise

np.std([a, b, c], axis=0)

Output

array([16.32993162,  0.        ,  8.16496581])

References