Python:NumPy mean()

MamtaWardhani's avatar
Published Nov 22, 2025
Contribute to Docs

The mean() method of a NumPy ndarray computes the arithmetic average of the array elements. The calculation can be performed across the entire array or along a specified axis. The result is either a scalar value or an array of means, depending on the chosen axis.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

ndarray.mean(axis=None, dtype=None, out=None, keepdims=False, initial=<no value>, where=True)

Parameters:

  • axis (optional): The axis or axes along which the mean is computed. If omitted, the mean of all elements is returned.
  • dtype (optional): Data type used during the calculation.
  • out (optional): Output array for storing the result.
  • keepdims (optional): Preserves reduced dimensions when set to True.
  • initial (optional): Starting value for the sum.
  • where (optional): A boolean mask that selects elements included in the mean.

Return value:

Returns a scalar or ndarray containing the computed arithmetic mean.

Example 1

In this example, the mean is computed across both axes: once for the entire array and once along each individual row:

import numpy as np
arr = np.array([
[2, 4, 6],
[8, 10, 12]
])
# Mean of all elements
overall_mean = arr.mean()
# Mean along each row
row_mean = arr.mean(axis=1)
print("Overall mean:", overall_mean)
print("Row-wise mean:", row_mean)

The output of this code is:

Overall mean: 7.0
Row-wise mean: [ 4. 10.]

Example 2

In this example, a boolean mask is used with where to compute the mean only across selected elements:

import numpy as np
arr = np.array([10, 20, 0, 40, 0])
mask = arr > 0 # Select only non-zero values
masked_mean = arr.mean(where=mask)
print("Mean of non-zero values:", masked_mean)

The output of this code is:

Mean of non-zero values: 23.333333333333332

Codebyte Example

Use this codebyte to compute the mean along a specific axis in a 2D array:

Code
Output
Loading...

Frequently Asked Questions

1. What is a NumPy ndarray?

A NumPy ndarray is a multidimensional, fixed-size array optimized for numerical computation. It stores elements of the same data type and supports fast vectorized operations, making it the core data structure of NumPy.

2. What is NumPy mean()?

The mean() function computes the arithmetic average of the selected array elements. It supports axes, masks, and type casting, making it suitable for both simple and high-performance statistical calculations.

3. How does mean() work in Python?

The mean() method sums the selected elements of the array and divides by the number of included elements. When an axis is specified, this process is applied along that dimension, returning an array of means.

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours