Numpy choose without replacement along specific dimension
<p>Without replacement I'm choosing k elements from a sample n distinct times according to a specified distribution.</p>

<p>The iterative solution is simple:</p>

<pre><code>for _ in range(n):
    np.random.choice(a, size=k, replace=False, p=p)
</code></pre>

<p>I can't set <code>size=(k, n)</code> because I would sample without replacement across samples. <code>a</code> and <code>n</code> are large, I hope for a vectorized solution.</p>

<p>So the full iterative solution is:</p>

<pre><code>In [158]: ll=[]
In [159]: for _ in range(10):
     ...:     ll.append(np.random.choice(5,3)) 
In [160]: ll
Out[160]: 
[array([3, 2, 4]),
 array([1, 1, 3]),
 array([0, 3, 1]),
 ...
 array([0, 3, 0])]
In [161]: np.array(ll)
Out[161]: 
array([[3, 2, 4],
       [1, 1, 3],
       ...
       [3, 0, 1],
       [4, 4, 2],
       [0, 3, 0]])
</code></pre>

<p>That could be cast as list comprehension: <code>np.array([np.random.choice(5,3) for _ in range(10)])</code>.</p>

<p>Or an equivalent where you <code>A=np.zeros((10,3),int)</code> and <code>A[i,:]=np.random...</code></p>

<p>In other words you want choices from <code>range(5)</code>, but want them to be unique only within rows.</p>

<p>The <code>np.random.choice</code> docs suggest an alternative:</p>

<pre><code>&gt;&gt;&gt; np.random.choice(5, 3, replace=False)
array([3,1,0])
&gt;&gt;&gt; #This is equivalent to np.random.permutation(np.arange(5))[:3]
</code></pre>

<p>I'm wondering if I can generate</p>

<pre><code>array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       ...
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
</code></pre>

<p>and permute values within rows. But with <code>permute</code> I can only shuffle all the columns together.  So I'm still stuck with iterating on rows to produce the choice without replacement.</p>

python numpy