Python numpy.average() Function

Vaibhhav Khetarpal Jan 30, 2023
  1. Python numpy.average() Function
  2. Implement the numpy.average() Function in Python
  3. Comparisons With the numpy.mean() Function
Python numpy.average() Function

The several functions provided by the NumPy library are widely utilized to help in different fields while coding in Python. One of them is the numpy.average() function, which will be the focus of this article.

This tutorial discusses the numpy.average() function and how it can be implemented in Python with the help of the NumPy library.

Python numpy.average() Function

The numpy.average() function, as the name suggests, features under the list of functions provided by the NumPy library, which is a significant and popular library that makes it easy to deal with numbers and perform certain operations on them.

In simple terms, the numpy.average() function is utilized to calculate the weighted average of a certain array-like structure along an axis that needs to be specified.

The syntax for the numpy.average() and its parameters have been mentioned and explained below for ease of understanding for the readers.

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

The parameters related to this function are explained further below.

  1. a - It is the object on which the function will operate. In this case, it is an array-like structure. If not, an automatic conversion process might be tried.
  2. axis - The axis or the axes along which the average must be computed is specified/stored in this parameter.
  3. weights - An array similar to the size of a which contains the weights of the elements in the array a respectively.
  4. The parameters returned and keepdims are optional and irrelevant to the examples taken in this code. However, both can be easily found on the internet for curious readers.

Implement the numpy.average() Function in Python

Implementing the numpy.average() function is straightforward and can be easily understood by beginners.

The following code implements the numpy.average() function.

import numpy as np

a = [10, 20, 30, 80]
b = np.average(a)
print(b)

The above code provides the following output.

35.0

Comparisons With the numpy.mean() Function

At first glance, both the numpy.average() function and the numpy.mean() function look to perform the same task, and in general numbers scenarios, they even provide the same results.

Let us take the above example and calculate the list’s average and mean.

import numpy as np

a = [10, 20, 30, 80]
b = np.average(a)
c = np.mean(a)
print(b)
print(c)

The above code provides the following output.

35.0
35.0

However, both of these have their unique actions as well, which makes them differ from each other.

The numpy.average() function contains a weight parameter, which can compute the weighted average of the given specified array-like structure; this feature is lacking in the numpy.mean() function.

Moreover, the numpy.mean() function has a dtype parameter, which allows it to be useful for making the function work on not just numbers but any ambiguous objects that may be passed on to the function. The numpy.average() function, on the other hand, lacks this feature and only works for integers.

We should also note that the numpy.mean() function takes masks into account, which means that the mean calculated with the help of this function only considers the unmasked values. The numpy.average(), however, does not take in the concept of masks.

Vaibhhav Khetarpal avatar Vaibhhav Khetarpal avatar

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

LinkedIn