Python generates random numbers
For more information on big data's analysis and modeling, please follow the official account "bigdatamodeling".
Random numbers are often used in the implementation of the algorithm, and sometimes we forget all kinds of random number generation methods. Here we summarize the random number generation methods in Python for future reference.
The function of import numpy as np# is the same. Each randomly generated number is the same as np.random.seed (10) np.random.RandomState (10) # normal distribution np.random.normal (loc=0.0, scale=1.0, size=None) np.random.normal () # returns scalar ~ N (0L1) np.random.normal (1) # returns scalar ~ N (0L1) np.random.normal (size= (2,2)) # returns N (0L1) np.random.normal (0L1, size= (2) ) # ditto np.random.normal (2,10, size= (2,2)) # Standard normal distribution N (0high=5 1) np.random.randn () # generate scalar np.random.randn (1) np.random.randn (2) np.random.randn (2,2) 5 * np.random.randn (2,2) + 10 samples np.random.uniform (low=1, high=5) from uniform distribution ([low, high): semi-open interval) Size= (2,2) np.random.uniform (1,5,10) np.random.uniform (1,5) # generate a scalar in [1,5) # sampling np.random.rand () # from uniform distribution ([0,1): semi-open interval) # generate scalar np.random.rand (1) np.random.rand (2,2) # # generate integer values of discrete uniform distribution on semi-open and semi-closed interval [low,high) If high=None, then the value interval becomes [0Powerlow) np.random.randint (low=1, high=5, size= (2,2)) np.random.randint (low=1, high=5, size=10) np.random.randint (1,5,10) # same as above np.random.randint (low=5, size=10) np.random.randint (1,5) # generate a scalar # in [1,5) to generate an integer value of discrete uniform distribution on the closed interval [low,high] If high=None Then the value range becomes [1jinglow] np.random.random_integers (low=1, high=5, size= (2,2)) np.random.random_integers (low=1, high=5, size=10) np.random.random_integers (1,5,10) # ditto np.random.random_integers (low=5, size=10) np.random.random_integers (1,5) # to generate [1] A scalar # np.random.random in 5] is equivalent to the random number np.random.random () # returned by np.random.random_sample# between [0score1). Return scalar np.random.random (1) np.random.random (2) np.random.random ((2,3)) # numpy.random.choice (a, size=None, replace=True, p=None) # Generates a random sample from a given 1Mel D array# select from array a If an is an integer, select # replace from np.arange (a) to represent the probability that each element in the array will be selected. Null means uniform distribution np.random.choice (5,3) np.random.choice (5,3, p = [0.1,0,0.3,0.6,0]) np.random.choice (5,3, replace=False) np.random.choice (5,3, replace=False, p = [0.1,0,0.3,0.6,0]) arr = ['pooh',' rabbit', 'piglet',' Christopher'] np.random.choice (arr, 5, p = [0.5]) 0.1, 0.1, 0.3])