If performance is not critical you could just write your own function for a quick fix, doing something like
numpy.array([choices[j][i] for i, j in enumerate([2, 3, 1, 0])])

This 32-array limitation definitely looks weird to me though, doesn't seem to make sense.

-=- Olivier

2011/6/16 Ed Schofield <ed@pythoncharmers.com>
Hi all,

I have been investigation the limitation of the choose() method (and function) to 32 elements. This is a regression in recent versions of NumPy. I have tested choose() in the following NumPy versions:

1.0.4: fine
1.1.1: bug
1.2.1: fine
1.3.0: bug
1.4.x: bug
1.5.x: bug
1.6.x: bug
Numeric 24.3: fine

(To run the tests on versions of NumPy prior to 1.4.x I used Python 2.4.3. For the other tests I used Python 2.7.)

Here 'bug' means the choose() function has the 32-element limitation. I have been helping an organization to port a large old Numeric-using codebase to NumPy, and the choose() limitation in recent NumPy versions is throwing a spanner in the works. The codebase is currently using both NumPy and Numeric side-by-side, with Numeric only being used for its choose() function, with a few dozen lines like this:

a = numpy.array(Numeric.choose(b, c))

Here is a simple example that triggers the bug. It is a simple extension of the example from the choose() docstring:


import numpy as np

choices = [[0, 1, 2, 3], [10, 11, 12, 13],
          [20, 21, 22, 23], [30, 31, 32, 33]]

np.choose([2, 3, 1, 0], choices * 8)


A side note: the exception message (defined in core/src/multiarray/iterators.c) is also slightly inconsistent with the actual behaviour:

Traceback (most recent call last):
  File "chooser.py", line 6, in <module>
    np.choose([2, 3, 1, 0], choices * 8)
  File "/usr/lib64/python2.7/site-packages/numpy/core/fromnumeric.py", line 277, in choose
    return _wrapit(a, 'choose', choices, out=out, mode=mode)
  File "/usr/lib64/python2.7/site-packages/numpy/core/fromnumeric.py", line 37, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
ValueError: Need between 2 and (32) array objects (inclusive).

The actual behaviour is that choose() passes with 31 objects but fails with 32 objects, so this should read "exclusive" rather than "inclusive". (And why the parentheses around 32?)

Does anyone know what changed between 1.2.1 and 1.3.0 that introduced the 32-element limitation to choose(), and whether we might be able to lift this limitation again for future NumPy versions? I have a couple of days to work on a patch ... if someone can advise me how to approach this.

Best wishes,

Dr. Edward Schofield
Python Charmers
+61 (0)405 676 229

NumPy-Discussion mailing list