Difference Between NumPy's mean() and average() Methods

numpy.mean() Vs. numpy.average() Method: In this tutorial, we will learn about the numpy.mean() and numpy.average() methods and the differences between these methods with the help of examples. By Pranit Sharma Last updated : June 04, 2023

The numpy.mean() method is used to compute the arithmetic mean along with the specified axis, whereas, the numpy.average() method is used to compute the weighted average along the specified axis. Both of the methods are of numpy library and work on the numpy arrays. Let's understand both of the methods in detail.

numpy.mean() Method

The numpy.mean() method computes the arithmetic mean i.e., an average value along with the axis.

numpy.mean(arr, axis = None)


# numpy.mean() method
import numpy as np
# 1D array
inputArr = [10, 12, 16, 40, 50]

print("inputArr : ", inputArr)
print("Mean of inputArr : ", np.mean(inputArr))


inputArr :  [10, 12, 16, 40, 50]
Mean of inputArr :  25.6

numpy.average() Method

The numpy.average() method computes the weighted average along with the axis. Note that numpy.average() returns the same result as numpy.mean() when weight is not specified.

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


# numpy.average() method
import numpy as np

# 1D array
arr = [1, 2, 3, 4]

print("arr : ", arr)
print("Average of arr : ", np.average(arr))

# weight array
wts = np.array([4, 3, 2, 1])
print("Weighted average of arr : ", np.average(arr, weights=wts))


arr :  [1, 2, 3, 4]
Average of arr :  2.5
Weighted average of arr :  2.0

NumPy's mean() and average() Methods Difference Summary

The main difference between numpy.mean() and numpy.average() method is that the mean() performs the simple arithmetic mean operation and returns an average of the array elements, whereas, the average() performs a weighted average operation by providing weights for each element.

Python NumPy Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.