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
You can calculate the indices with np.random.choice, limiting the number of chosen indices to the percentage:
np.random.choice
indices = np.random.choice(np.arange(myarray.size), replace=False, size=int(myarray.size * 0.2)) myarray[indices] = 0