Alternative for numpy.choose that allows an arbitrary or at least more than 32 arguments?
<p>With my code I'm running into an issue where the <code>numpy.choose</code> methods does not accept all the arguments since it is limited by <code>NPY_MAXARGS</code> (<code>=32</code>). Is there an alternative available, that allows an arbitrary number of argument arrays or at least more than <code>32</code> that is as fast as <code>numpy.choose</code>?</p>

<pre><code>choices = [np.arange(0,100)]*100
selection = [0] * 100
np.choose(selection, choices)

&gt;&gt; ValueError: Need between 2 and (32) array objects (inclusive).
</code></pre>

<p>Any help would be appreciated... :)</p>

<p><code>choose</code> has the 32 object limit because it broadcasts the arrays together.  Consider the error messages for these two actions:</p>

<pre><code>In [982]: np.arange(33).choose(np.ones((33,33)))
...
ValueError: Need at least 1 and at most 32 array objects.
In [983]: np.broadcast(*range(33))
...
ValueError: Need at least 1 and at most 32 array objects.
</code></pre>

<p>An example exploiting that broadcasting, picking values from a 2d array, 1d and scalar.</p>

<pre><code>In [998]: np.diag([2,1,0]).choose((np.arange(9).reshape(3,3), 0,[.1,.2,.3]))
Out[998]: 
array([[ 0.1,  1. ,  2. ],
       [ 3. ,  0. ,  5. ],
       [ 6. ,  7. ,  8. ]])
</code></pre>

<p>As <code>@Benjamin</code> shows <code>np.choose</code> can be used to select items from the successive columns of a 2d array - provided there aren't more than 32 columns</p>

<pre><code>In [1002]: M=np.arange(9).reshape(3,3)
In [1003]: np.array([2,0,1]).choose(M)
Out[1003]: array([6, 1, 5])
In [1004]: M[[2,0,1],[0,1,2]]
Out[1004]: array([6, 1, 5])
</code></pre>

<p>It was in just such a context that I recall first seeing this 32 array limit to <code>choose</code>, and one of the few where I've seen <code>choose</code> used in an answer.</p>

<p>It is a compiled function, <code>PyArray_Choose</code> and <code>array_choose</code></p>

<p><a href="https://github.com/numpy/numpy/blob/0b2e590ec18942f8f149ab2306b80da86b04eaeb/numpy/core/src/multiarray/item_selection.c" rel="nofollow">https://github.com/numpy/numpy/blob/0b2e590ec18942f8f149ab2306b80da86b04eaeb/numpy/core/src/multiarray/item_selection.c</a></p>

<p><a href="https://github.com/numpy/numpy/blob/945c308e96fb815729e8f8aeb0ad6b39b8bdf84a/numpy/core/src/multiarray/methods.c" rel="nofollow">https://github.com/numpy/numpy/blob/945c308e96fb815729e8f8aeb0ad6b39b8bdf84a/numpy/core/src/multiarray/methods.c</a></p>

<p>I don't see any uses of this function in other compiled numpy code. And apart from testing, little use in rest of <code>numpy</code>.</p>

python numpy