mean¶
- mean(x: array, /, *, axis: int | Tuple[int, ...] | None = None, keepdims: bool = False) array ¶
Calculates the arithmetic mean of the input array
x
.- Parameters:
x (array) – input array. Should have a floating-point data type.
axis (Optional[Union[int, Tuple[int, ...]]]) – axis or axes along which to compute arithmetic means. By default, the mean must be computed over the entire array. If a tuple of integers, arithmetic means must be computed over multiple axes. A valid axis must be an integer on the interval
[-N, N)
, whereN
is the number of axes inx
. If an axis is specified as a negative integer, the function must determine the axis along which to perform the operation by counting backward from the last axis (where-1
refers to the last axis). If provided an invalid axis, the function must raise an exception. Default:None
.keepdims (bool) – if
True
, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see Broadcasting). Otherwise, ifFalse
, the reduced axes (dimensions) must not be included in the result. Default:False
.
- Returns:
out (array) – if the arithmetic mean is computed over the entire array, a zero-dimensional array containing the arithmetic mean; otherwise, a non-zero-dimensional array containing the arithmetic means. The returned array must have the same data type as
x
.
Notes
While this specification recommends that this function only accept input arrays having a floating-point data type, specification-compliant array libraries may choose to accept input arrays having an integer data type. While mixed data type promotion is implementation-defined, if the input array
x
has an integer data type, the returned array must have the default real-valued floating-point data type.
Special Cases
Let
M
equal the number of elements over which to compute the arithmetic mean. For real-valued operands,If
M
is0
, the arithmetic mean must beNaN
.If
x_i
isNaN
, the arithmetic mean must beNaN
(i.e.,NaN
values propagate).
For complex floating-point operands, real-valued floating-point special cases should independently apply to the real and imaginary component operations involving real numbers. For example, let
a = real(x_i)
andb = imag(x_i)
, andIf
M
is0
, the arithmetic mean must beNaN + NaN j
.If
a
isNaN
, the real component of the result must beNaN
.Similarly, if
b
isNaN
, the imaginary component of the result must beNaN
.
Note
Array libraries, such as NumPy, PyTorch, and JAX, currently deviate from this specification in their handling of components which are
NaN
when computing the arithmetic mean. In general, consumers of array libraries implementing this specification are recommended to useisnan()
to test whether the result of computing the arithmetic mean over an array have a complex floating-point data type isNaN
, rather than relying onNaN
propagation of individual components.Changed in version 2024.12: Added complex data type support.