Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-06 Thread Martin Spacek
On 2010-08-06 13:11, Martin Spacek wrote: > Josef, I'd forgotten you could use None to increase the dimensionality of an > array. Neat. And, somehow, it's almost twice as fast as the Cython version!: > > >>> timeit a[np.arange(a.shape[0])[:, None], i] > 10 loops, best of 3: 5.76 us per loop

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-06 Thread Martin Spacek
On 2010-08-06 06:57, Keith Goodman wrote: > You can speed it up by getting rid of two copies: > > idx = np.arange(a.shape[0]) > idx *= a.shape[1] > idx += i Keith, you're right of course. I'd forgotten about your earlier suggestion about operating in-place. Here's my new version: def rowta

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-06 Thread Keith Goodman
On Fri, Aug 6, 2010 at 3:01 AM, Martin Spacek wrote: > Keith Goodman wrote: >  > Here's one way: >  > >  >>> a.flat[i + a.shape[1] * np.arange(a.shape[0])] >  >     array([0, 3, 5, 6, 9]) > > > I'm afraid I made my example a little too simple. In retrospect, what I really > want is to be able to u

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-06 Thread josef . pktd
On Fri, Aug 6, 2010 at 6:01 AM, Martin Spacek wrote: > Keith Goodman wrote: >  > Here's one way: >  > >  >>> a.flat[i + a.shape[1] * np.arange(a.shape[0])] >  >     array([0, 3, 5, 6, 9]) > > > I'm afraid I made my example a little too simple. In retrospect, what I really > want is to be able to u

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-06 Thread Martin Spacek
Keith Goodman wrote: > Here's one way: > >>> a.flat[i + a.shape[1] * np.arange(a.shape[0])] > array([0, 3, 5, 6, 9]) I'm afraid I made my example a little too simple. In retrospect, what I really want is to be able to use a 2D index array "i", like this: >>> a = np.array([[ 0, 1, 2,

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread John Salvatier
choose might be slower if you weren't doing an "arange(N)" each time. On Thu, Aug 5, 2010 at 1:51 PM, Keith Goodman wrote: > On Thu, Aug 5, 2010 at 1:32 PM, wrote: > > On Thu, Aug 5, 2010 at 4:07 PM, Martin Spacek > wrote: > >> josef.pkt wrote: > > a = np.array([[0, 1], > >>

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread Keith Goodman
On Thu, Aug 5, 2010 at 1:32 PM, wrote: > On Thu, Aug 5, 2010 at 4:07 PM, Martin Spacek wrote: >> josef.pkt wrote: > a = np.array([[0, 1], >>                   [2, 3], >>                   [4, 5], >>                   [6, 7], >>                   [8, 9]]) > i = np.array([0, 1, 1, 0, 1]) >

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread Bruce Southey
On 08/05/2010 03:07 PM, Martin Spacek wrote: > josef.pkt wrote: a = np.array([[0, 1], > [2, 3], > [4, 5], > [6, 7], > [8, 9]]) i = np.array([0, 1, 1, 0, 1]) a[range(a.shape[0]), i] > array([0, 3, 5, 6,

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread josef . pktd
On Thu, Aug 5, 2010 at 4:07 PM, Martin Spacek wrote: > josef.pkt wrote: a = np.array([[0, 1], >                   [2, 3], >                   [4, 5], >                   [6, 7], >                   [8, 9]]) i = np.array([0, 1, 1, 0, 1]) a[range(a.shape[0]), i] > array([0, 3, 5, 6, 9

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread Martin Spacek
josef.pkt wrote: >>> a = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]) >>> i = np.array([0, 1, 1, 0, 1]) >>> a[range(a.shape[0]), i] array([0, 3, 5, 6, 9]) >>> a[np.arange(a.shape[0]), i] array([0, 3, 5, 6, 9]) Thank

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread John Salvatier
You may also use the choose function: http://docs.scipy.org/doc/numpy/reference/generated/numpy.choose.html choose(i, (a[:,0], a[:,1]) On Thu, Aug 5, 2010 at 10:31 AM, Keith Goodman wrote: > On Thu, Aug 5, 2010 at 10:26 AM, wrote: > > On Thu, Aug 5, 2010 at 1:12 PM, Martin Spacek > wrote: >

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread Keith Goodman
On Thu, Aug 5, 2010 at 10:26 AM, wrote: > On Thu, Aug 5, 2010 at 1:12 PM, Martin Spacek wrote: >> I want to take an n x m array "a" and index into it using an integer index >> array >> "i" of length n that will pull out the value at the designated column from >> each >> corresponding row of "a

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread Keith Goodman
On Thu, Aug 5, 2010 at 10:12 AM, Martin Spacek wrote: > I want to take an n x m array "a" and index into it using an integer index > array > "i" of length n that will pull out the value at the designated column from > each > corresponding row of "a". > a = np.arange(10) a.shape = 5, 2

Re: [Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread josef . pktd
On Thu, Aug 5, 2010 at 1:12 PM, Martin Spacek wrote: > I want to take an n x m array "a" and index into it using an integer index > array > "i" of length n that will pull out the value at the designated column from > each > corresponding row of "a". > a = np.arange(10) a.shape = 5, 2 >

[Numpy-discussion] use index array of len n to select columns of n x m array

2010-08-05 Thread Martin Spacek
I want to take an n x m array "a" and index into it using an integer index array "i" of length n that will pull out the value at the designated column from each corresponding row of "a". >>> a = np.arange(10) >>> a.shape = 5, 2 >>> a array([[0, 1], [2, 3], [4, 5], [6, 7]