The random number function of np.random

The random number function of np.random (1)

function illustrate
rand (d0, d1, .., dn) Create an array of random numbers from d0-dn, float, [0,1), uniform distribution
randn(d0,d1,..,dn) Create an array of random numbers from d0-dn, standard normal distribution
randint(low[,high,shape]) Create random integer or integer array according to shape, the range is [low, high)
seed(s) random number seed, s is the given seed value

np.random.rand

import numpy as np

a = np.random.rand(3, 4, 5)

a
Out[3]: 
array([[[0.28576737, 0.96566496, 0.59411491, 0.47805199, 0.97454449],
        [0.15970049, 0.35184063, 0.66815684, 0.13571458, 0.41168113],
        [0.66737322, 0.91583297, 0.68033204, 0.49083857, 0.33549182],
        [0.52797439, 0.23526146, 0.39731129, 0.26576975, 0.26846021]],

       [[0.46860445, 0.84988491, 0.92614786, 0.76410349, 0.00283208],
        [0.88036955, 0.01402271, 0.59294569, 0.14080713, 0.72076521],
        [0.0537956 , 0.08118672, 0.59281986, 0.60544876, 0.77931621],
        [0.41678215, 0.24321042, 0.25167563, 0.94738625, 0.86642919]],

       [[0.36137271, 0.21672667, 0.85449629, 0.51065516, 0.16990425],
        [0.97507815, 0.78870518, 0.36101021, 0.56538782, 0.56392004],
        [0.93777677, 0.73199966, 0.97342172, 0.42147127, 0.73654324],
        [0.83139234, 0.00221262, 0.51822612, 0.60964223, 0.83029954]]])

np.random.randn

b = np.random.randn(3, 4, 5)

b
Out[5]: 
array([[[ 0.09170952, -0.36083675, -0.18189783, -0.52370155,
         -0.61183783],
        [ 1.05285606, -0.82944771, -0.93438396,  0.32229904,
         -0.85316565],
        [ 1.41103666, -0.32534111, -0.02202953,  1.02101228,
          1.59756695],
        [-0.33896372,  0.42234042,  0.14297587, -0.70335248,
          0.29436318]],

       [[ 0.73454216,  0.35412624, -1.76199508,  1.79502353,
          1.05694614],
        [-0.42403323, -0.36551581,  0.54033378, -0.04914723,
          1.15092556],
        [ 0.48814148,  1.09265266,  0.65504441, -1.04280834,
          0.70437122],
        [ 2.92946803, -1.73066859, -0.30184912,  1.04918753,
         -1.58460681]],

       [[ 1.24923498, -0.65467868, -1.30427044,  1.49415265,
          0.87520623],
        [-0.26425316, -0.89014489,  0.98409579,  1.13291179,
         -0.91343016],
        [-0.71570644,  0.81026219, -0.00906133,  0.90806035,
         -0.914998  ],
        [ 0.22115875, -0.81820313,  0.66359573, -0.1490853 ,
          0.75663096]]])

np.random.randint

c = np.random.randint(100, 200, (3, 4))

c
Out[9]: 
array([[104, 140, 161, 193],
       [134, 147, 126, 120],
       [117, 141, 162, 137]])

np.random.seed

The random seed generator makes the random number generated next time a "specific" random number determined by the seed number. If the parameter in seed is empty, the generated random number is "completely" random. References and Documentation .

np.random.seed(10)

np.random.randint(100, 200, (3 ,4))
Out[11]: 
array([[109, 115, 164, 128],
       [189, 193, 129, 108],
       [173, 100, 140, 136]])

np.random.seed(10)

np.random.randint(100 ,200, (3, 4))
Out[13]: 
array([[109, 115, 164, 128],
       [189, 193, 129, 108],
       [173, 100, 140, 136]])

The random number function of np.random (2)

function illustrate
shuffle(a) According to the first axis of the array a (that is, the outermost dimension), change the array x
permutation(a) Generate a new out-of-order array based on the first axis of array a, without changing array x
choice(a[,size,replace,p]) Extract elements from the one-dimensional array a with probability p to form a new array of size shape replace indicates whether the elements can be reused, the default is False

np.random.shuffle

a = np.random.randint(100, 200, (3, 4))

a
Out[15]: 
array([[116, 111, 154, 188],
       [162, 133, 172, 178],
       [149, 151, 154, 177]])

np.random.shuffle(a)

a
Out[17]: 
array([[116, 111, 154, 188],
       [149, 151, 154, 177],
       [162, 133, 172, 178]])

np.random.shuffle(a)

a
Out[19]: 
array([[162, 133, 172, 178],
       [116, 111, 154, 188],
       [149, 151, 154, 177]])

As you can see, a has changed, the axis.

np.random.permutation

b = np.random.randint(100, 200, (3, 4))

b
Out[21]: 
array([[113, 192, 186, 130],
       [130, 189, 112, 165],
       [131, 157, 136, 127]])

np.random.permutation(b)
Out[22]: 
array([[113, 192, 186, 130],
       [130, 189, 112, 165],
       [131, 157, 136, 127]])

b
Out[24]: 
array([[113, 192, 186, 130],
       [130, 189, 112, 165],
       [131, 157, 136, 127]])

It can be seen that b has not changed.

np.random.choice

c = np.random.randint(100, 200, (8,))

c
Out[26]: array([123, 194, 111, 128, 174, 188, 109, 115])

np.random.choice(c, (3, 2))
Out[27]: 
array([[111, 123],
       [109, 115],
       [123, 128]])#默认可以出现重复值

np.random.choice(c, (3, 2), replace=False)
Out[28]: 
array([[188, 111],
       [123, 115],
       [174, 128]])#不允许出现重复值

np.random.choice(c, (3, 2),p=c/np.sum(c))
Out[29]: 
array([[194, 188],
       [109, 111],
       [174, 109]])#指定每个值出现的概率

The random number function of np.random (3)

function illustrate
uniform(low,high,size) Generate an array with uniform distribution, low start value, high end value, size shape
normal(loc,scale,size) Generate an array with a normal distribution, loc mean, scale standard deviation, size shape
poisson(lam,size) Generate array with Poisson distribution, lam random event rate, size shape
u = np.random.uniform(0, 10, (3, 4))

u
Out[31]: 
array([[9.83020867, 4.67403279, 8.75744495, 2.96068699],
       [1.31291053, 8.42817933, 6.59036304, 5.95439605],
       [4.36353698, 3.56250327, 5.87130925, 1.49471337]])

n = np.random.normal(10, 5, (3, 4))

n
Out[33]: 
array([[ 8.17771928,  4.17423265,  3.28465058, 17.2669643 ],
       [10.00584724,  9.94039808, 13.57941572,  4.07115727],
       [ 6.81836048,  6.94593078,  3.40304302,  7.19135792]])

p = np.random.poisson(2.0, (3, 4))

p
Out[35]: 
array([[0, 2, 2, 1],
       [2, 0, 1, 3],
       [4, 2, 0, 3]])

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324611073&siteId=291194637