Calculate the average (mean) of 2 NumPy arrays

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
5 min

banner

# Table of Contents

  1. Calculate the average (mean) of 2 NumPy arrays
  2. Calculate the average (mean) of multiple 2-dimensional NumPy arrays
  3. Calculate the average of 2 two-dimensional NumPy arrays using division
  4. Calculate the weighted average of 2 NumPy arrays
  5. Calculate the weighted average of 2 NumPy arrays using numpy.average()

# Calculate the average (mean) of 2 NumPy arrays

To calculate the average (mean) of 2 NumPy arrays:

  1. Use the addition operator to sum the 2 arrays element-wise.
  2. Divide the resulting array by 2 to get the average.
main.py
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([5, 6, 7, 8]) # ๐Ÿ‘‡๏ธ [ 6 8 10 12] print(arr1 + arr2) arr3 = (arr1 + arr2) / 2 # ๐Ÿ‘‡๏ธ [3. 4. 5. 6.] print(arr3)

calculate average of 2 numpy arrays

The code for this article is available on GitHub

We used the addition (+) operator to sum the two arrays element-wise and then divided by 2.

The average (or mean) of 2 NumPy arrays is calculated by:

  1. Adding the two arrays element-wise.
  2. Dividing the numbers in the resulting array by the number of arrays (2 in the example).

The same approach can be used to calculate the average of 3 or more NumPy arrays.

main.py
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([5, 6, 7, 8]) arr3 = np.array([3, 4, 5, 6]) # ๐Ÿ‘‡๏ธ [ 9 12 15 18] print(arr1 + arr2 + arr3) arr3 = (arr1 + arr2 + arr3) / 3 # ๐Ÿ‘‡๏ธ [3. 4. 5. 6.] print(arr3)

calculate average of 3 numpy arrays

The code for this article is available on GitHub

We used the addition (+) operator to sum the 3 arrays element-wise and then divided the resulting array by 3 to get the average.

# Calculate the average (mean) of multiple 2-dimensional NumPy arrays

If you need to calculate the average (mean) of multiple 2-dimensional NumPy arrays:

  1. Create a 3-dimensional array from your 2-dimensional arrays.
  2. Call the numpy.mean() method with the resulting array.
  3. Set the axis argument to 0.
main.py
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[3, 4], [5, 6]]) arr3 = np.mean(np.array([arr1, arr2]), axis=0) # [[2. 3.] # [4. 5.]] print(arr3)

calculate average of 2 numpy 2 dimensional arrays

The code for this article is available on GitHub

We used the numpy.array() method to create a 3-dimensional array from the two 2-dimensional arrays.

main.py
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[3, 4], [5, 6]]) # [[[1 2] # [3 4]] # [[3 4] # [5 6]]] print(np.array([arr1, arr2]))

The last step is to get the average (mean) of the arrays by using numpy.mean.

main.py
arr3 = np.mean(np.array([arr1, arr2]), axis=0) # [[2. 3.] # [4. 5.]] print(arr3)

The numpy.mean() method computes the arithmetic mean along the specified axis.

The same approach can be used to calculate the average (mean) of more than two 2-dimensional NumPy arrays.

main.py
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[3, 4], [5, 6]]) arr3 = np.array([[5, 6], [7, 8]]) arr4 = np.mean(np.array([arr1, arr2, arr3]), axis=0) # [[3. 4.] # [5. 6.]] print(arr4)

calculate average of more than 2 2 d arrays

# Calculate the average of 2 two-dimensional NumPy arrays using division

You can also use division to calculate the average of 2 two-dimensional NumPy arrays.

main.py
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[3, 4], [5, 6]]) arr3 = (arr1 + arr2) / 2 # [[2. 3.] # [4. 5.]] print(arr3)
The code for this article is available on GitHub

We used the addition (+) operator to sum the two 2-dimensional arrays.

main.py
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[3, 4], [5, 6]]) # [[ 4 6] # [ 8 10]] print(arr1 + arr2)

The last step is to divide the resulting array by the number of arrays to get the mean.

main.py
arr3 = (arr1 + arr2) / 2 # [[2. 3.] # [4. 5.]] print(arr3)

The same approach can be used to calculate the average of more than 2 two-dimensional NumPy arrays.

main.py
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[3, 4], [5, 6]]) arr3 = np.array([[5, 6], [7, 8]]) arr3 = (arr1 + arr2 + arr3) / 3 # [[3. 4.] # [5. 6.]] print(arr3)

We used the addition (+) operator to sum the 3 two-dimensional NumPy arrays element-wise.

main.py
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[3, 4], [5, 6]]) arr3 = np.array([[5, 6], [7, 8]]) # [[ 9 12] # [15 18]] print(arr1 + arr2 + arr3)

The last step is to divide the resulting array by 3 to get the average values.

main.py
arr3 = (arr1 + arr2 + arr3) / 3 # [[3. 4.] # [5. 6.]] print(arr3)

# Calculate the weighted average of 2 NumPy arrays

If you need to calculate the weighted average of 2 NumPy arrays:

  1. Multiply each array by the weight.
  2. Use the addition (+) operator to sum the resulting arrays element-wise.
main.py
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([5, 6, 7, 8]) weight1 = 0.5 weight2 = 0.5 arr3 = arr1 * weight1 + arr2 * weight2 # ๐Ÿ‘‡๏ธ [3. 4. 5. 6.] print(arr3)
The code for this article is available on GitHub

We used the multiplication operator to multiply each array by the weight.

The last step is to use the addition (+) operator to get the average of the 2 arrays.

# Calculate the weighted average of 2 NumPy arrays using numpy.average()

You can also use the numpy.average() method to calculate the weighted average of 2 NumPy arrays.

main.py
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([5, 6, 7, 8]) weight1 = 0.5 weight2 = 0.5 arr3 = np.average( [arr1, arr2], axis=0, weights=[weight1, weight2] ) # ๐Ÿ‘‡๏ธ [3. 4. 5. 6.] print(arr3)
The code for this article is available on GitHub

The numpy.average() method computes the weighted average along the specified axis.

The method takes a weights argument - an array of weights associated with the values in the supplied arrays.

The weights array can either be 1-dimensional (in which case its length must be the size of along the given axis) or the same shape as the supplied arrays.

# Table of Contents

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev