[Numpy-discussion] np.choose() question

Mark Miller markperrymiller@gmail....
Tue Jun 8 12:23:22 CDT 2010


Not pretty, but it works:

>>> idx
array([[4, 2],
            [3, 1]])
>>> times
array([100, 101, 102, 103, 104])
>>> numpy.reshape(times[idx.flatten()],idx.shape)
array([[104, 102],
            [103, 101]])
>>>



On Tue, Jun 8, 2010 at 10:09 AM, Gökhan Sever <gokhansever@gmail.com> wrote:
>
>
> On Tue, Jun 8, 2010 at 11:24 AM, Andreas Hilboll <lists@hilboll.de> wrote:
>>
>> Hi there,
>>
>> I have a problem, which I'm sure can somehow be solved using np.choose()
>> - but I cannot figure out how :(
>>
>> I have an array idx, which holds int values and has a 2d shape. All
>> values inside idx are 0 <= idx < n. And I have a second array times,
>> which is 1d, with times.shape = (n,).
>>
>> Out of these two arrays I now want to create a 2d array having the same
>> shape as idx, and holding the values contained in times, as indexed by
>> idx.
>>
>> A simple np.choose(idx,times) does not work (error "Need between 2 and
>> (32) array objects (inclusive).").
>>
>> Example:
>>
>> idx = [[4,2],[3,1]]
>> times = [100,101,102,103,104]
>>
>>  From these two I want to create an array
>>
>> result = [[104,102],[103,101]]
>>
>> How can this be done?
>>
>> Thanks a lot for your insight!
>>
>> Andreas
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
> Here is a non numpy.choose solution:
> newtimes = [times[idx[x][y]] for x in range(2) for y in range(2)]
> np.array(newtimes).reshape(2,2)
> array([[104, 102],
>            [103, 101]])
>
> --
> Gökhan
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>


More information about the NumPy-Discussion mailing list