Numpy Sampling: Reference and Examples

Numpy Sampling: Reference and Examples

Last updated:
Table of Contents

Numpy version: 1.18.2

For other examples on how to use statistical function in Python: Numpy/Scipy Distributions and Statistical Functions Examples

Sample from list

Use np.random.choice(<list>, <num-samples>):

Example: take 2 samples from names list

To enable replacement, use replace=True

import numpy as np

names = ["alice", "bob", "charlie", "david"]

np.random.choice(names,size=2,replace=False)
# array(['charlie', 'alice'], dtype='<U7')

Weighted sample from list

Use parameter p=[<probability-array>] having one probability for each list element.

Example: take 3 samples from names list, with some elements having higher probability than others:

import numpy as np

names = ["alice", "bob", "charlie", "david", "edward"]

# 40% of chance for alice, 20% each for bob, charlie and david
np.random.choice(names, size=3, replace=True, p=[0.4, 0.2, 0.2, 0.2, 0.0])
# >>> array(['charlie', 'alice', 'alice'], dtype='<U7')

Example: take 5 samples from array fruits

Sample from normal distribution

Use np.random.normal()

Example: take 5 samples from a standard normal distribution (mean = 0, standard deviation = 1)

import numpy as np

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

Sample number (integer) from range

Use np.random.uniform(<max_num>, <sample_size>) like when sampling from a uniform distribution:

Example:: sample 2 integers from a uniform distribution ranging from 0 to 99

import numpy as np

np.random.choice(100, size=2)
# array([1, 44])

Sample number (float) from range

Use np.random.uniform(low=<min-num>, high=<max-num>, size=<sample-size>) like when sampling from a uniform distribution.

Example: sample 10 floats from -5 to 5

import numpy as np

np.random.uniform(low=-5,high=5,size=10)
# >>> array([-1.52197428e+00,  5.08109762e-01,  2.96426560e+00,  3.17985813e+00,
#       -4.54695283e+00, -4.33826344e-01,  1.74998558e-03, -1.91757002e+00,
#        4.67157356e+00, -4.38634184e+00])

Sample from uniform distribution (discrete)

Use np.random.choice(<max_num>, <sample_size>)

Example:: sample 5 integers from a uniform distribution ranging from 0 to 9

import numpy as np

np.random.choice(10,size=5)
# array([5, 3, 0, 6, 8])

Sample from uniform distribution (continuous)

Use np.random.uniform

Example: draw 10 samples from a standard uniform distribution (between 0 and 1)

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