How to find the mean of a Numpy array?

The numpy.mean() function calculates the arithmetic mean of the numpy array.

Example

import numpy as np
arr = [5,2,6,21,63]
print("Our array: ", arr) 
print("mean of our array : ", np.mean(arr))

We can also calculate the mean across different axis such as rows or columns by specifying the axis parameter. Specifying axis = 0 means the mean will be calculated along the column and axis = 1 specifies to find the mean across the rows.

Example

import numpy as np  
  
arr = [[1, 17, 12, 12, 36],  
       [78, 16, 27, 70, 21], 
       [98, 45, 42, 11, 45, ]] 
     
# mean along the columns
print("mean of our array along the columns by setting axis = 0 : ", np.mean(arr, axis = 0)) 
   
# mean along the rows
print("mean of our array along the rows by setting axis = 1 : ", np.mean(arr, axis = 1))