22

A Cheat Sheet on Generating Random Numbers in NumPy

 4 years ago
source link: https://mc.ai/a-cheat-sheet-on-generating-random-numbers-in-numpy/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

To begin with, we’ll first import the NumPy module by running import numpy as np , allowing us to access all relevant functions in the module.

1. rand(d0, d1, ..., dn)

This method generates random numbers in a given shape. Some common examples are given below. Note that the numbers specified in the rand() function correspond to the number of dimensions of the array that is to be generated. A special case is that when no numbers are specified in the function, a random value will be generated.

Random Numbers With rand()

2. randn(d0, d1, …, dn) and standard_normal([size])

Both methods are used to generate random numbers from the standard normal distribution , the curves of which are shown in the figure below. The red curve shows you the standard normal distribution with a mean of 0 and standard deviation of 1.

Standard Normal Distribution Shown By Read Curve (Source: Wikipedia )

Both methods are to draw numbers randomly from the distribution to generate arrays of the defined shape. Their usages are about the same, except that for the standard_normal() method, we need to use a tuple to set the shape size. Some common examples are given below.

Random Numbers With randn() and standard_normal()

One thing to note is that because we know that these methods are to create a sample of numbers from the standard normal distribution, and thus we can verify if this is the case shown in the figure below.

Histogram of Random Numbers Generated With randn()

Related to these two methods, there is another method called normal([loc, scale, size]) , using which we can generate random numbers from the normal distribution specified by loc and scale parameters.

3. randint (low[, high, size, dtype])

This method generates random integers in the shape defined by size from low (inclusive) to high (exclusive) in the discrete uniform distribution. Optionally, we can also set the dtype as int64 , int , or something else, with np.int being the default value.

One thing to note is that when we don’t set the high argument, the numbers will be generated in the range of [0, low ).

Random Numbers With randint()

4. random_sample ([size]), random ([size]), ranf ([size]), and sample ([size])

All of these functions are to generate random floats in the shape defined by size in the range of [0.0, 1,0), which is a continuous uniform distribution. Using random_sample() as an example, the relevant use cases are shown below.

One thing to note that as these random numbers are drawn from the continuous uniform distribution of [0.0, 1.0), and thus the mean of these numbers is around 0.5, which is the half of the sum of 0.0 and 1.0.

Random Numbers With random_sample()

Related to these four methods, there is another method called uniform([low, high, size]) , using which we can generate random numbers from the half-open uniform distribution specified by low and high parameters.

5. choice (a[, size, replace, p])

This method generates a random sample from a given 1-D array specified by the argument a . However, if a is set as an int, the method will run as if a were an ndarray generated from np.arange(a) . Some examples using this method are shown below.

The size argument specifies the shape of the ndarray that is returned, while the p argument is a list of floats indicating the probability of the elements in a for being drawn. It should be noted that if you set the p argument, the sum of the probability values has to be 1.0.

Random Numbers With choice()

6. shuffle (x) and permutation (x)

Both methods are used to permute the sequence randomly. The major difference between them is that the shuffle() method modifies the sequence in-place and returns None , while the permutation() method generates a new ndarray of the same shape after the modification. Let’s see some examples below.

One thing to note is that when we permute a multi-dimensional array, it only works on its first axis as shown in the last two examples. In other words, the contents of the subarrays in a multi-dimensional array stay the same.

Random Numbers With shuffle() and permutation()

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK