[Numpy-discussion] Indexing in Numpy vs. IDL?
Sun Nov 16 17:30:53 CST 2008
On Sun, Nov 16, 2008 at 16:15, Jason Woolard <Jason.Woolard@noaa.gov> wrote:
> hi all,
> I'm fairly new to Numpy and I've been trying to port over some IDL code
> to become more familiar. I've been moderately successful with
> numpy.where and numpy.compress to do some of things that were pretty
> easy to do in IDL. I'm a bit confused about how the indexing of arrays
> works though.
You may want to look at this section of the reference manual:
> This is pretty straightforward:
> in IDL
> data = [50.00, 100.00, 150.00, 200.00, 250.00, 300.00, 350.00]
> index = WHERE((data GT 100.00) AND (data LT 300.00))
> new_data = data[index]
> print, new_data
> 150.000 200.000 250.000
> in Python
> >>> import numpy
> >>> from numpy import *
> >>> data = [50.00, 100.00, 150.00, 200.00, 250.00, 300.00, 350.00]
> >>> data = array(data, dtype=float32) #Convert list to array
> >>> index_mask = numpy.where((data > 100.00) & (data < 300.00), 1,0)
index_mask = (data > 100.) & (data < 300.0)
> #Test for the condition.
> >>> index_one = numpy.compress(index_mask, data)
index_one = data[index_mask]
> >>> print index_one
> [ 150. 200. 250.]
Note that this is not an index; this is equivalent to new_data in your
IDL example. Are you sure you wanted to call this index_one?
> But I'm having a bit of trouble with the Python equivalent of this:
> in IDL:
> index_two = WHERE ((data[index_one] GT bottom) AND (data[index_one] LE
Well, since index_one are not indices, then data[index_one] doesn't
mean anything. Can you show us the equivalent IDL that generates
> and also this:
> result = MAX(data[index_one[index_two]])
> From what I've read it looks like numpy.take() might work to do the
> indexing. I've tried to test this but I'm not getting the answers I'd
> expect. Am I overlooking something obvious here?
So-called "advanced indexing" where the indices are boolean or integer
arrays will probably solve your problem, but we need more information
on what you mean by index_one.
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion