NumPy average()

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

Example


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

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.

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.

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).

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.