← Back to NumPy Course | Chapter 8: Statistics | Lesson 1 of 6

np.mean/median/std/var

Four core measures: average, middle, spread and squared spread.

In this page:

  1. np.mean/median/std/var
Syntax
python
np.mean(arr)
np.median(arr)
np.std(arr)
np.var(arr)

np.mean/median/std/var

mean, median, std and var summarize the centre and spread of data. Variance is the average squared distance from the mean and std is its square root. On 2-D data, pass axis to summarize columns or rows.

Note: var equals std squared.

Example: np.mean/median/std/var

python
import numpy as np

data = np.array([[1, 2, 3], [4, 5, 6]])
print(np.mean(data), np.median(data))
print(np.var(data).round(3), np.std(data).round(3))
print("column means:", np.mean(data, axis=0))

# Output:
# 3.5 3.5
# 2.917 1.708
# column means: [2.5 3.5 4.5]
Related Topics
Common Mistakes
  1. Forgetting that var units are squared
  2. Ignoring outliers that distort mean
  3. Using the wrong axis
Chapter Summary
  • mean gives the centre
  • median resists outliers
  • var is std squared
  • axis picks column or row summaries
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.