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.
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@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@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&da... _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
amcmorl 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)
You can do this with "fancy-indexing". Obviously it is going to take some time for people to get used to this idea as none of the responses yet suggest it.
But the following works.
c = ar[inds[...,0],inds[...,1]]
gives the desired effect.
Thus, your simple description c[x,y] = ar[inds[x,y,0],inds[x,y,1]] is a text-book description of what fancy-indexing does.
Best regards,
-Travis
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.
Hi again.
Thanks, everyone, for your quick replies.
Travis Oliphant wrote:
amcmorl 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)
You can do this with "fancy-indexing". Obviously it is going to take some time for people to get used to this idea as none of the responses yet suggest it. But the following works. c = ar[inds[...,0],inds[...,1]]
gives the desired effect.
Thus, your simple description c[x,y] = ar[inds[x,y,0],inds[x,y,1]] is a text-book description of what fancy-indexing does.
Great. Turns out I wasn't too far off then. I've written a quick function of my own that extends the fancy indexing to nD:
def fancy_index_nd(ar, ind): evList = ['ar['] for i in range(len(ar.shape)): evList = evList + [' ind[...,%d]' % i] if i < len(ar.shape) - 1: evList = evList + [","] evList = evList + [' ]'] return eval(''.join(evList))
1) Am I missing a simpler way to extend the fancy-indexing to n-dimensions? If not...
2) this seems (conceptually) that it might be a little faster than the routines that have to calculate a flat index. Hopefully it could be of use to people. Any thoughts?
Cheers,
Angus
you can do things like
a[ list( ind[...,i] for i in range(.shape[-1]) ) ]
if the indices could be accessed as ind[i] instead of ind[...,i] (transposing the indices array) then you could simply do:
a[ list(ind) ]
pau
On 4/7/06, Angus McMorland a.mcmorland@auckland.ac.nz wrote:
Hi again.
Thanks, everyone, for your quick replies.
Travis Oliphant wrote:
amcmorl 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)
You can do this with "fancy-indexing". Obviously it is going to take some time for people to get used to this idea as none of the responses yet suggest it. But the following works. c = ar[inds[...,0],inds[...,1]]
gives the desired effect.
Thus, your simple description c[x,y] = ar[inds[x,y,0],inds[x,y,1]] is a text-book description of what fancy-indexing does.
Great. Turns out I wasn't too far off then. I've written a quick function of my own that extends the fancy indexing to nD:
def fancy_index_nd(ar, ind): evList = ['ar['] for i in range(len(ar.shape)): evList = evList + [' ind[...,%d]' % i] if i < len(ar.shape) - 1: evList = evList + [","] evList = evList + [' ]'] return eval(''.join(evList))
- Am I missing a simpler way to extend the fancy-indexing to
n-dimensions? If not...
- this seems (conceptually) that it might be a little faster than the
routines that have to calculate a flat index. Hopefully it could be of use to people. Any thoughts?
Cheers,
Angus
Angus McMorland email a.mcmorland@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&da... _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion