Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.mean

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.

How it works

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.

Syntax

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

Parameters

NameTypeDescription
aarray_likeArray-like object containing the values to average.
axisNone, int, or tuple of ints, optionalAxis or axes to reduce; None reduces the flattened input.
dtypedata-type, optionalData type used to compute the mean.
outndarray, optionalOutput array receiving the result.
keepdimsbool, optionalWhether reduced axes remain as size-one dimensions.
wherearray_like of bool, optionalBoolean mask selecting values included in the mean.

Returns

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.

Key points

  • Use np.mean to calculate an arithmetic mean over an array or selected axes.
  • With axis=None, all elements are reduced as though the input were flattened.
  • A tuple of axis numbers lets one call reduce several dimensions together.
  • Integer inputs use float64 for the calculation by default.
  • Set dtype when you need to control the arithmetic type used during accumulation.

Common mistakes

  • Floating-point inputs retain their dtype by default, which can produce less accurate means than using a wider dtype.

Example: Compute overall and axis-specific means

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.

When to use it

Whole-array average

Summarize every value in a numeric array with one scalar.

Axis-wise reduction

Aggregate rows, columns, or multiple dimensions while retaining other dimensions.

Broadcast-compatible statistics

Produce a result that broadcasts against the original array for normalization or centering.

Interview questions

QWhat does np.mean do with axis=None versus an explicit axis?

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.

QHow does dtype affect np.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.

QWhen are keepdims and out useful?

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.average
  • numpy.median
  • numpy.mean
  • numpy.ndarray.mean

Source: numpy.mean. Checked 2026-10-15.