Numpy Distributions and Statistical Functions: Examples + Reference

Last updated:

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

Random sampling from a distribution

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

  • Draw 10 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 10 samples from an 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 10 samples from an uniform distribution

    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 10 integers from a uniform distribution (discrete) between 0 and 100

    np.random.randint(low=0, high=100, size=10)
    # array([92, 95, 57,  6, 26, 86, 55, 46, 29, 62])
    

    if you want draws without replacement, do this instead:

    # the only difference is that this will return samples from
    # 0 to 99 (100 is the size of the range)
    np.random.choice(100,10,replace=False)
    

Draw from collections

  • Draw 1 element from a given array:

    elem = np.random.choice([0,1]) 
    # elem has 0.5 chance of being 0 or 1
    
  • Draw 5 elements, without replacement:

    arr = [1,2,3,4,5,6,7,8,9]
    
    elems = np.random.choice(arr,size=5,replace=False)
    

Evaluate x on a PDF

  • Standard normal (Gaussian) distribution

    I.e. 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 CDF

  • Standard normal (Gaussian) distribution

    I.e. 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
    

See also

Dialogue & Discussion