Whole-array average
Summarize every value in a numeric array with one scalar.
NumPy · Function
Computes the arithmetic mean of array elements, either across the flattened input or along selected axes. Optional controls set the calculation dtype, output destination, retained dimensions, and included elements.
NumPy converts the input to an array and averages its elements. With no axis, it reduces the flattened array; specifying an axis or tuple of axes reduces only those dimensions, while dtype controls the calculation type and where can restrict included elements.
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>, *, where=<no value>)| Name | Type | Description |
|---|---|---|
a | array_like | Array-like object containing the values to average. |
axis | None, int, or tuple of ints, optional | Axis or axes to reduce; None reduces the flattened input. |
dtype | data-type, optional | Data type used to compute the mean. |
out | ndarray, optional | Output array receiving the result. |
keepdims | bool, optional | Whether reduced axes remain as size-one dimensions. |
where | array_like of bool, optional | Boolean mask selecting values included in the mean. |
An ndarray or scalar containing the mean; its shape is determined by the reduced axes, and dtype follows the computation and output rules described on the page.
import numpy as np
values = np.array([[1, 2, 3], [4, 5, 6]])
print(np.mean(values))
print(np.mean(values, axis=0))
print(np.mean(values, axis=1))Computes the overall mean, then means for each column and row.
Summarize every value in a numeric array with one scalar.
Aggregate rows, columns, or multiple dimensions while retaining other dimensions.
Produce a result that broadcasts against the original array for normalization or centering.
np.mean computes the arithmetic average. Without axis, it averages all elements after flattening; with axis, it reduces the selected dimension or dimensions.
Follow-up: How can selected elements be excluded? Pass a boolean where mask; only positions where the mask is True contribute to the mean.
The default dtype is float64 for integer inputs and the input floating dtype for floating-point inputs. The output is a scalar when all axes are reduced, otherwise it has the remaining dimensions.
Follow-up: Why might dtype=np.float64 be useful for float32 input? It can improve accumulation accuracy when the default float32 computation is insufficient.
keepdims preserves reduced axes with length one, allowing the result to broadcast against the original array. out can receive the result in a supplied array.
Follow-up: What must be checked when using out? Its shape and dtype must be suitable for the reduction result and conversion.
numpy.averagenumpy.mediannumpy.meannumpy.ndarray.mean