Descriptive statistics and random sampling

2.5.5

Measure center and spread, handle NaN deliberately, and use a reproducible random generator.

On this page

Mean, variance, and standard deviation

Mean gives the arithmetic average. Variance measures squared deviations, and standard deviation is its square root. NumPy uses N − ddof in the denominator. Use ddof=1 for the usual sample variance estimate. The standard deviation itself is still a biased estimate of the population standard deviation.

import numpy as np

marks = np.array([60, 70, 80])
print(float(marks.mean()))
print(float(marks.var(ddof=1)), float(marks.std(ddof=1)))
Example output
70.0
100.0 10.0

NaN and reproducible randomness

np.nanmean ignores NaN while calculating a mean. Track how many values you exclude. For simulation, default_rng creates a Generator. A fixed seed lets you repeat a run within the same environment and call sequence.

import numpy as np

rng = np.random.default_rng(42)
sample = rng.integers(1, 7, size=4)
print(sample.shape)
print(float(np.nanmean([1.0, np.nan, 3.0])))
Example output
(4,)
2.0

Official references