Descriptive statistics in Python
Summarizing a sample with NumPy's stats functions.
import numpy as np
samples = np.array([12, 15, 15, 18, 21, 24, 24, 30])
average = np.mean(samples)
middle = np.median(samples)
spread = np.std(samples)
p90 = np.percentile(samples, 90)
counts, edges = np.histogram(samples, bins=4)
unique, freq = np.unique(samples, return_counts=True)
How it works
mean,median, andstddescribe the center and spread.percentilefinds the 90th percentile.histogramanduniquecount distributions.
Keywords and builtins used here
as
The run, in numbers
- Lines
- 11
- Characters to type
- 287
- Tokens
- 90
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 51 seconds.
Step 3 of 4 in Math & shapes, step 5 of 22 in NumPy.