Average of NumPy Array - Examples
NumPy Array Average
Using NumPy, you can calculate the average of elements for an entire NumPy array, along a specific axis, or as a weighted average of elements.
To find the average of a NumPy array, you can use the numpy.average()
statistical function.
Syntax
The syntax of the numpy.average()
function is:
numpy.average(a, axis=None, weights=None, returned=False)
We will explain the parameters in this syntax using examples below.
Examples
1. Average of 2D NumPy Array
In this example, we calculate the average of a 2D NumPy array using the numpy.average()
function.
Python Program
import numpy as np
arr = np.array([[4, 5], [3, 7]])
avg = np.average(arr)
print('array\n', arr)
print('average\n', avg)
Output
array
[[4 5]
[3 7]]
average
4.75
Explanation
- The input array is
[[4, 5], [3, 7]]
. - All elements are summed:
4 + 5 + 3 + 7 = 19
. - The average is calculated as
19 / 4 = 4.75
. - The result,
4.75
, is printed as the output.
2. Average of NumPy Array Along an Axis
You can calculate the average along a specific axis using the axis
parameter.
Python Program
import numpy as np
arr = np.array([[4, 5], [3, 7]])
avg = np.average(arr, axis=1)
print('array\n', arr)
print('average along axis=1\n', avg)
Output
array
[[4 5]
[3 7]]
average along axis=1
[4.5 5. ]
Explanation
- The input array is
[[4, 5], [3, 7]]
. - The averages along
axis=1
(rows) are calculated: - Row 1:
(4 + 5) / 2 = 4.5
- Row 2:
(3 + 7) / 2 = 5.0
- The result,
[4.5, 5.0]
, is printed as the output.
3. Weighted Average of NumPy Array
You can specify weights to calculate a weighted average of elements in the array. The weights are multiplied with the elements, and their sum is divided by the total of the weights.
Python Program
import numpy as np
arr = np.array([[4, 5], [3, 7]])
avg = np.average(arr, axis=1, weights=[0.2, 0.8])
print('array\n', arr)
print('average along axis=1 with weights\n', avg)
Output
array
[[4 5]
[3 7]]
average along axis=1 with weights
[4.8 6.2]
Explanation
- The input array is
[[4, 5], [3, 7]]
. - Weights are
[0.2, 0.8]
. - Weighted averages are calculated for each row:
- Row 1:
(4*0.2 + 5*0.8) = 4.8
- Row 2:
(3*0.2 + 7*0.8) = 6.2
- The result,
[4.8, 6.2]
, is printed as the output.
4. Returned Parameter in NumPy Average
You can set returned=True
to return a tuple of the average and the sum of weights.
Python Program
import numpy as np
arr = np.array([4, 5, 6, 7])
avg, sum_weights = np.average(arr, weights=[1, 2, 3, 4], returned=True)
print('array\n', arr)
print('average\n', avg)
print('sum of weights\n', sum_weights)
Output
array
[4 5 6 7]
average
6.1
sum of weights
10
Explanation
- The input array is
[4, 5, 6, 7]
, and weights are[1, 2, 3, 4]
. - The weighted sum is
(4*1 + 5*2 + 6*3 + 7*4) = 60
. - The total sum of weights is
1 + 2 + 3 + 4 = 10
. - The average is
60 / 10 = 6.0
, and the sum of weights is10
.
Summary
In this NumPy Tutorial, we learned how to calculate the average of NumPy array elements using numpy.average()
, along an axis, with weights, and using the returned
parameter. These examples demonstrate the flexibility and utility of the numpy.average()
function.