NumPy average function in Python [4 Use cases]

In this Python article, I will explain the NumPy average function in Python, its syntax, the parameters required, and the return value. I will also explain the difference between the np.average and np.mean() function.

To calculate the mean of elements in an array, the NumPy average function in Python is highly versatile. It not only computes simple averages but also weighted averages, where different elements carry distinct weights. Additionally, it skillfully handles multidimensional arrays, allowing averaging across specified axes, making it a vital tool in data analysis and scientific computing.

NumPy average function in Python

The np.average() function in Python is a part of the NumPy library, a fundamental package for scientific computing. This function calculates the average of the elements in an array.

It offers more flexibility than the standard mean calculation, as it can compute a weighted average if weights are provided.

Syntax and parameter required in the np.average() function

The basic syntax of the NumPy average function in Python is:

numpy.average(arr, axis=None, weights=None, returned=False)

Here,

arrThe Python array that contains the data.
axisThe axis along which to average the array. The default is None, averaging all the data.
weightsAn array of weights associated with the elements of a. By default, all data have equal weight.
returnedIf True, returns a tuple of the average and the sum of the weights. Useful for weighted averages.
List of parameters required in the NumPy average function in Python.

np.average() in Python use cases

There can be many different use cases of the np.average() in Python.

1. NumPy array average in Python

The simplest use of Python np.average() is to find the mean of an array.

import numpy as np

temperatures = np.array([70, 72, 68, 65, 67, 69, 71])
average_temp = np.average(temperatures)
print(f"Average Temperature: {average_temp} °F")

Output:

Average Temperature: 68.85714285714286 °F

After the implementation of the code in the Pycharm editor, the screenshot is mentioned below.

Numpy average function in Python

2. Weighted Average of NumPy array in Python

One of the key features of np.average() in Python is its ability to compute weighted averages.

import numpy as np

scores = np.array([85, 90, 95, 80])
weights = np.array([0.2, 0.25, 0.35, 0.2])
weighted_average = np.average(scores, weights=weights)
print(f"Weighted Average Score: {weighted_average}")

Output:

Weighted Average Score: 88.75

After executing the code in Pycharm, one can see the output in the below screenshot.

numpy average of array in Python

3. Average of 2d array in Python NumPy

We can find the average of all the data in the 2D array using the np.average() function in Python.

import numpy as np

Salaries = np.array([[41000, 51000, 31000],
                  [71000, 50700, 20800]])
monthly_avg_Salaries = np.average(Salaries)
print(f"Average Monthly Salaries offered by the company: {monthly_avg_Salaries}")

Output:

Average Monthly Salaries offered by the company: 44250.0

After executing the code in Pycharm, one can see the output in the below screenshot.

np average in Python

4. np.average in Python with different axes

The np.average() Python NumPy function can operate along specified axes in a multidimensional array.

Case 1: find the average of rows in a 2D array through the NumPy average function in Python. Here, we will use the axis=0 parameter.

import numpy as np

sales = np.array([[12000, 15000, 13000],  # East region sales
                  [10000, 11000, 9000]])  # West region sales
monthly_avg_sales = np.average(sales, axis=0)
print(f"Average Monthly Sales (East, West): {monthly_avg_sales}")

Output:

Average Monthly Sales (East, West): [11000. 13000. 11000.]

After executing the code in Pycharm, one can see the output in the below screenshot.

numpy.average in Python

Case 2: find the NumPy average columns in a 2D array through the axis=1 parameter within the NumPy average function in Python.

import numpy as np
sales = np.array([[14000, 15000, 13000],  # East region sales
                  [10000, 11000, 9000]])  # West region sales
region_avg_sales = np.average(sales, axis=1)
print(f"Average Sales per Region: {region_avg_sales}")

Output:

Average Sales per Region: [14000. 10000.]

After executing the code in Pycharm, one can see the output in the below screenshot.

python numpy average

np.average vs np.mean in Python

While np.mean and np.average can compute averages, the key difference is that np.average() in Python can compute weighted averages, whereas np.mean() in Python always treats elements equally.

import numpy as np

incomes = np.array([50000, 60000, 55000, 70000, 65000])
mean_income = np.mean(incomes)
average_income = np.average(incomes)

print(f"Mean Income: {mean_income}")
print(f"Average Income (using np.average): {average_income}")

Output:

Mean Income: 60000.0
Average Income (using np.average): 60000.0

After the implementation of the code in the Pycharm editor, the screenshot is mentioned below.

average numpy vs mean in Python

Conclusion

Understanding the NumPy average function in Python with its syntax, parameters required, and different use cases can help one to find the mean of the values easily. And, also knowing the basic difference between the np.average and np.mean functions can help one to utilize them wisely.

You may also like to read: