NumPy average()

The numpy.average() method computes the weighted average along the specified axis.

Example

import numpy as np

# create an array
array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7])

# calculate the average of the array avg = np.average(array1)
print(avg) # Output: 3.5

average() Syntax

The syntax of the numpy.average() method is:

numpy.average(array, axis = None, weights = None, returned = False, keepdims = <no value>)

average() Arguments

The numpy.average() method takes the following arguments:

  • array - array containing numbers whose average is desired (can be array_like)
  • axis (optional) - axis or axes along which the averages are computed (int or tuple of int)
  • weights (optional) - the weights associated with each value in array (array_like)
  • returned (optional) - return tuple (average, sum_of_weights) if True, else return average only.
  • keepdims (optional) - specifies whether to preserve the shape of the original array (bool)

Notes: The default values of numpy.average() have the following implications:

  • axis = None - the average of the entire array is taken.
    • weights = None - all values have the same weight (1)
    • By default, keepdims will not be passed.

average() Return Value

The numpy.average() method returns the weighted average of the array.


Example 1: Find the Average of a ndArray

import numpy as np

# create an array
array1 = np.array([[[0, 1], 
                    [2, 3]], 
                    [[4, 5], 
                    [6, 7]]])

# find the average of entire array average1 = np.average(array1) # find the average across axis 0 average2 = np.average(array1, 0) # find the average across axis 0 and 1 average3 = np.average(array1, (0, 1))
print('\naverage of the entire array:', average1) print('\naverage across axis 0:\n', average2) print('\naverage across axis 0 and 1:', average3)

Output

average of the entire array: 3.5

average across axis 0:
[[2. 3.]
 [4. 5.]]

average across axis 0 and 1: [3. 4.]

Example 2: Specifying Weights for Values of a ndArray

The weights parameter can be used to control the weight of each value in the input array.

import numpy as np

array1= np.array([[1, 2, 3],
                [4, 5, 6]])

# by default all values have the same weight(1) result1 = np.average(array1) # assign variable weights result2 = np.average(array1, weights = np.arange(0,6,1).reshape(2, 3)) # assign 0 weight to first column # to get average of 2nd and 3rd column result3 = np.average(array1, weights = [0, 1, 1], axis = 1)
print('No weights given:', result1) print('Variable weights:', result2) print('Average of 2nd and 3rd columns:', result3)

Output

No weights given: 3.5
Variable weights: 4.666666666666667
Average of 2nd and 3rd columns: [2.5 5.5]

When weights is not assigned, numpy.average() works the same as numpy.mean().

For example, in result1,

average = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 21 / 6 = 3.5

When weights is passed, the weighted average is taken.

For example, in result2,

weighted average
= sum(values * weights) / sum(weights)
= (1  * 0 + 2 * 1 + 3  * 2+ 4 * 3 + 5 * 4 + 6 * 5) / (15)
= 4.666666666667

Example 3: Using Optional keepdims Argument

If keepdims is set to True, the resultant average array is of the same number of dimensions as the original array.

import numpy as np

array1= np.array([[1, 2, 3],
                [4, 5, 6]])

# keepdims defaults to False result1 = np.average(array1, axis = 0) # pass keepdims as True result2 = np.average(array1, axis = 0, keepdims = True)
print('Dimensions in original array:',arr.ndim) print('Without keepdims:', result1, 'with dimensions', result1.ndim) print('With keepdims:', result2, 'with dimensions', result2.ndim)

Output

Dimensions in original array: 2
Without keepdims: [2.5 3.5 4.5] with dimensions 1
With keepdims: [[2.5 3.5 4.5]] with dimensions 2

Example 4: Using Optional returned Argument

The returned parameter allows to specify whether the return value should include just the calculated average or if it should include a tuple of the form (average, sum_of_weights).

import numpy as np

array1= np.array([[1, 2, 3],
                [4, 5, 6]])

# by default, returned = False # only average is returned avg = np.average(array1) # return the average and sum of weights avg2,sumWeights = np.average(array1, returned = True)
print('Average:', avg) print('Average:', avg2, 'with sum of weights:', sumWeights )

Output

Average: 3.5
Average: 3.5 with sum of weights: 6.0

Frequently Asked Questions

What happens when all weights are zeroes?

If all the weights are zeroes, we get ZeroDivisionError.

Let's look at an example.

import numpy as np

array1= np.array([[1, 2, 3],
                [4, 5, 6]])
weights = np.zeros(6).reshape(2, 3)

# compute average, by default returned = False, only average returned
avg = np.average(array1, weights = weights)

print('Average:', avg)

Output

ZeroDivisionError: Weights sum to zero, can't be normalized
What happens when the length of weights is different from the length of the array?

When the length of weights is not the same as the length of an array along the given axis, we get TypeError.

Let's look at an example.

import numpy as np

array1= np.array([1, 2, 3, 4, 5, 6])
weights = np.ones(5)

# the length of array is 6 whereas the length of weights is 5
avg = np.average(array1, weights = weights)

print('Average:', avg)

Output

TypeError: Axis must be specified when shapes of a and weights differ.

If the length of weights and the length of the array along the specified axis don't match, we get ValueError.

import numpy as np

array1= np.array([1, 2, 3, 4, 5, 6])
weights = np.ones(5)

# the length of array is 6 whereas the length of weights is 5
avg = np.average(array1, weights = weights, axis = 0)

print('Average:', avg)

Output

ValueError: Length of weights not compatible with specified axis.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges