Share on:

December 29, 2019

1. Introduction
2. NumPy Arrays
2.1 What’s A NumPy Array
2.2 Creating NumPy Arrays
2.3 Indexing And Modifying 1-D Arrays
2.4 Indexing And Modifying Multidimensional Arrays
2.5 Basic Math
3. Intermediate Array Stuff
3.2 newaxis
3.3 reshape
3.4 boolean indexing
3.5 nan
3.6 infinity
3.7 random
4. Common Operations
4.1 where
4.2 Math Funcs
4.3 all and any
4.4 concatenate
4.5 Stacking
4.6 Sorting
4.7 unique
5. Challenges

This video covers the core functionality in NumPy’s random module. This includes random number generation, sampling from distributions, shuffling arrays and sampling from arrays with and without replacement.

# Code

import numpy as np

# simulate rolling a 6-sided die 3 times
np.random.randint(low=1, high=7, size=3)  # [1, 3, 2]

# set a random seed to get reproducible results
np.random.seed(2357)
np.random.randint(low=1, high=7, size=3)  # [2, 4, 1]
np.random.randint(low=1, high=7, size=3)  # [3, 1, 6]
np.random.seed(2357)
np.random.randint(low=1, high=7, size=3)  # [2, 4, 1]
np.random.randint(low=1, high=7, size=3)  # [3, 1, 6]

# draw three values between 1 and 6 without relpacement?
np.random.seed(2357)
np.random.choice(
a = np.arange(1, 7),
size = 3,
replace = False,
p = None
)  # [6, 5, 1]

# draw three values between 1 and 6 with probabilities
np.random.choice(
a = np.arange(1, 7),
size = 3,
replace = False,
p = np.array([0.1, 0.1, 0.1, 0.1, 0.3, 0.3])
)  # [5, 2, 6]

# draw three values from an array of strings
np.random.choice(
a = np.array(['you', 'can', 'use', 'strings', 'too']),
size = 3,
replace = False,
p = None
)  # ['use', 'you', 'can']

## Sample rows from a 2d array
foo = np.array([
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]
])

# with replacement
np.random.seed(1234)
rand_rows = np.random.randint(
low=0,
high=foo.shape[0],
size=3
)  # [3, 4, 4]
foo[rand_rows]

# without replacement
rand_rows = np.random.choice(
a=np.arange(start=0, stop=foo.shape[0]),
replace=False,
size=3
)  # [4, 2, 3]
foo[rand_rows]

# row-wise shuffle
np.random.permutation(foo)

## uniform() will give you values from a uniform distribution
np.random.uniform(low = 1.0, high = 2.0, size = (2, 2))

## normal() from a normal distribution
np.random.normal(loc = 0.0, scale = 1.0, size = 2)

## binomial() from a binomial distribution
np.random.binomial(n = 10, p = 0.25, size = (3, 2))

## If you want to see more distributions, check out
## https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.random.html

# Transcript

In this section, we’ll see how you can use numpy’s random module to shuffle arrays, sample values from arrays, and draw values from a host of probability distributions.
Let’s see an example of how you might simulate rolling a 6-sided die 3 times. In other words, we want to draw three integers from the range 1 to 6, with replacement. For this we can use the randint() function from numpy’s random module.
If you try running this on your machine, you’ll probably get something different. However, we can get reproducible results by setting a random number seed immediately before we generate random numbers.
To set a seed, use the seed() function with your favorite value passed in. Try this example on your machine and you should get the same result.

Now, what if we wanted to draw three values between 1 and 6 without replacement? For this we can use the choice() function, giving it

• a 1d array of values to choose from,
• the number of samples we want to draw,
• whether values should be replaced which is False by default,
• and a 1d array of probabilities corresponding to our 1d array of options, which by default gives equal probability to each option.

choice() is like a more generic version of randint(). Let’s see some examples. First we’ll draw 3 ints between 1 and 6 without replacement.
Next we’ll do the same thing, but we’ll give a probability to each element.
Lastly we’ll draw 3 elements from an array of strings simply to show that choice() can work on all types of numpy arrays.

Now, what if you wanted to sample the rows from a 5x2 array like this one?
You can use randint() and choice() for that too. The trick is to generate a random 1d array of row indices and use that result to select rows from the array you wanted to sample.
With this technique, you can easily sample rows with replacement or without replacement.
You can also use this technique to shuffle an array, but numpy makes this even easier with its permutation() function.

Numpy lets you sample values from a variety of probability distributions. We won’t cover all of them here because, ain’t nobody got time for that, but let’s look some common ones you should be familiar with.
uniform() will give you values from a uniform distribution normal() from a normal distribution.
binomial() from a binomial distribution.
There are a bunch more covered in the numpy docs.