First I create my array
myarray = np.random.random_integers(0,10, size=20)
Then, I want to set 20% of the elements in the array to 0 (or some o
Use np.random.permutation as random index generator, and take the first 20% of the index.
np.random.permutation
myarray = np.random.random_integers(0,10, size=20) n = len(myarray) random_idx = np.random.permutation(n) frac = 20 # [%] zero_idx = random_idx[:round(n*frac/100)] myarray[zero_idx] = 0