# Calculate the average (mean) of 2 NumPy arrays

Last updated: Apr 12, 2024
5 min

## #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
```Copied!```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)
``````

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
```Copied!```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)
``````

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
```Copied!```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)
``````

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

main.py
```Copied!```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
```Copied!```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
```Copied!```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 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
```Copied!```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)
``````

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

main.py
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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)
``````

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
```Copied!```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 `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.