内容简介:To begin with, we’ll first import the NumPy module by runningThis method generates random numbers in a given shape. Some common examples are given below. Note that the numbers specified in theBoth methods are used to generate random numbers from the standa
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.
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.
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.
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.
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
).
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.
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.
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.
以上所述就是小编给大家介绍的《A Cheat Sheet on Generating Random Numbers in NumPy》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。