[Numpy-discussion] argsort and take along arbitrary axes

Charles R Harris charlesr.harris at gmail.com
Mon May 14 14:32:47 EDT 2007


On 5/14/07, Zachary Pincus <zpincus at stanford.edu> wrote:
>
> >> I've got a few questions that came up as I tried to calculate various
> >> statistics about an image time-series. For example, I have an array
> >> of shape (t,x,y) representing t frames of a time-lapse of resolution
> >> (x,y).
> >>
> >> Now, say I want to both argsort and sort this time-series, pixel-
> >> wise. (For example.)
> >>
> >> In 1-d it's easy:
> >> indices = a.argsort()
> >> sorted = a[indices]
> >>
> >> I would have thought that doing this on my 3-d array would work
> >> similarly:
> >> indices = a.argsort(axis=0)
> >> sorted = a.take(indices, axis=0)
> >>
> >> Unfortunately, this gives a ValueError of "dimensions too large."
> >> Now, I know that 'a.sort(axis=0)' works fine for the given example,
> >> but I'm curious about how to this sort of indexing operation in the
> >> general case.
> >
> > Unfortunately, argsort doesn't work transparently with take or
> > fancy indexing for multidimensional arrays. I am thinking of adding
> > a function argtake for this, and also for the results returned by
> > argmax and argmin, but at the moment you have to fill in the
> > values of the other indices and use fancy indexing. For now, it is
> > probably simpler, prettier, and faster to just sort the array.
>
> Thanks Charles. Unfortunately, the argsort/sort buisness was, as I
> mentioned, just an example of the kind of 'take' operation that I am
> trying to figure out how to do. There are other operations that will
> have similarly-formatted 'indices' arrays (as above) that aren't
> generated from argsort...
>
> As such, how do I "fill in the values of the other indices and use
> fancy indexing"? Even after reading the numpy book about that, and
> reading the docstring for numpy.take, I'm still vague on this. Would
> I use numpy.indices to get a list of index arrays, and then swap in
> (at the correct position in this list) the result of argsort (or the
> other operations), and use that for fancy indexing? Is there an
> easier/faster way?


I've attached  a quick, mostly untested, version of argtake. It's in Python,
probably not too fast, but see if it works for you.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070514/715b8ece/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: argtake.py
Type: text/x-python
Size: 1361 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070514/715b8ece/attachment.py>


More information about the NumPy-Discussion mailing list