Last updated: Apr 12, 2024
Reading timeยท5 min
To calculate the average (mean) of 2 NumPy arrays:
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:
The same approach can be used to calculate the average of 3 or more NumPy arrays.
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.
If you need to calculate the average (mean) of multiple 2-dimensional NumPy arrays:
numpy.mean()
method with the resulting array.axis
argument to 0
.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.
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.
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.
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)
You can also use division to calculate the average of 2 two-dimensional NumPy arrays.
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.
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.
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.
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.
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.
arr3 = (arr1 + arr2 + arr3) / 3 # [[3. 4.] # [5. 6.]] print(arr3)
If you need to calculate the weighted average of 2 NumPy arrays:
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.
numpy.average()
You can also use the numpy.average() method to calculate the weighted average of 2 NumPy arrays.
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.