Summary of new random number generation methods after numpy1.1.7

1. Introduction

When I was looking at the numpy official website recently, I found that version 1.17 has made some changes to the random number. Official website address: Random sampling (numpy.random)

The changes are as follows:
insert image description here
The new version of numpy maintains the compatibility of RandomState, and the new Generator is more powerful than RandomState.

insert image description here
So sometimes we go to the Internet to find blogs, but we may not be able to get the latest content.

2. Generator

Official website address: Random Generator

When I first went to the official website to find it, I also confirmed again and again that there was a new method .

1. Common functions

The functions commonly used by Generator to generate random numbers are as follows:
insert image description here

2. Examples

The first three are commonly used, and we will talk about them one by one.

2.1 Generate random integers
random.Generator.integers(low, high=None, size=None, dtype=np.int64, endpoint=False)

parameter:

  • low: int or array-like of ints, to determine the lower limit of the random number, which must be given; if high is not specified, if the parameter is low=3, then assign 3 to high, and the random number is selected in [0,3) ; If there is only one parameter, and no low or high is specified when it is given, it defaults to high, and at this time low defaults to 0;
  • high: int or array-like of ints, optional, to determine the upper limit of random numbers;
  • size: int or tuple of ints, optional, determines the size of the returned matrix, which can be one-dimensional or multi-dimensional;
  • dtype: dtype, optional, to determine the integer type;
  • endpoint: bool, optional, determine whether the upper and lower limits include high, the default is not included;

Example:

>>> import numpy as np
#初始化Generator
>>> rng=np.random.default_rng()
>>> rng.integers(low=-2,high=2,size=(2,3),endpoint=True)
array([[ 1,  0,  0],
       [ 1,  2, -1]])
>>> rng.integers(low=3,size=5)
array([0, 1, 2, 0, 0])
>>> rng.integers(3,size=(2,3))
array([[2, 0, 1],
       [1, 1, 2]])
>>> rng.integers(low=3)
0
>>> rng.integers(low=3)
2
#high是数组的情况,返回值是一个有不同上限的1*3的矩阵,
>>> rng.integers(1,[3,5,10])
array([1, 2, 8])
#low是数组的情况,指定下限
>>> rng.integers([3,5,10],11)
array([ 3, 10, 10])
>>> 
2.2 Generate random numbers
random.Generator.random(size=None, dtype=np.float64, out=None)

There are relatively few parameters, size and dtype will not be described in detail, and out is used to specify the array to store the result. random can only generate random numbers between [0,1), and other ranges have to use operations such as addition, subtraction, multiplication, and division to get the desired results.

For example, if you want a random number between [a, b), you can do the following:

(b - a) * random() + a

Example:

#产生[0,1)之间的随机数
>>> rng.random(size=4)
array([0.98110911, 0.18213644, 0.41812857, 0.19352714])
#产生[0,5)之间的随机数
>>> 5*rng.random(size=4)
array([1.07804164, 3.67840157, 3.8162768 , 2.13327948])
>>> 
2.3 Select random numbers in the existing one-dimensional array
random.Generator.choice(a, size=None, replace=True, p=None, axis=0, shuffle=True)

The parameters are as follows:

  • a: {array_like, int}, if it is an array (not limited to one-dimensional array), select it from the array elements; if it is an int integer, select it from the array generated by np.arange(a);
  • replace: bool, optional, whether there is a return sample, that is, whether there are repeated values ​​in the generated array; True indicates that there are repeated values, and False indicates that there are no repeated values;
  • p: 1-D array_like, optional, the possibility of each value in a being selected; if not given, the default is uniform distribution;
  • asix: int, optional, this is mainly for a, if a is a multi-dimensional array, which dimension is selected to select elements, if a is a 2*4 matrix, axis=0 means that each row is used as the selected element; axis= 1 indicates that each column is selected as an element;
  • shuffle: bool, optional, if it is sampling without replacement, whether to shuffle the order.

Example:

>>> rng.choice(25,size=(2,4),replace=True)
array([[ 0, 11, 18, 11],
       [ 3, 18, 20, 22]])
>>> rng.choice(25,size=(2,4),replace=False)
array([[ 1, 14,  7, 23],
       [20,  9,  6, 19]])
>>> 

Regarding the probability p, I saw such an application in a blog before, using choice to achieve a roulette-like effect. In the genetic algorithm, it is necessary to select crossover and mutation parent nodes according to fitness. Roulette is to The higher the degree of fitness, the greater the possibility of being selected, and the p parameter in the choice function just matches.

Simple example:

>>> fitness=rng.random(size=6)
#适应度
>>> fitness
array([0.31172348, 0.21873388, 0.38370583, 0.29026004, 0.39245999,
       0.33158675])
>>> p=fitness/fitness.sum()
>>> p
array([0.1616429 , 0.11342354, 0.19896904, 0.15051312, 0.20350848,
       0.17194292])
>>> result=rng.choice(a=6,size=4,replace=False)
#result保存的是下标
>>> result
array([2, 4, 5, 3])
>>> 

3、seed

Like RandomState, the seed is used to generate the same random number, which is specified when it needs to be instantiated in the Generator, and the application in this situation has not yet been encountered.

code:

>>> rng=np.random.default_rng(seed=10)
>>> rng.random()
0.9560017096289753
>>> rng=np.random.default_rng(seed=10)
>>> rng.random()
0.9560017096289753
>>> 

3. RandomState

This is a comparison of common functions under the two classes of RandomState and Generator

insert image description here
RandomState official website introduction: Legacy Random Generation

Here are a few commonly used functions:
insert image description here
the usage is similar to that of the Generator class. If you don’t want to read it in English, you can also look at rand(), randn(), randint(), random_integers(), etc. in Python’s numpy library in this blog. The use of random functions

4. Experience

There are fewer Generator functions, and I personally prefer the new version, which is supported anyway, depending on personal habits.

Guess you like

Origin blog.csdn.net/u012949658/article/details/117155881