Getting an item in an array with its coordinates given by another array

Hi, I'm trying to get an item in an array when I only have another array giving the position. For instance:
c = numpy.arange(0., 3*4*5).reshape((3,4,5)) c***(numpy.array((2,3), dtype=int)) [55, 56, 57, 58, 59]
I'm trying to figure what to put between c and ((2,3)). I supposed that the take method would be what I wanted but it is not... The [] operator does not work on array(but it seems to give the expected result with a tuple or a list). Is there a method for this ? Matthieu -- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92

Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]], which is even stranger. 2007/10/28, Matthieu Brucher <matthieu.brucher@gmail.com>:
Hi,
I'm trying to get an item in an array when I only have another array giving the position.
For instance:
c = numpy.arange(0., 3*4*5).reshape((3,4,5)) c***(numpy.array ((2,3), dtype=int)) [55, 56, 57, 58, 59]
I'm trying to figure what to put between c and ((2,3)). I supposed that the take method would be what I wanted but it is not... The [] operator does not work on array(but it seems to give the expected result with a tuple or a list). Is there a method for this ?
Matthieu
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92

Matthieu Brucher <matthieu.brucher <at> gmail.com> writes:
Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]], which is even stranger.
c[(2,3)] is the same as c[2,3] and obviously works as you expected. c[[2,3]] is refered to as 'advanced indexing' in the numpy book. It will return elements 2 and 3 along the first dimension. To get what you want, you need to put it like this: c[[2],[3]] In [118]: c = N.arange(0.,3*4*5).reshape((3,4,5)) In [119]: c[[2],[3]] Out[119]: array([[ 55., 56., 57., 58., 59.]]) This does not work however using a ndarray holding the indices. In [120]: ind = N.array([[2],[3]]) In [121]: c[ind] --------------------------------------------------------------------------- <type 'exceptions.IndexError'> Traceback (most recent call last) /media/hda6/home/ck/<ipython console> in <module>() <type 'exceptions.IndexError'>: index (3) out of range (0<=index<=2) in dimension 0 so you have to convert it to a list before: In [122]: c[ind.tolist()] Out[122]: array([[ 55., 56., 57., 58., 59.]]) Christian

Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]], which is even stranger.
c[(2,3)] is the same as c[2,3] and obviously works as you expected.
Well, this is not indicated in the documentation. c[[2,3]] is refered to as 'advanced indexing' in the numpy book.
It will return elements 2 and 3 along the first dimension. To get what you want, you need to put it like this:
c[[2],[3]]
In [118]: c = N.arange(0.,3*4*5).reshape((3,4,5))
In [119]: c[[2],[3]]
Out[119]: array([[ 55., 56., 57., 58., 59.]])
This is very strange to say the least, as tuple do not work in the same way. This does not work however using a ndarray holding the indices.
In [120]: ind = N.array([[2],[3]])
In [121]: c[ind]
--------------------------------------------------------------------------- <type 'exceptions.IndexError'> Traceback (most recent call last)
/media/hda6/home/ck/<ipython console> in <module>()
<type 'exceptions.IndexError'>: index (3) out of range (0<=index<=2) in dimension 0
so you have to convert it to a list before:
In [122]: c[ind.tolist()] Out[122]: array([[ 55., 56., 57., 58., 59.]])
But if I have the coordinates of the points in an array, I have to reshape it and then convert it into a list. Or convert it into a list and then convert it to a tuple. I know that advanced indexing is useful, but here it is not coherent. tuples and lists should have the same result on the array, or at least it should be documented. So there is not way to get a sub-array based on coordinates in an array ? Matthieu -- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92

Take a look at numpy.ix_ http://www.scipy.org/Numpy_Example_List_With_Doc#head-603de8bdb62d0412798c45... This method creates the index array for you. You only have to specify the coordinates in each dimesion. Bernhard On Oct 29, 8:46 am, "Matthieu Brucher" <matthieu.bruc...@gmail.com> wrote:
Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]], which is even stranger.
c[(2,3)] is the same as c[2,3] and obviously works as you expected.
Well, this is not indicated in the documentation.
c[[2,3]] is refered to as 'advanced indexing' in the numpy book.
It will return elements 2 and 3 along the first dimension. To get what you want, you need to put it like this:
c[[2],[3]]
In [118]: c = N.arange(0.,3*4*5).reshape((3,4,5))
In [119]: c[[2],[3]]
Out[119]: array([[ 55., 56., 57., 58., 59.]])
This is very strange to say the least, as tuple do not work in the same way.
This does not work however using a ndarray holding the indices.
In [120]: ind = N.array([[2],[3]])
In [121]: c[ind]
--------------------------------------------------------------------------- <type 'exceptions.IndexError'> Traceback (most recent call last)
/media/hda6/home/ck/<ipython console> in <module>()
<type 'exceptions.IndexError'>: index (3) out of range (0<=index<=2) in dimension 0
so you have to convert it to a list before:
In [122]: c[ind.tolist()] Out[122]: array([[ 55., 56., 57., 58., 59.]])
But if I have the coordinates of the points in an array, I have to reshape it and then convert it into a list. Or convert it into a list and then convert it to a tuple. I know that advanced indexing is useful, but here it is not coherent. tuples and lists should have the same result on the array, or at least it should be documented. So there is not way to get a sub-array based on coordinates in an array ?
Matthieu
-- French PhD student Website :http://miles.developpez.com/ Blogs :http://matt.eifelle.comandhttp://blog.developpez.com/?blog=92
_______________________________________________ Numpy-discussion mailing list Numpy-discuss...@scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion

Matthieu Brucher <matthieu.brucher <at> gmail.com> writes:
But if I have the coordinates of the points in an array, I have to reshape it and then convert it into a list. Or convert it into a list and then convert it to a tuple. I know that advanced indexing is useful, but here it is not coherent. tuples and lists should have the same result on the array, or at least it should be documented.
I can't give you the reasons why that behaviour was chosen. But it's simply wrong that it's not documented. Before answering you I read the corresponding chapter in the numpy book to be sure not to tell you nonsense. So go ahead and do so, too.
So there is not way to get a sub-array based on coordinates in an array?
It's just a guess, but probably you can't use an ndarray as index because indices like this [[2],[3,4]] aren't valid input for a ndarray. Christian

But if I have the coordinates of the points in an array, I have to reshape it and then convert it into a list. Or convert it into a list and then convert it to a tuple. I know that advanced indexing is useful, but here it is not coherent. tuples and lists should have the same result on the array, or at least it should be documented.
I can't give you the reasons why that behaviour was chosen. But it's simply wrong that it's not documented. Before answering you I read the corresponding chapter in the numpy book to be sure not to tell you nonsense. So go ahead and do so, too.
Thank you for this answer, I suppose we'll have to wait the answer of a numpy guru ;) Matthieu -- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92

On 10/28/07, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]], which
is even stranger.
c[(2,3)] is the same as c[2,3] and obviously works as you expected.
Well, this is not indicated in the documentation.
This is true at the Python level and is not related to numpy. "x=c[2,3]" is equivalent to "x=c.__getitem__((2,3))". Note that the index pair is passed as a tuple. On the other hand, a single index is not passed as a tuple, but is instead passed as is. For example: "x = c[a]" gets passed as "x=c.__getitem__(a)". If 'a' happens to be '(2,3)' you get the behavior above. So, although lists and arrays can be used for "fancy-indexing", tuples cannot be since you can't tell the difference between a tuple of indices and multiple indices inside square brackets. [SNIP] -- . __ . |-\ . . tim.hochberg@ieee.org

Matthieu Brucher wrote:
Hi,
I'm trying to get an item in an array when I only have another array giving the position.
For instance:
c = numpy.arange(0., 3*4*5).reshape((3,4,5)) c***(numpy.array ((2,3), dtype=int)) [55, 56, 57, 58, 59]
I'm trying to figure what to put between c and ((2,3)). I supposed that the take method would be what I wanted but it is not... The [] operator does not work on array(but it seems to give the expected result with a tuple or a list). Is there a method for this ?
What do you mean by "item" in an array? Usually, when I refer to an "item" in the array, I'm talking about getting a particular element of the array. It would appear that you are trying to get a "sub-array" or "sub-space" of the array because you are using partial indexing. Getting a particular sub-array is the easiest thing to do with NumPy and only requires something like c[2,3] Presumably, you are trying to create the numbers on the fly and so want to know what to create. The answer is that you should create a tuple of the numbers because as others have mentioned: c[2,3] is equivalent to c[(2,3)] on the interpreter level (i.e. the numpy array cannot tell the difference between the two forms of syntax). Almost all of the time a list is interpreted as "advanced indexing" (because there needed to be some way to distinguish the two). I say "almost" because there is one exception to the rule that occurs if your list has less than or equal to the same number of elements as the number of dimensions of the array *and* contains slice objects, or Ellipsis, or newaxis. In that case, the list will be interpreted as a tuple. Thus, in your case you could do c[[2,3,slice(None)]] or c[[2,3,Ellipsis]] and get your desired result. This (in my view) less than ideal behavior was created for backward compatibility to a pattern that had emerged for constructing indexing objects for multi-dimensional slicing. I'm pretty sure that numpy arrays of type integer are *always* interpreted as fancy indexing. Let me know if there is more clarification needed. The relevant code is in the function fancy_indexing_check in arrayobject.c of the source code. Best regards, -Travis O. -Travis
Matthieu
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 ------------------------------------------------------------------------
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion

What do you mean by "item" in an array? Usually, when I refer to an "item" in the array, I'm talking about getting a particular element of the array.
In fact, I have a multi-dimensional array, and the last dimension is the vector I want to get (I needed it for tests for my dense deformation field I asked about on the scipy user list). So it's not a particular element, it's a group of elements. It would appear that you are trying to get a "sub-array" or "sub-space"
of the array because you are using partial indexing.
Yes, that's what I really want to do in this case. Let me know if there is more clarification needed. The relevant code
is in the function fancy_indexing_check in arrayobject.c of the source code.
Thank you, I have my explanation, but perhaps a totuple() method could be added so that a double conversion is not needed anymore when an array is used for indexing ? Matthieu -- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92

Thank you, I have my explanation, but perhaps a totuple() method could be added so that a double conversion is not needed anymore when an array is used for indexing ?
In this case the constructor tuple(arr) should work just fine. The tolist() method is really more useful for converting a multi-dimensional array to a list of lists (instead of a list of N-1 dimensional arrays). -Travis

In this case the constructor
tuple(arr)
should work just fine.
Sorry, I didn't know it could work (shame on me I should have tested). -- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
participants (5)
-
bernhard.voigt@gmail.com
-
Christian K.
-
Matthieu Brucher
-
Timothy Hochberg
-
Travis E. Oliphant