typestar

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

  1. mean, median, and std describe the center and spread.
  2. percentile finds the 90th percentile.
  3. histogram and unique count distributions.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 4 in Math & shapes, step 5 of 22 in NumPy.

← Previous Next →

Descriptive statistics in other languages