numpy.mean() in Python

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

Syntax

snippet
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>)

Parameters

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.

Return

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.

Example 1:

snippet
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:

Output
2.5 13.0

In the above code

  • We have imported numpy with alias name np.
  • We have created two arrays 'a' and 'x' using np.array() function.
  • We have declared the variable 'b' and 'y' and assigned the return value of np.zeros() function.
  • We have passed arrays 'a' and 'x' in the function.
  • Lastly, we tried to print the value of 'b' and 'y'.

Example 2:

snippet
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:

Output
array([2.5, 4.5]) array([3., 4.])

Example 3:

In single precision, mean can be inaccurate:

snippet
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:

Output
27.5

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.zeros() function with dtype float32.
  • We have set the value of all the elements of 1st row to 23.0 and 2nd row 32.0.
  • We have passed the array 'a' in the function and assigned the return value of the np.mean() function.
  • Lastly, we tried to print the value of 'c'.

In the output, it shows the mean of array 'a'.

Example 4:

Computing the mean in float64 is more accurate:

snippet
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:

Output
1.0999985 1.1000000014901161
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +