[Numpy-discussion] Use-case for np.choose

Anne Archibald peridot.faceted at gmail.com
Sun Nov 8 23:19:46 EST 2009


2009/11/8 David Goldsmith <d.l.goldsmith at gmail.com>:
> On Sun, Nov 8, 2009 at 7:40 PM, Anne Archibald <peridot.faceted at gmail.com>
> wrote:
>>
>> As Josef said, this is not correct. I think the key point of confusion is
>> this:
>>
>> Do not pass choose two arrays.
>>
>> Pass it one array and a *list* of arrays. The fact that choices can be
>> an array is a quirk we can't change, but you should think of the
>> second argument as a list of arrays,
>
> Fine, but as you say, one *can* pass choose an array as the second argument
> and it doesn't raise an exception, so if someone is stupid/careless enough
> to pass an array for `choices`, how is choose interpreting it as a list?  Is
> the first dimension "list converted" (so that, e.g., my (2,1,2) example is
> interpreted as a two element list, each of whose elements is a (1,2) array)?

It seems to me that this is the only reasonable interpretation, yes.
After all, arrays behave like sequences along the first axis, whose
elements are arrays of one less dimension. Thus if you pass an array,
any broadcasting happens ignoring the first axis, which is a rather
abnormal pattern for numpy broadcasting, but necessary here.

As a bonus, I think this is what is implemented in current versions of
numpy. (In 1.2.1 it raises an exception if broadcasting is necessary.)

Anne



More information about the NumPy-Discussion mailing list