How is np.mean() different from np.average() in NumPy?
Last Updated :
28 Apr, 2025
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()
|
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.
Similar Reads
How to Calculate Mean Absolute Error in Python? When building machine learning models, our aim is to make predictions as accurately as possible. However, not all models are perfect some predictions will surely deviate from the actual values. To evaluate how well a model performs, we rely on error metrics. One widely used metric for measuring pred
3 min read
Difference between Numpy array and Numpy matrix While working with Python many times we come across the question that what exactly is the difference between a numpy array and numpy matrix, in this article we are going to read about the same. What is np.array() in PythonThe Numpy array object in Numpy is called ndarray. We can create ndarray using
3 min read
Numpy MaskedArray.average() function | Python numpy.MaskedArray.average() function is used to return the weighted average of array over the given axis. Syntax : numpy.ma.average(arr, axis=None, weights=None, returned=False) Parameters: arr :[ array_like] Input masked array whose data to be averaged. Masked entries are not taken into account in
3 min read
How to Calculate Moving Averages in Python? In this discussion we are going to see how to Calculate Moving Averages in Python in this discussion we will write a proper explanation What is Moving Averages?Moving Averages, a statistical method in data analysis, smooths fluctuations in time-series data to reveal underlying trends. Calculating th
11 min read
How to Create a Normal Distribution in Python PyTorch In this article, we will discuss how to create Normal Distribution in Pytorch in Python. torch.normal() torch.normal() method is used to create a tensor of random numbers. It will take two input parameters. the first parameter is the mean value and the second parameter is the standard deviation (std
2 min read