9951 explained code solutions for 126 technologies


python-numpyGet random sample from given array using choice()


import numpy as np
a = np.array([1,2,3,6,7,8])
sample = np.random.choice(a, 3)ctrl + c
import numpy as np

load Numpy module for Python

np.array

declare Numpy array

[1,2,3,6,7,8]

array to get random sample from

.random.choice

returns random array from given array of a given size

a, 3

return array from a array of 3 elements long

sample

will contain random sample from array