Numpy `mean()`

function is used to calculate arithmetic mean of the values along the specified axis.

np.mean(a, axis=None, dtype=None)

Here, `a`

parameter is used to pass Numpy array to `mean()`

function.

`axis`

parameter is optional and is used to specify the axis along which we want to perform arithmetic mean. When no axis value is passed then arithmetic mean of entire value is computed and a single value is returned.

`dtype`

parameter controls the data type of the resultant mean.

import numpy as np a = np.array([32, 4, 8, 12, 20]) print('Original array is ', a) print('Mean is ', np.mean(a))

Output of the above program

Original array is [32 4 8 12 20] Mean is 15.2

As you can see in the above output, `np.mean()`

has computed mean for this array. In 1-dimensional array, it is optional to provide `axis`

parameter to `mean()`

function.

import numpy as np a = np.array([[32, 4, 8, 12, 20], [35, 5, 15, 10, 30]]) print('Mean of arr is ', np.mean(a)) print('Mean of arr along axis0 is ', np.mean(a, axis=0)) print('Mean of arr along axis1 is ', np.mean(a, axis=1))

Output of the above program

Mean of arr is 17.1 Mean of arr along axis0 is [33.5 4.5 11.5 11. 25. ] Mean of arr along axis1 is [15.2 19. ]

When axis value is not provided, then `mean()`

function works like this-

When axis=0 is passed to `mean()`

function, then it works like this-

When axis=1 is passed to `mean()`

function, then it works like this-

- How to use NumPy sum() in Python?
- How to use NumPy max() in Python?
- How to use NumPy min() in Python?
- How to use NumPy average() in Python?
- How to use NumPy sort() in Python?
- How to use NumPy matrix() in Python?
- How to use NumPy transpose() in Python?
- How to use NumPy dot() in Python?