Using Numpy's random.choice to randomly remove item from list
<p>According to the <a href="http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html" rel="nofollow">http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html</a>, using the <code>replace = False</code> with Numpy's <code>random.choice</code> method should make the sample without replacement. However, this does not seem to work for me:</p>

<pre><code>In [33]: import numpy as np

In [34]: arr = range(5)

In [35]: number = np.random.choice(arr, replace = False)

In [36]: arr
Out[36]: [0, 1, 2, 3, 4]
</code></pre>

<p>The array <code>arr</code> is still <code>range(5)</code> after sampling, and not missing a (random) number as I would expect. How could I sample a number from <code>range(5)</code> without replacement? </p>

<p>As mentioned in one of the comments, np.choice selects with or without replacement, a series of numbers from a sequence. But it does not modify the sequence. </p>

<p><strong>Easy alternative</strong></p>

<pre><code>arr = range(5)
# numbers below will never contain repeated numbers (replace=False)
numbers = np.random.choice(arr, 3, replace=False) 
</code></pre>

<p>The behaviour I think you want would be:</p>

<pre><code>arr = range(5)
all_but_one = np.random.choice(arr, len(arr) -1, replace=False)
</code></pre>

<p>so you would select N-1 numbers without replacement (to avoid repetitions), effectively removing a random element from the iterable. </p>

<p><strong>More efficient alternative</strong> </p>

<pre><code>arr = range(5)
random_index = np.random.randint(0, len(arr))
arr.pop(random_index)
</code></pre>

python numpy random