NumPy | mean method
mean(~) method computes the mean value along the specified axis.
The input array.
The axis along which to compute the mean. For 2D arrays, the allowed values are as follows:
Row-wise computation of mean
Column-wise computation of mean
All values used to compute the mean
The data-type to use during the computation of the mean. If the inputs are integers, then
float64 is used. Otherwise, the same data-type will be used.
As a best practice, you should specify
dtype since if your input is of type
float32 will be used during the computation of the mean, which would result in less accurate results.
NumPy array |
Instead of creating a new array, you can place the computed mean into the array specified by
If axis is unset, then a scalar is returned. Otherwise, a Numpy array is returned.
Computing the mean of 1D array
Computing the mean of 2D array
Consider the following array:
a = np.array([[1,2],[3,4]])aarray([[1, 2],[3, 4]])
Mean of all values
Mean of each column
np.mean(a, axis=0)array([2., 3.])
Mean of each row
np.mean(a, axis=1)array([1.5, 3.5])