How To Compute Average Of NumPy Array?

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 18, 2022

NumPy average() function is used to compute the weighted average along the specified axis. This is a statistical function used to calculate the weight mean along the specified axis. Without weights parameter average() will behave same as NumPy mean() function.

In this article, I will explain average() syntax, parameters, and how to get the average of a total number of elements of the NumPy array, or along some axis, or you can also calculate a weighted average of elements.

1. Quick Examples of Python NumPy Average Function

If you are in a hurry, below are some quick examples of how to calculate the average of an array by using the NumPy average() function.


# Below are the quick examples

# Example 1: Get the average of 2-D array
arr2 = np.average(arr) 

# Example 2: Get the average of array along axis = 0
arr2 = np.average(arr, axis=0) 

# Example 3: Get the average of array along axis = 1
arr2 = np.average(arr, axis=1) 

# Example 4: Get the average with weights to average along axis = 0
arr2 = np.average(arr, axis=0, weights=[0.3,0.7])

# Example 5: Get the average with weights to average along axis = 1 
arr2 = np.average(arr, axis=1, weights=[0.3,0.7,0.8])

# Example 6: Get the average of array along with weights, axis = 1,& returned
arr2 = np.average(arr, axis=1, weights=[0.3,0.7,0.8], returned = True)

2. Syntax of NumPy average()

Following is the syntax of the average() function.


# Syntax of NumPy average()
numpy.average(arr, axis=None, weights=None, returned=False)

2.1 Parameters of average()

Below are the parameters of the average() function.

  • arr – An array containing data to be averaged. If it’s not an array, a conversion is attempted.
  • axis – int or None or tuple of ints(optional) Axis or axes along which to average arr. By default, the axis is set to None, which will calculate the average of all the elements of the source array.
  • weights – This parameter defines an array containing weights associated with the values in arr. Each value in arr contributes to the average according to its associated weight. The weighted array can be one-dimensional or of the same shape as the input array. Average = sum(arr * weights) / sum(weights)
  • returned – By default, this parameter is set to False. If you set it as True, then a tuple of average and sum_of_weights is returned. If it is False, the average is returned. If weights=None, sum of weights is equivalent to the number of elements over which the average is taken.

2.2 Return Value of average()

It returns the average of elements in an array if the returned parameter is set to False. If it is set to True it returns both the average and the sum of weights.

3. Usage of NumPy average() Function

NumPy average() function is a statistical function for calculating the average of a total number of elements in an array, or along a specified axis, or we can also calculate the weighted average of elements in an array. Note that average is used to calculate the standard deviation of the NumPy array.

Let’s create a 2-Dimensional array using numpy.array() function and compute the average of an array.


import numpy as np
# Create 2-D NumPy array
arr = np.array([[6, 8, 4], 
               [ 9, 5, 7]])
 
# Get the average of 2-D array
arr2 = np.average(arr) 
print(arr2)

# Output
# 6.5

4. Get the Average of 2-D Array along with Axis

In order to find out the average of an array along with an axis we need to pass the axis parameter to the function. To find the average values of each column use axis 0, and to get the average values of each row use axis 1. You can specify the axis of interest using the axis parameter.


# Get the average values over row 
# for each of 3 columns 
arr2 = np.average(arr, axis=0) 
print(arr2)

# Output
# [7.5 6.5 5.5]

# Get the average values over column
# for each of 2 rows
arr2 = np.average(arr, axis=1) 
print(arr2)

# Output
# [6. 7.]

5. Average with Axis & Weights

We can also specify the weights param while calculating the average of elements. These weights multiply with the elements and then calculate the average of the array.


# Get the average of array along with weights and axis = 0
arr2 = np.average(arr, axis=0, weights=[0.3,0.7])
print(arr2)

# Output
# [8.1 5.9 6.1]

# Get the average of array along with weights and axis = 1
arr2 = np.average(arr, axis=1, weights=[0.3,0.7,0.8])
print(arr2) 

# Output
# [5.88888889 6.55555556]

6. Using Axis, Weights & Returned

Set returned parameter to True and pass along with axis and weights to the average() function, this will compute the average of the given array and will return the tuple of average and sum of weights


# Get the average of array along with weights, axis = 1,& returned
arr2 = np.average(arr, axis=1, weights=[0.3,0.7,0.8], returned = True)
print(arr2) 

# Output :
# (array([5.88888889, 6.55555556]), array([1.8, 1.8]))

Conclusion

In this article, I have explained the syntax and usage of numpy.average() function which calculates the average of elements, or along some axis. And I have also covered calculating a weighted average of elements with examples.

Happy Learning!!

References

Leave a Reply

You are currently viewing How To Compute Average Of NumPy Array?