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

josef.pktd@gmai... josef.pktd@gmai...
Mon Nov 9 18:59:29 CST 2009

```On Mon, Nov 9, 2009 at 7:54 PM, David Goldsmith <d.l.goldsmith@gmail.com> wrote:
> May I infer from the sudden silence that I finally have it?

I think so,
I assume that the result of broadcasting is unique, I haven't seen an
example yet where broadcasting would depend on the sequence in which
it is done.

Josef

>
> DG
>
> On Sun, Nov 8, 2009 at 8:50 PM, David Goldsmith <d.l.goldsmith@gmail.com>
> wrote:
>>
>> OK, let me see if I'm interpreting this example correctly:
>>
>> >>> c1=np.arange(2).reshape(2,1,1); c1
>> array([[[0]],
>>
>>        [[1]]])
>> >>> c2=2+np.arange(2).reshape(1,1,2); c2
>> array([[[2, 3]]])
>> >>> a=np.eye(2,dtype=int)
>> >>> np.choose(a, [c1, c2])
>> array([[[2, 0],
>>         [0, 3]],
>>
>>        [[2, 1],
>>         [1, 3]]])
>>
>> First, everything is being broadcast to (2,2,2); a is broadcast to
>> [[[1,0], [0,1]], [[1,0], [0,1]]], c1 is broadcast to [[[0,0], [0,0]],
>> [[1,1], [1,1]]] and c2 is broadcast to [[[2,3], [2,3]], [[2,3], [2,3]]].
>> Now result is created by "stepping through" broadcast a and using,
>> respectively, the positionally corresponding element from broadcast c1
>> (resp. c2) if the value in a at the position is 0 (resp. 1).  At least, this
>> gives the result above (but I have not examined other possible broadcasts of
>> the arguments to see if they would also give the result - I conjectured what
>> appeared to me to be the most "natural" broadcasts and checked to see if it
>> worked and it does; is there something I should know - e.g., uniqueness of
>> the result, or a rule governing how choose broadcasts - to *know* that the
>> broadcasts above are indeed the broadcasts choose is using?)
>>
>> Thanks again,
>>
>> DG
>> On Sun, Nov 8, 2009 at 8:19 PM, Anne Archibald <peridot.faceted@gmail.com>
>> wrote:
>>>
>>> 2009/11/8 David Goldsmith <d.l.goldsmith@gmail.com>:
>>> > On Sun, Nov 8, 2009 at 7:40 PM, Anne Archibald
>>> > <peridot.faceted@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
>>> _______________________________________________
>>> NumPy-Discussion mailing list
>>> NumPy-Discussion@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
```

More information about the NumPy-Discussion mailing list