numpy random sampling word module

Table of contents

1 random number

2 Random sampling

3 Get the normal distribution array

4 pseudo-random numbers


The random numbers generated by computer systems are pseudo-random numbers, and NumPy is no exception, but its random sampling submodule provides many convenient functions that can meet most application needs.

1 random number

np.random.random() is the most commonly used random number generation function. The random numbers generated by this function are evenly distributed in the [0, 1) interval (left closed and right open). By default, the np.random.random() function returns a floating-point random number. This function can also accept an integer or tuple parameter to specify the structure (shape) of the returned floating-point random number array. There are also many people who are accustomed to using the np.random.rand() function to generate random numbers. Its function is the same as the np.random.random() function, except that the np.random.rand() function does not accept tuple parameters and must be written as multiple Integer parameter.

Example:

import numpy as np
print(np.random.random())
print(np.random.random(2))
print(np.random.random((2,3)))

np.random.randint() is another commonly used random number generation function. The random integers generated by this function are evenly distributed in the [low, high) interval (left closed and right open). If the low parameter is omitted, the default low value is equal to 0. The np.random.randint() function also has a default parameter size, which is used to specify the structure (shape) of the returned integer random number array. Example:

print(np.random.randint(10))
print(np.random.randint(10, size=5))
print(np.random.randint(10, size=(2,5)))
print(np.random.randint(10, 100, size=(2,5)))

2 Random sampling

Random sampling is the random selection of a specified number of elements from a specified ordered list. Random sampling is widely used, such as product sampling, lottery sorting, etc. NumPy's random sampling function is np.random.choice(), and its prototype is as follows:

#np.random.choice(a, size=None, replace=True, p=None)

Parameter a represents the entire sample to be sampled, and it only accepts integers or one-dimensional arrays (lists). If the parameter a is an integer, it is equivalent to using the array np.arange(a) as the entire sample. The parameter size is used to specify the structure (shape) of the returned sampling result array. The parameter replace is used to specify whether to allow multiple extractions of the same sample. The default is allowed. Parameter p is a weight array with the same length as the entire sample set, used to specify the probability of the corresponding sample being selected.

print(np.random.choice(1,5)) # 抽签样本只有1个元素0,抽取5次
print(np.random.choice(['a','b','c'], size=(3,5), p=[0.5,0.25,0.25])) # 指定权重
print(np.random.choice(np.arange(100), size=(2,5), replace=False)) # 不允许重复

3 Get the normal distribution array

Using the np.random.randn() function is the simplest way to generate standard normal distributed random numbers. The np.random.randn() function is used to generate random numbers from a normal distribution (standard normal distribution) with a mean of 0 and a standard deviation of 1. This function can accept one or two integer parameters to specify the structure (shape) of the returned random number array that conforms to the standard normal distribution. The example is as follows:

print(np.random.randn()) # 标准正态分布,均值为0,标准差为1
print(np.random.randn(5)) #返回数组,5个元素
print(np.random.randn(2,5))  #返回二维数组

If you need to generate non-standard normal distributed random numbers, you should use the np.random.normal() function. The np.random.normal() function generates normally distributed random numbers with mean 0 and standard deviation 1 by default. The parameter loc is used to specify the mean, the parameter scale is used to specify the standard deviation, and the parameter size is used to specify the structure (shape) of the returned random number array that conforms to the normal distribution. As can be seen from the code running results below, when the standard deviation is specified as 0.2, the data distribution is closer to the mean than using the default standard deviation.

Example:

print(np.random.normal()) # 默认均值为0,标准差为1
print(np.random.normal(loc=2, size=5)) # 参数loc指定均值为2
print(np.random.normal(loc=2, scale=0.2, size=(2,5))) # 参数loc指定均值为2,参数scale指定标准差为0.2

4 pseudo-random numbers

Random numbers in computer programs or programming languages ​​are pseudo-random numbers. Because the computer hardware is deterministic, the code is fixed, and the algorithm is accurate, no truly random numbers will be generated through these deterministic, fixed, and accurate things unless factors other than this closed system are introduced. Random number algorithms in computer systems generally use linear congruence or square-centering algorithms, generated through a seed (usually replaced by a clock). This means that if you know the seed and the random number that has been generated, it is possible to obtain information about the next random number sequence. This is the predictability of pseudo-random numbers.

The NumPy random number function uses a pseudo-random number generator internally, which requires a seed (integer) to complete initialization every time it is instantiated. If the seeds of the two initializations are the same, the random number sequence generated after each initialization will be exactly the same. The np.random.seed() function can specify the initialization seed of the pseudo-random number generator, example:

np.random.seed(12345) # 使用'12345'随机种子初始化伪随机数生成器
print(np.random.random(5))
print(np.random.random((2,3)))
np.random.seed(12345) # 再次使用'12345'随机种子初始化伪随机数生成器
print(np.random.random(5)) # 和上面完全一致
print(np.random.random((2,3))) # 和上面完全一致
#得到随机数:
[0.92961609 0.31637555 0.18391881 0.20456028 0.56772503]
[[0.5955447  0.96451452 0.6531771 ]
 [0.74890664 0.65356987 0.74771481]]
[0.92961609 0.31637555 0.18391881 0.20456028 0.56772503]
[[0.5955447  0.96451452 0.6531771 ]
 [0.74890664 0.65356987 0.74771481]]

In addition, NumPy also provides a random number generator, which can be directly operated to generate random numbers. The example is as follows:

r = np.random.RandomState(12345) # 使用随机数生成器也同样
print(r.random(5))
print(r.random((2,3)))

You can still get the same pseudo-random number as the initialization seed.

おすすめ

転載: blog.csdn.net/longhaierwd/article/details/131860142