擅长:python、mysql、java
<p>以下是您可以做的:</p>
<pre><code>def remove_randomly(data, percent):
np.random.shuffle(data)
roll = np.random.randint(1, 100, data.shape) # array of random integers with the same shape as data
indices = np.where(roll > percent) # indices of elements in `roll` that are greater than the percentage
test_list = data[indices]
data[indices] = 0
return indices, test_list # return indices and the values
</code></pre>
<p>请注意,<code>np.random.randint(1, 100)</code>只会生成范围<code>[1, 100)</code>内的随机整数,因此永远不会生成100%</p>