Skip to main content

Python NumPy: How to Calculate the Average (Mean) of Multiple Arrays

NumPy provides efficient ways to perform element-wise operations on arrays, including calculating averages or means. Whether you have multiple 1D arrays that you want to average element-wise, or several 2D arrays representing, for example, different observations of the same grid, NumPy offers straightforward techniques.

This guide will comprehensively demonstrate how to calculate the element-wise average (mean) of two or more NumPy arrays, covering both 1D and 2D arrays. We'll explore direct arithmetic operations and the versatile numpy.mean() and numpy.average() functions, including how to compute weighted averages.

Understanding Element-Wise Array Averaging

When we talk about averaging multiple NumPy arrays, we typically mean performing an element-wise average. This means that for each corresponding position in the input arrays, we take the values at that position from all arrays, sum them up, and then divide by the number of arrays. For this to work directly with arithmetic operators, the arrays must have compatible shapes (usually, the same shape).

Calculating the Average of 1D NumPy Arrays

Averaging Two 1D Arrays (Element-wise Sum and Division)

The simplest way to average two 1D NumPy arrays of the same size is to sum them element-wise and then divide the resulting array by 2.

import numpy as np

array_A = np.array([10, 20, 30, 40])
array_B = np.array([2, 4, 6, 8])

# Step 1: Element-wise sum
sum_of_arrays = array_A + array_B
print(f"Sum of array_A and array_B: {sum_of_arrays}")

# Step 2: Element-wise division by the number of arrays (2)
average_array = sum_of_arrays / 2
# Or directly: average_array = (array_A + array_B) / 2

print(f"Average of array_A and array_B: {average_array}")

Output:

Sum of array_A and array_B: [12 24 36 48]
Average of array_A and array_B: [ 6. 12. 18. 24.]
note

NumPy's broadcasting rules handle the scalar division / 2 correctly.

Averaging Three or More 1D Arrays

The same principle extends to more than two arrays.

import numpy as np

scores_subject1 = np.array([70, 85, 90])
scores_subject2 = np.array([75, 80, 92])
scores_subject3 = np.array([80, 75, 88])

# Sum all arrays element-wise
total_scores = scores_subject1 + scores_subject2 + scores_subject3
print(f"Total scores: {total_scores}")

# Divide by the number of arrays (3)
average_scores = total_scores / 3

print(f"Average scores across 3 subjects: {average_scores}")

Output:

Total scores: [225 240 270]
Average scores across 3 subjects: [75. 80. 90.]

Calculating the Average of 2D NumPy Arrays (Element-Wise)

For 2D arrays (matrices), if you want to find the average array where each element is the mean of the corresponding elements from all input 2D arrays, you have a couple of good options. All input 2D arrays should have the same shape.

Method 1: Using numpy.mean(axis=0) on a Stacked Array

This is often the most idiomatic and flexible NumPy approach, especially for multiple arrays.

  1. Stack your 2D arrays into a single 3D array.
  2. Use numpy.mean() along the new axis (axis 0) that represents the different arrays.
import numpy as np

m1 = np.array([[1, 2], [3, 4]])
m2 = np.array([[3, 4], [5, 6]])
m3 = np.array([[5, 6], [7, 8]])

# Step 1: Create a 3D array by stacking the 2D arrays
# The shape will be (num_arrays, num_rows, num_cols) -> (3, 2, 2)
stacked_m = np.array([m1, m2, m3])
print("Stacked matrices (3D array):")
print(stacked_m)

# Step 2: Calculate the mean along axis=0 (the axis that stacks the different arrays)
avg_m_np_mean = np.mean(stacked_m, axis=0)
print("Average of 2D arrays using numpy.mean(axis=0):")
print(avg_m_np_mean)

Output:

Stacked matrices (3D array):
[[[1 2]
[3 4]]

[[3 4]
[5 6]]

[[5 6]
[7 8]]]
Average of 2D arrays using numpy.mean(axis=0):
[[3. 4.]
[5. 6.]]

Method 2: Element-wise Sum and Division for 2D Arrays

This is analogous to the 1D case and works if all 2D arrays have the same shape.

import numpy as np

m1 = np.array([[1, 2], [3, 4]])
m2 = np.array([[3, 4], [5, 6]])
m3 = np.array([[5, 6], [7, 8]])

# Step 1: Element-wise sum
sum_of_matrices = m1 + m2 + m3
print("Sum of m1, m2, m3:")
print(sum_of_matrices)
print()

# Step 2: Element-wise division by the number of arrays (3)
average_matrix_arithmetic = sum_of_matrices / 3

print("Average of 2D arrays using arithmetic sum and division:")
print(average_matrix_arithmetic)

Output:

Sum of m1, m2, m3:
[[ 9 12]
[15 18]]

Average of 2D arrays using arithmetic sum and division:
[[3. 4.]
[5. 6.]]

Both methods yield the same result for element-wise averaging of 2D arrays. np.mean(axis=0) is often more convenient if you already have your arrays in a list that can be easily converted to a 3D array.

Calculating Weighted Averages of NumPy Arrays

Sometimes, you don't want a simple average; you want to give more importance (weight) to certain arrays. The sum of weights should typically (but not strictly necessarily for np.average) equal 1 for a standard weighted mean.

Weighted Average using Basic Arithmetic

For 1D arrays (can be extended to 2D if shapes match):

  1. Multiply each array by its corresponding weight.
  2. Sum the weighted arrays element-wise.
import numpy as np

readings_sensor1 = np.array([10, 12, 14])
readings_sensor2 = np.array([11, 11, 15]) # Sensor 2 is more reliable

weight_sensor1 = 0.4 # 40% weight
weight_sensor2 = 0.6 # 60% weight (sum of weights = 1.0)

# Calculate weighted sum
weighted_sum_arrays = (readings_sensor1 * weight_sensor1) + (readings_sensor2 * weight_sensor2)

print("Weighted average using arithmetic:")
print(weighted_sum_arrays)

Output:

Weighted average using arithmetic:
[10.6 11.4 14.6]

If the sum of weights is not 1, you would typically divide the weighted_sum_arrays by (weight_sensor1 + weight_sensor2) to normalize.

Weighted Average using numpy.average()

The numpy.average() function is specifically designed for this. It can compute the weighted average along a specified axis.

import numpy as np

# Using 1D arrays from 4.1
readings_sensor1 = np.array([10, 12, 14])
readings_sensor2 = np.array([11, 11, 15])
weights = np.array([0.4, 0.6]) # Weights for sensor1 and sensor2 respectively

# Stack arrays to make a 2D array where rows are the original arrays
# Shape will be (num_arrays, num_elements_per_array) -> (2, 3)
stacked_for_average = np.array([readings_sensor1, readings_sensor2])

# Calculate weighted average along axis=0 (averaging across the different sensors for each element position)
weighted_avg_np_average = np.average(stacked_for_average, axis=0, weights=weights)

print("Weighted average using numpy.average():")
print(weighted_avg_np_average)

Output:

Weighted average using numpy.average():
[10.6 11.4 14.6]
  • a: The input array (or list of arrays that can be stacked). Here, stacked_for_average.
  • axis=0: Calculates the weighted average across the "first" dimension (which corresponds to the different original arrays after stacking).
  • weights: An array of weights associated with the values along the specified axis. Its length must match the size of a along axis.

This method is cleaner and more direct for weighted averages, especially with multiple arrays or multi-dimensional arrays.

Key Considerations: Array Shapes

For element-wise arithmetic operations (+, /) to work directly as shown, the input NumPy arrays generally need to have the same shape. If shapes differ but are compatible according to NumPy's broadcasting rules, operations might still work but could lead to unexpected results if not understood. For np.mean(stacked_arrays, axis=0) or np.average(stacked_arrays, axis=0, weights=...), the individual arrays being stacked should ideally have the same shape for a meaningful element-wise average across them.

Conclusion

NumPy offers efficient and intuitive ways to calculate element-wise averages (means) of multiple arrays:

  • For a simple average of 1D or 2D arrays (same shape): Use direct element-wise addition followed by division by the number of arrays: (arr1 + arr2 + ... + arrN) / N.
  • For a more robust simple average, especially with multiple 2D arrays: Stack them into a higher-dimensional array and use numpy.mean(stacked_arrays, axis=0).
  • For weighted averages: Use direct arithmetic (arr1*w1 + arr2*w2 + ...) or, more conveniently, numpy.average(stacked_arrays, axis=0, weights=weights_array).

Always ensure your array shapes are compatible for the intended element-wise operation. These techniques are fundamental for many data aggregation and signal processing tasks in scientific computing.