In the Numpy library, there are two functions np.mean() and np.average() are present. Both are actually doing nearly the same job of calculating mean/average. The difference comes when we are calculating the weighted average. If that is the case then we have to use np.average(). With np.average function we can calculate both arithmetic mean and weighted average. In this article, we have shown the basic use case of both functions and how they are different from each other.
Difference between np.average() and np.mean()
np.mean() |
np.average() |
---|---|
Use to calculate arithmetic mean |
Use to calculate the arithmetic mean as well as weighted average. |
All elements have equal weight |
All elements may or may not have equal weight. |
Weight cannot be passed trough the parameter of the given function. |
Weight can be passed through the parameter of the given function. |
Syntax : np.mean(arr, axis = None)
where ‘arr’ is the given array. |
Syntax : numpy.average(arr, axis = None, weights = None) Where ‘arr’ is the given array
|
np.mean()
In numpy library, np.mean() is a function used to calculate arithmetic mean of the given array along with the axis. Lets see the code implementation.
Code
This code calculates the mean (average) of a NumPy array named ‘arr’ and prints both the array and its mean value.
Python3
import numpy as np arr = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]) print ( "Array : " ,arr, "\n" ) print ( "Mean of the Array : " ,np.mean(arr)) |
Output
Array : [ 1 2 3 4 5 6 7 8 9 10]
Mean of the Array : 5.5
Complexity of the above method:
Time complexity : O(n), where n is no. of elements
Auxiliary Space : O(1)
Note : In 2d arrays time complexity will be O(m*n) , where m is no. of rows and n is no. of columns. Same for other dimensional arrays too.
np.average()
The arithmetic mean and weighted average calculations are more flexible with np.average(). It can calculate the weighted average if we pass the weight; if not, it returns the same value as np.mean(). This implies that the arithmetic mean is the result when we do not pass the weight condition. So let’s jump into implementing the code right away. So here goes the code.
Code
This code calculates the mean and weighted average of a NumPy array. The mean is the average of the array’s elements, and the weighted average considers assigned weights for each element in the array.
Python3
import numpy as np arr = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]) print ( "Array : " ,arr, "\n" ) print ( "Mean of the Array : " ,np.average(arr), "\n" ) weight = np.array([ 4 , 5 , 6 , 12 , 15 , 10 , 2 , 8 , 19 , 20 ]) print ( "Weight average of Array : " ,np.average(arr, weights = weight)) |
Output
Array : [ 1 2 3 4 5 6 7 8 9 10]
Mean of the Array : 5.5
Weight average of Array : 6.574257425742574
Complexity of the above method:
Time complexity : O(n), where n is no. of elements
Auxiliary Space : O(1)
Note : In 2d arrays time complexity will be O(m*n) , where m is no. of rows and n is no. of columns. Same for other dimensional arrays too.
Conclusion
In numpy library, both np.mean() and np.average() can be used to calculate arithmetic mean. The differenece comes when we have to perform average on weighted array. np.average() gives us flexibilty to work with the weighted arrays too. If we pass the weight of the array to the function, it can perform weighted average at very ease. That is not the case for the np.mean(). It can only be perfomred on non-weighted arrays.