np.random.rand code example

Example 1: random matrix python

np.random.rand(3,2)

Example 2: numpy generate random 2d array

import numpy as np
a=np.random.rand(3,3)
print(a)

Example 3: numpy randn with a shape of another array

a = np.zeros([2, 3])
print(a.shape)
# outputs: (2, 3)
b = np.random.randn(*a.shape)
print(b.shape)
# outputs: (2, 3)

Example 4: np.random.randn()

>>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],  #random
       [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]]) #random

Example 5: np.random.rand()

# train test split
df = pd.read_csv('file_location')
mask = np.random.rand(len(df)) < 0.8
train  = df[mask]
test = df[~mask]

Tags: