Generate a random sample from a given one-dimensional array Examples Generate a uniform random sample from Np.arange (5) of size 3: >>> Np.random.choice (5, 3) array ([0, 3, 4]) >>> #This is equivalent to Np.random.randint (0,5,3)
Generate a non-uniform random sample from Np.arange (5) of size 3: >>> Np.random.choice (5, 3, p=[0.1, 0, 0.3, 0.6, 0]) array ([3, 3, 0])
Generate a uniform random sample from Np.arange (5) of size 3 without replacement: >>> Np.random.choice (5, 3, Replace=false) array ([3,1,0]) >>> #This is equivalent to Np.random.permutation (Np.arange (5)) [: 3]
Generate a non-uniform random sample from Np.arange (5) of size 3 without replacement: >>> Np.random.choice (5, 3, Replace=false, p=[0.1, 0, 0.3, 0.6, 0]) array ([2, 3, 0])
Any of the above can is repeated with an arbitrary array-like instead of just integers. For instance: >>> Aa_milne_arr = [' Pooh ', ' Rabbit ', ' piglet ', ' Christopher ']>>> np.random.choice (Aa_milne_arr, 5, p =[0.5, 0.1, 0.1, 0.3]) array ([' Pooh ', ' Pooh ', ' Pooh ', ' Christopher ', ' Piglet '], dtype= ' | S11 ')
|