NumPy Statistics and Random
NumPy has fast functions to summarise data and to generate random numbers.
Basic Statistics
Example
import numpy as np
sales = np.array([1200, 1500, 900, 1800, 1100, 2000, 1600])
print("Total:", sales.sum())
print("Average:", sales.mean())
print("Median:", np.median(sales))
print("Lowest:", sales.min())
print("Highest:", sales.max())
print("Std dev:", round(sales.std(), 2))Total: 10100 Average: 1442.857142857143 Median: 1500.0 Lowest: 900 Highest: 2000 Std dev: 365.89
| Function | Meaning |
|---|---|
sum() | Adds all values |
mean() | Average |
np.median() | Middle value after sorting |
min() / max() | Smallest / largest value |
std() | Standard deviation: how spread out the values are |
var() | Variance (std squared) |
argmin() / argmax() | Index of the smallest / largest value |
cumsum() | Running total |
Finding Where the Max Is
Example
import numpy as np
days = np.array(["Mon", "Tue", "Wed", "Thu", "Fri"])
sales = np.array([1200, 1500, 900, 1800, 1100])
best = sales.argmax()
print("Best day:", days[best])
print(sales.cumsum())Best day: Thu [1200 2700 3600 5400 6500]
Working Along an Axis
For 2-D arrays, the axis parameter decides the direction:
axis=0goes down the rows, giving one result per columnaxis=1goes across the columns, giving one result per row
Example
import numpy as np
# rows = students, columns = Maths, English, Science
marks = np.array([
[85, 72, 90],
[60, 88, 75],
[92, 95, 89]
])
print("Total of all:", marks.sum())
print("Average per subject:", marks.mean(axis=0))
print("Total per student:", marks.sum(axis=1))Total of all: 746 Average per subject: [79. 85. 84.66666667] Total per student: [247 223 276]
An easy way to remember: the axis you name is the one that disappears. axis=0 removes the rows, so you are left with one number per column.
Random Numbers
The modern way is to create a random generator with np.random.default_rng(). Giving it a seed number makes the results repeat exactly every time you run the program, which is useful for testing and sharing.
Example
import numpy as np
rng = np.random.default_rng(42)
print(rng.integers(1, 7, size=5)) # five dice rolls
print(rng.random(3)) # three decimals from 0 to 1[1 5 4 3 3] [0.69736803 0.09417735 0.97562235]
Without a seed, you get different numbers each run.
| Method | What it gives |
|---|---|
rng.integers(low, high, size) | Whole numbers from low up to (not including) high |
rng.random(size) | Decimals between 0 and 1 |
rng.normal(mean, std, size) | Values from a bell-shaped (normal) distribution |
rng.choice(arr, size) | Random picks from an array |
rng.shuffle(arr) | Mixes the order of an array in place |
Simulating Data
Random numbers are great for practice data. Here we create 1000 fake heights and check their average:
Example
import numpy as np
rng = np.random.default_rng(7)
heights = rng.normal(165, 8, size=1000)
print(round(heights.mean(), 1))
print(heights.shape)164.4 (1000,)
The average comes out close to 165, as we asked.
Exercise
Exercise
For a 2-D array m, what does m.sum(axis=1) return?
