Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (20.3k points)

I notice that

In [30]: np.mean([1, 2, 3])

Out[30]: 2.0

In [31]: np.average([1, 2, 3])

Out[31]: 2.0

However, there should be some differences, since after all, they are two different functions.

What are the differences between them?

2 Answers

0 votes
by (40.4k points)

np.average will take an optional weight parameter. If it is not supplied they are equivalent.

 code is as follows for np.mean:


    mean = a.mean

except AttributeError:

    return _wrapit(a, 'mean', axis, dtype, out)

return mean(axis, dtype, out)

Source code for np.average:


if weights is None :

    avg = a.mean(axis)

    scl = avg.dtype.type(a.size/avg.size)


    #code that does weighted mean here

if returned: #returned is another optional argument

    scl = np.multiply(avg, 0) + scl

    return avg, scl


    return avg


0 votes
by (106k points)

In some versions of numpy there is another important difference that you must be aware:

average does not take into account masks, so compute the average over the whole set of data.

mean takes in account masks, so compute the mean only over unmasked values.

g = [1,2,3,55,66,77] 

f =,5) 


Out: 34.0 


Out: 2.0

Browse Categories