← Back to NumPy Course | Chapter 6: Math Operations | Lesson 5 of 7

np.mean/median/std

Quick summary numbers: the average, the middle value, and how spread out the data is.

In this page:

  1. np.mean/median/std
Syntax
python
np.mean(arr, axis=axis)
np.median(arr)
np.std(arr)

np.mean/median/std

mean is the arithmetic average, median the middle value, and std the standard deviation, a measure of spread. All accept an axis argument. np.median is a function only, not an array method.

Note: std uses ddof=0 by default (population); pass ddof=1 for the sample standard deviation.

Example: np.mean/median/std

python
import numpy as np

a = np.array([2, 4, 4, 4, 5, 5, 7, 9])
print(a.mean())
print(np.median(a))
print(a.std())
print(a.std(ddof=1).round(3))

# Output:
# 5.0
# 4.5
# 2.0
# 2.138
Related Topics
Common Mistakes
  1. Calling arr.median() which does not exist
  2. Forgetting ddof for sample statistics
  3. Averaging integers and expecting an integer
Chapter Summary
  • mean, median and std summarize data
  • axis controls direction
  • Default std is population
  • median is only np.median
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.