Numpy Distributions and Statistical Functions: Examples + Reference

Last updated:
Table of Contents

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

Generate random number from 0 up to N

All integers from 0 (inclusive) to N-1 have equal probability

Template: np.random.randint(0, N)

import numpy as np

np.random.randint(0,100)
# 56

Generate random number in interval

Template: np.random.randint(A, B)

Again, all integers from A (inclusive) up to B-1 have equal probability

import numpy as np

np.random.randint(10,15)
# 12

Generate random number between 0 and 1

Generate a random number (real) from 0 (including) up to 0.99999... (depending on precision).

All real numbers from 0 to 0.99999... are generated with equal probability.

import numpy as np

# this is equivalent to np.random.uniform(low=0,high=1)
np.random.sample()
# 0.30220482

Sample from standard normal distribution

import numpy as np

# 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])

Sample from arbitrary normal distribution

import numpy as np

# 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])

Sample from uniform distribution (continuous)

import numpy as np

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])

Sample from uniform distribution (discrete)

import numpy as np

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

Sample from lists/arrays, without replacement

To enable replacement, use replace=True

import numpy as np

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

np.random.choice(lst,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)

import scipy.stats as st

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?

import scipy.stats as st

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

import numpy as np

# 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

start=-3
stop=2

# num_elements is calculated using
# the range (start,stop) you chose above
num_elements = 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