Numpy Distributions and Statistical Functions: Examples + Reference

Last updated:
Table of Contents

imports: all examples assume you have the following at the top of your script: import numpy as np and import scipy.stats as st

HEADS-UP: In general, leave out the size= parameter if you just want a sample with a single element

Draw random samples from a standard normal distribution

# mean at zero and standard deviation of 1
np.random.randn(10)
# array([ 0.32929561, -0.5814358 ,  0.74105779,  0.06120152,  1.02753804,
#   0.2814268 ,  0.76394055, -0.13202086, -0.99976205, -0.51692293])

Draw random samples from arbitrary normal distribution

# an array of 10 points randomly sampled from a normal distribution
# loc=mean, scale=std deviation
np.random.normal(loc=0.0, scale=1.0, size=10)
# array([ 0.57258901,  2.25547575,  0.65749017, -0.04182533,  0.55000601,
#       -1.15594624,  0.32455692,  0.16460812,  0.70899117, -0.95861313])

Draw random samples from uniform distribution (continuous)

np.random.uniform(low=0,high=1,size=10)
# array([ 0.21310048,  0.28180847,  0.58721479,  0.8013283 ,  0.33171448,
#       0.98888729,  0.4519467 ,  0.93362951,  0.64370449,  0.13997242])

Draw samples from uniform distribution (discrete)

np.random.choice(10,size=10,replace=True)
# array([2, 8, 2, 7, 7, 9, 1, 7, 9, 5])

To draw without replacement:

np.random.choice(10,size=10,replace=False)
# array([0, 3, 9, 5, 1, 4, 8, 6, 2, 7])

Draw 1 element from collection

elem = np.random.choice([0,1]) 
# elem has 0.5 chance of being 0 or 1

Draw random elements from collection, without replacement

arr = [1,2,3,4,5,6,7,8,9]

np.random.choice(arr,size=5,replace=False)
# array([8, 2, 7, 5, 1])

Evaluate x on a gaussian PDF

In other words, P(x) | P ~ Gaussian(0,1)

dist = st.norm(loc=0.0,scale=1.0)
dist.pdf(1.645)
# 0.10311081109198142

Evaluate x on a Gaussian CDF

In other words, what percentage of the density is to the left of x?

dist = st.norm(loc=0.0,scale=1.0)
dist.cdf(1.645)
# 0.95001509446087862

linspace VS arange

Use linspace if you care about the number of elements, use arange if you care about the step size

Generate N samples, evenly spaced

In this case generate 11 samples evenly spaced from 0.0 to 1.0

# template is: linspace(start, stop, num=50)

x = np.linspace(0.0,1.0,11)
# array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ])

Generate samples, evenly spaced with step size

template: arange(start, stop, step)

import numpy as np

# template is: arange(start, stop, step)

x = np.arange(0.0,10.0,3)
# array([ 0.,  3.,  6.,  9.])

Generate samples in logarithmic scale

template: logspace(start, stop, num=50)

Generate numbers in logarithmic scale: 0.001, 0.01, 0.1, 1.0, 10.0, etc.

import numpy as np

# you probably want it to be evenly spaced so $num
# depends on the range
start=-3
stop=2
num = np.abs([start,stop]).sum() + 1

x = np.logspace(start,stop,num)
# array([  1.00000000e-03,   1.00000000e-02,   1.00000000e-01,
#        1.00000000e+00,   1.00000000e+01,   1.00000000e+02])

References

Dialogue & Discussion