numpy random sample from array code example

Example 1: random array numpy

>>> np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  
       [ 0.37601032,  0.25528411],  
       [ 0.49313049,  0.94909878]]) 

>>> np.random.random_integers(low=0, high=9, size=(3,2))
array([[2, 3],
       [2, 3],
       [6, 3]])

Example 2: python select random subset from numpy array

fruits = ['apple', 'banana', 'orange', 'grape']
subset_size = int(0.7 * len(fruits))
np.random.choice(fruits, subset_size, replace=False)
# array(['grape', 'banana'], dtype='<U6')

Example 3: numpy random choice

>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])