bhylu.com

Advertisements

NumPy Statistics

A mean, median and standard deviation describe different properties of a dataset. State the statistic’s definition rather than presenting one number as a complete description.

How it works

NumPy’s standard deviation defaults to ddof=0, which treats the values as the full population for that calculation. Sample estimation commonly uses a different divisor. Quantile methods also affect results, especially for small datasets; this example names linear interpolation explicitly. Keep units and sample size beside the statistic and inspect unusual values before interpreting a summary.

Run the code

Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.

For local work, use a separate virtual environment and run python statistics.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.

Example

statistics.py

import numpy as np

values = np.array([6., 12., 18., 24.])
print(float(np.mean(values)), float(np.median(values)))
print(np.percentile(values, [25, 50, 75], method="linear").tolist())
print(np.isclose(np.std(values, ddof=0), np.sqrt(45.)))

Expected result

15.0 15.0
[10.5, 15.0, 19.5]
True

The mean and median happen to agree in this evenly spaced fixture. The quartiles use the stated method, and the spread check is based on a known variance. Agreement here does not make the measures interchangeable on a skewed dataset.

Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.