🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.mean()

AI & DATA SCIENCE // np-mean

np.mean() computes the arithmetic average of array elements, either over the whole array or along a specified axis.

Syntax

np.mean(arr, axis=None)

Deep Dive Course

Without an axis argument, mean() collapses the entire array into a single scalar average. Specifying axis=0 computes the mean down each column, collapsing rows, while axis=1 computes it across each row, collapsing columns — a common point of confusion, since the axis you specify is the one that gets reduced away, not the one that survives in the result.

1Understanding np.mean()

Without an axis argument, mean() collapses the entire array into a single scalar average. Specifying axis=0 computes the mean down each column, collapsing rows, while axis=1 computes it across each row, collapsing columns — a common point of confusion, since the axis you specify is the one that gets reduced away, not the one that survives in the result.

💡

Remember the axis argument names the dimension being collapsed, not the one that remains — axis=0 on a 2D array reduces rows down to a single value per column, which trips people up expecting the opposite.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.mean() showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.mean(matrix, axis=0))
print(np.mean(matrix, axis=1))
localhost:3000

3Best Practices

Follow these guidelines when working with np.mean():

1. Double-check whether axis=0 or axis=1 matches your intent by testing on a small example, since the reduced-vs-remaining dimension is easy to get backwards

2. Use np.nanmean() instead of np.mean() when the data might contain NaN values that should be ignored rather than propagating a NaN result

3. Prefer np.mean() over manually summing and dividing by the length, since it correctly handles multi-dimensional axes and NaN-aware variants

⚠️

Tip: Remember the axis argument names the dimension being collapsed, not the one that remains — axis=0 on a 2D array reduces rows down to a single value per column, which trips people up expecting the opposite.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr))
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.mean(matrix, axis=0))
print(np.mean(matrix, axis=1))

Best Practices

  • Double-check whether axis=0 or axis=1 matches your intent by testing on a small example, since the reduced-vs-remaining dimension is easy to get backwards
  • Use np.nanmean() instead of np.mean() when the data might contain NaN values that should be ignored rather than propagating a NaN result
  • Prefer np.mean() over manually summing and dividing by the length, since it correctly handles multi-dimensional axes and NaN-aware variants

Interview Question

For a 2D array, why does np.mean(matrix, axis=0) return one value per column instead of one value per row?

Hint: Think about what 'axis' actually names — the dimension being collapsed or the dimension that remains.

The axis argument specifies which dimension gets collapsed, or reduced away, by the operation — axis=0 refers to the row dimension, so specifying it tells NumPy to average down through the rows for each column position, collapsing all the rows into one row of column-wise averages. The result therefore has one value per column, which is the dimension that remains after the row dimension is reduced. This is the opposite of what many people intuitively expect, since 'axis 0' sounds like it should describe what survives, not what's collapsed.

Exercises

MediumPractice using np.mean() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr))

Frequently Asked Questions

For a 2D array, why does np.mean(matrix, axis=0) return one value per column instead of one value per row?

The axis argument specifies which dimension gets collapsed, or reduced away, by the operation — axis=0 refers to the row dimension, so specifying it tells NumPy to average down through the rows for each column position, collapsing all the rows into one row of column-wise averages. The result therefore has one value per column, which is the dimension that remains after the row dimension is reduced. This is the opposite of what many people intuitively expect, since 'axis 0' sounds like it should describe what survives, not what's collapsed.

Related Functions

np-mediannp-stdndarray-shape