Tutorial by Examples

# Generates 5 random numbers from a uniform distribution [0, 1) np.random.rand(5) # Out: array([ 0.4071833 , 0.069167 , 0.69742877, 0.45354268, 0.7220556 ])
Using random.seed: np.random.seed(0) np.random.rand(5) # Out: array([ 0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ]) By creating a random number generator object: prng = np.random.RandomState(0) prng.rand(5) # Out: array([ 0.5488135 , 0.71518937, 0.60276338, 0.54488318,...
# Creates a 5x5 random integer array ranging from 10 (inclusive) to 20 (inclusive) np.random.randint(10, 20, (5, 5)) ''' Out: array([[12, 14, 17, 16, 18], [18, 11, 16, 17, 17], [18, 11, 15, 19, 18], [19, 14, 13, 10, 13], [15, 10, 12, 13, 18]]...
letters = list('abcde') Select three letters randomly (with replacement - same item can be chosen multiple times): np.random.choice(letters, 3) ''' Out: array(['e', 'e', 'd'], dtype='<U1') ''' Sampling without replacement: np.random.choice(letters, 3, replace=False) ''' Out...
Draw samples from a normal (gaussian) distribution # Generate 5 random numbers from a standard normal distribution # (mean = 0, standard deviation = 1) np.random.randn(5) # Out: array([-0.84423086, 0.70564081, -0.39878617, -0.82719653, -0.4157447 ]) # This result can also be achieved with t...

Page 1 of 1