[Numpy-discussion] Re: Newbie indexing question and print order

Lennart Ohlsson lennart.ohlsson at cs.lth.se
Wed Apr 5 08:12:20 CDT 2006


Hi,

Although I mainly use for 2D takes here is an nd-version of such a function:

def vtake(a, indices):
    """Corresponding to take in numpy but with vector valued indices"""
    indexrank = indices.shape[-1]
    flattedindex = 0
    for i in range(indexrank):
        flattedindex = flattedindex*a.shape[i] + indices[...,i]
    flattedshape = (-1,) + a.shape[indexrank:]
    return a.reshape(flattedshape).take(flattedindex)


- Lennart




On 4/5/06, Pau Gargallo<pau.gargallo at gmail.com> wrote:

hi,
i had the same problem and i defined a function with a similar sintax
to interp2 which i call take2 to solve it:

from numpy import *

def take2( a, x,y ):
        return take( ravel(a), x + y*a.shape[0] )

a = array( [[ 0.15,  0.75,  0.2 ],
                 [ 0.82,  0.5,   0.77],
                  [ 0.21,  0.91,  0.59]] )
xy = array([    [[1, 1], [1, 1], [2, 1]],
                [[2, 2], [0, 0], [1, 0]],
                [[1, 1], [0, 0], [2, 1]]] )

print take2( a, xy[...,0], xy[...,1] )

i hope this helps you.
pau




On 4/5/06, amcmorl <amcmorl at gmail.com> wrote:
> Hi all,
>
> I'm having a bit of trouble getting my head around numpy's indexing
> capabilities. A quick summary of the problem is that I want to
> lookup/index in nD from a second array of rank n+1, such that the last
> (or first, I guess) dimension contains the lookup co-ordinates for the
> value to extract from the first array. Here's a 2D (3,3) example:
>
> In [12]:print ar
> [[ 0.15  0.75  0.2 ]
>  [ 0.82  0.5   0.77]
>  [ 0.21  0.91  0.59]]
>
> In [24]:print inds
> [[[1 1]
>   [1 1]
>   [2 1]]
>
>  [[2 2]
>   [0 0]
>   [1 0]]
>
>  [[1 1]
>   [0 0]
>   [2 1]]]
>
> then somehow return the array (barring me making any row/column errors):
> In [26]: c = ar.somefancyindexingroutinehere(inds)
>
> In [26]:print c
> [[ 0.5  0.5  0.91]
>  [ 0.59 0.15 0.82]
>  [ 0.5  0.15 0.91]]
>
> i.e. c[x,y] = a[ inds[x,y,0], inds[x,y,1] ]
>
> Any suggestions? It looks like it should be relatively simple using
> 'put' or 'take' or 'fetch' or 'sit' or something like that, but I'm not
> getting it.
>
> While I'm here, can someone help me understand the rationale behind
> 'print' printing row, column (i.e. a[0,1] = 0.75 in the above example
> rather than x, y (=column, row; in which case 0.75 would be in the first
> column and second row), which seems to me to be more intuitive.
>
> I'm really enjoying getting into numpy - I can see it'll be
> simpler/faster coding than my previous environments, despite me not
> knowing my way at the moment, and that python has better opportunities
> for extensibility. So, many thanks for your great work.
> --
> Angus McMorland
> email a.mcmorland at auckland.ac.nz
> mobile +64-21-155-4906
>
> PhD Student, Neurophysiology / Multiphoton & Confocal Imaging
> Physiology, University of Auckland
> phone +64-9-3737-599 x89707
>
> Armourer, Auckland University Fencing
> Secretary, Fencing North Inc.
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting language
> that extends applications into web and mobile media. Attend the live webcast
> and join the prime developer group breaking into this new coding territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>





More information about the Numpy-discussion mailing list