Numpy/Scipy Distributions and Statistical Operations: Examples & Reference

# Numpy/Scipy Distributions and Statistical Operations: Examples & Reference

Last updated:
Table of Contents

For ways to sample from lists and distributions: Numpy sampling: Reference and Examples

## Generate random int 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

# generate a single int from 0 to 100 (exclusive)
np.random.randint(0,100)
# >>> 56

# generate 5 random ints from 0 to 100 (exclusive)
np.random.randint(0,100, size=5)
# >>> [29,52,89,9,22,3]


## Generate random int 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

# generate single int from 10 to 15 (exclusive)
np.random.randint(10,15)
# 12

# generate 5 random ints from 10 to 15 (exclusive)
np.random.randint(10,15, size=5)
# >>> [10,13,12,13,14]


## Generate normal distribution

Template: st.norm(loc=<mean>, scale=<standard_deviation>)

Generate samples from a normal (gaussian) distribution using .rvs(<num_samples>)

import scipy.stats as st

dist = st.norm(loc=0.0,scale=1.0)
dist.rvs(10)


## Generate random float between 0 and 1

Generate a random number (float) 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


## 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 np.linspace if you care about the number of elements, use np.arange if you care about the step size

np.linspace np.arange
Set the number of elements you want
(regardless of the step size)
Set the step size you want
(regardless of the number of elements)

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