The sum of elements, along with an axis divided by the number of elements, is known as arithmetic mean. The numpy.mean() function is used to compute the arithmetic mean along the specified axis.
This function returns the average of the array elements. By default, the average is taken on the flattened array. Else on the specified axis, float 64 is intermediate as well as return values are used for integer inputs
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>)
These are the following parameters in numpy.mean() function:
a: array_like
This parameter defines the source array containing elements whose mean is desired. In such a case where 'a' is not an array, a conversion is attempted.
axis: None, int or tuple of ints(optional)
This parameter defines the axis along which the means are computed. By default, the mean is computed of the flattened array. In version 1.7.0, if this is a tuple of ints, the mean is performed over multiple axes, instead of a single axis or all the axes as before.
dtype: data-type(optional)
This parameter is used to define the data-type used in computing the mean. For integer inputs, the default is float64, and for floating-point inputs, it is the same as the input dtype.
out: ndarray(optional)
This parameter defines an alternative output array in which the result will be placed. The shape of the resulting array should be the same as the shape of the expected output. The type of output values will cast when necessary.
keepdims: bool(optional)
When the value is true, the reduced axis is left as dimensions with size one in the output/result. Also, the result broadcasts correctly against the input array. When the default value is set, the keepdims does not pass via the mean method of sub-classes of ndarray, but any non-default value will surely pass. In case the sub-class method does not implement keepdims, then an exception will surely rise.
If we set the 'out' parameter to None, this function returns a new array containing the mean values. Otherwise, it will return the reference to the output array.
import numpy as np a = np.array([[1, 2], [3, 4]]) b=np.mean(a) b x = np.array([[5, 6], [7, 34]]) y=np.mean(x) y
Output:
In the above code
import numpy as np a = np.array([[2, 4], [3, 5]]) b=np.mean(a,axis=0) c=np.mean(a,axis=1) b c
Output:
In single precision, mean can be inaccurate:
import numpy as np a = np.zeros((2, 512*512), dtype=np.float32) a[0, :] = 23.0 a[1, :] = 32.0 c=np.mean(a) c
Output:
In the above code
In the output, it shows the mean of array 'a'.
Computing the mean in float64 is more accurate:
import numpy as np a[0, :] = 2.0 a[1, :] = 0.2 c=np.mean(a) c d=np.mean(a, dtype=np.float64) d
Output: