Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

NumPy mean() function

In this tutorial, we will cover numpy.mean() function of the NumPy library.

  • The mean can be calculated easily just by adding all the items of the arrays and dividing by the total number of array elements.

  • The numpy.mean() function in the NumPy library is used to compute the arithmetic mean along the specified axis in an array.

  • So this function mainly returns the average of the array elements. By default, the average is calculated over the flattened array, otherwise, it will be calculated over the specified axis.

Syntax of numpy.mean():

The syntax required to use this function is as follows:

numpy.mean(a, axis=None, out, dtype) 

Parameters:

Below we have the description of Parameters used by this Function:

  • a
    This parameter is used to indicate the input array.

  • axis
    This parameter is used to indicate the axis along which we want to calculate the median. By default, the input array is flattened(that is working on all the axis). Here, for value of axis, axis = 0 means along the column and axis = 1 means working along the row.

  • out
    This is an optional parameter that is used to indicate an alternative array in which we want to place the result. The array must have the same dimensions as the expected output.

  • dtype
    It is an optional parameter that is used to indicate the type we desire while computing the mean.

Returned Values:

This function mainly returns the arithmetic mean of the array (it will return a scalar value if the axis is none) or an array with mean values along the specified axis.

Example 1:

Given below is a basic example showing you the working of this function:

import numpy as np 

x= [80, 23, 17, 1, 39] 

print("The Input array is : ")
print(x) 
print("The mean of the array is : ")
print(np.mean(x)) 


The Input array is :
[80, 23, 17, 1, 39]
The mean of the array is :
32.0

Example 2:

Now we will apply this function on two dimensional array and will check the output:

import numpy as np 

p = [[14, 19, 12, 34, 43], [16, 8, 28, 8, 20], [25, 5, 55, 1, 2]] 

# calculating mean of the flattened array 
print("\nThe mean of the array when axis = None : ")
print(np.mean(p)) 

# calculating the mean along the axis = 0 
print("\nThe mean of the array when axis = 0 : ")
print(np.mean(p, axis = 0)) 

# calculating the mean along the axis = 1 
print("\nThe mean of the array when axis = 1 : ")
print(np.mean(p, axis = 1)) 

out_arr = np.arange(3) 
print("\nout_arr : ", out_arr) 
print("mean of arr, axis = 1 : ")
print(np.mean(p, axis = 1, out = out_arr)) 


The mean of the array when axis = None :
19.333333333333332

The mean of the array when axis = 0 :
[18.33333333 10.66666667 31.66666667 14.33333333 21.66666667]

The mean of the array when axis = 1 :
[24.4 16. 17.6]

out_arr : [0 1 2]
mean of arr, axis = 1 :
[24 16 17]

Summary

In this tutorial we covered the numpy.mean(), a statistical function in Numpy library. We covered what is mean, syntax of the mean() function, mean() function parameters along with a few code examples for it.