Ravindra BagaleCourses & study guides मराठी Track your progress

NumPy Tutorial for Beginners: Arrays, Maths and Filtering in Python

P12.4 NumPy Statistics: sum, mean, median, min, max, std

Why. The first look at any data column is its summary: total, average, spread.

What. Arrays have methods like .sum(), .mean(), .min(), .max(), .std(). np.median() and np.percentile() are functions.

How.

Summarise a column

  1. Put the values in an array.
  2. Call the summary methods.
  3. Round for display.

Worked example A — Summary of delivery times.

Step 1 — What we do. Delivery minutes for 8 orders in Nagpur.

Step 2 — Type and run this code.

import numpy as np
mins = np.array([18, 22, 25, 19, 40, 21, 23, 20])
print("mean:", mins.mean())
print("median:", np.median(mins))
print("min/max:", mins.min(), mins.max())
print("std:", round(mins.std(), 2))

Step 3 — Output you should see.

mean: 23.5
median: 21.5
min/max: 18 40
std: 6.58

Step 4 — What it means. One slow order (40) pulls the mean above the median. std shows how spread out the times are.

Worked example B — Percentiles.

Step 1 — What we do. What delivery time covers 90% of orders?

Step 2 — Type and run this code.

import numpy as np
mins = np.array([18, 22, 25, 19, 40, 21, 23, 20])
print(np.percentile(mins, 90))

Step 3 — Output you should see.

29.499999999999996

Step 4 — What it means. 90% of orders were delivered within about this many minutes — a common service promise metric.

Ravindra Bagale's Tip

Always look at the median next to the mean. If they are far apart, a few extreme values are present.