Common Function parsing in numpy. Random Module
Numpy. Random module official documentation
1. numpy. Random. Rand (D0, D1,..., DN)
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1)
Generates a multi-dimensional array based on the given shape. Each element is in the range of [0, 1 ].
Note: tuple cannot be used to define the array shape.
import numpy as np np.random.rand(2, 3)
array([[ 0.44590044, 0.36234046, 0.51609462], [ 0.45733218, 0.80836224, 0.31628453]])
2. numpy. Random. randn (D0, D1,..., DN)
Generates an array of shape (D0, D1,..., DN), filled with random floats sampled from a univariate "normal" distribution of mean 0 and variance 1
Generates a multi-dimensional array according to the given shape. The elements in the array follow the standard normal distribution.
To generate samples that are subject to n (MU, Sigma ^ 2) distributions, use Sigma * NP. Random. randn (...) + Mu
For example, generate 2*4 samples from N (3, 6.25 ):
2.5 * np.random.randn(2, 4) + 3
array([[ 2.90478558, 6.05670578, 6.21539068, 3.3955507 ], [ 0.11594363, 3.17433693, 5.35625762, 1.4824643 ]])
3. numpy. Random. randint (low, high = none, size = none, dtype = 'l ')
Return random integers from low (random) to high (exclusive ).
Generates random integers Based on given shapes and ranges.
np.random.randint(0, 10, size=(2, 4))
array([[2, 7, 2, 1], [3, 2, 4, 1]])
4. numpy. Random. random_integers (low, high = none, size = none)
Random Integers of Type np.int between low and high, inclusive.
np.random.random_integers(1, 10, size=(2, 5))
array([[ 3, 3, 8, 4, 5], [ 2, 7, 8, 10, 2]])
5. numpy. Random. random_sample (size = none)
6. numpy. Random. Random (size = none)
7. numpy. Random. ranf (size = none)
8. numpy. Random. Sample (size = none)
Return random floats in the half-open interval [0.0, 1.0 ).
The above four methods generate floating point numbers [)
To sample UNIF [a, B), B> A multiply the output of random_sample by (B-A) and add:
(B-a) * random_sample () +
1 import numpy as np2 print(‘random_sample:\n‘, np.random.random_sample((2, 3)))3 print(‘random:\n‘, np.random.random((2, 3)))4 print(‘ranf:\n‘, np.random.ranf((2, 3)))5 print(‘sample:\n‘, np.random.sample((2, 3)))
1 random_sample: 2 [[ 0.87996593 0.2706701 0.42158973] 3 [ 0.91952234 0.99470239 0.07363656]] 4 random: 5 [[ 0.44572326 0.23595379 0.1061901 ] 6 [ 0.48362249 0.4270327 0.12281262]] 7 ranf: 8 [[ 0.07180002 0.25542854 0.55630057] 9 [ 0.38181471 0.91512916 0.04020929]]10 sample:11 [[ 0.80390231 0.0024602 0.95974309]12 [ 0.32902852 0.62796713 0.42254831]]
9. numpy. Random. Choice (A, size = none, replace = true, P = none)
Generate a random number from a given one-dimensional array
If a is an int number, all elements of the generated array are in NP. arange ().
For example, if A is a 1-D array-like, all the elements of the generated array are in.
1 print(‘1:\n‘, np.random.choice(5))2 print(‘2:\n‘, np.random.choice(5, 2, p=[0.1, 0.4, 0.3, 0.1, 0.1]))3 print(‘3:\n‘, np.random.choice(5, (2, 3)))4 print(‘4:\n‘, np.random.choice([1, 3, 4, 6], (2, 5), p=[0.1, 0.3, 0.1, 0.5]))
1 1: 2 4 3 2: 4 [1 4] 5 3: 6 [[2 1 4] 7 [0 2 3]] 8 4: 9 [[3 6 1 6 1]10 [3 3 3 3 1]]
10. numpy. Random. Seed (none)
Set the same seed, and the random number generated each time is the same. If seed is not set, different random numbers are generated each time.
1 np.random.seed(2)2 np.random.rand(2, 3)
1 array([[ 0.4359949 , 0.02592623, 0.54966248],2 [ 0.43532239, 0.4203678 , 0.33033482]])
1 np.random.seed(2)2 np.random.rand(2, 3)
1 array([[ 0.4359949 , 0.02592623, 0.54966248],2 [ 0.43532239, 0.4203678 , 0.33033482]])
1 np.random.rand(2, 3)
1 array([[ 0.20464863, 0.61927097, 0.29965467],2 [ 0.26682728, 0.62113383, 0.52914209]])
Numpy. Random module common function Parsing