"where" in python

C

Chris

I am a newby in Python and I'm first looking for equivalent to things
I already manage: IDL.
For example, I want to plot a sub-set of points, selected from a
bigger set by applying some filter. In my example, I want to select
only the values > 0.
I succeed to write 5 different ways to do this, which one is the more
efficient, the quicker, the more Python?
It seems the 4th method give wrong results, but I don't know why...
Thanks for your tips,
Christophe
------------------------------------------------------------------------------------------
import pylab as pylab
import numpy as numpy
#Read the data
d=pylab.load('Tabla_General2.cvs',comments='#',delimiter=';')
# Define the columns to plot
i_x = 10
i_y = 5

# Select the points to plot, 1rst method
b_where = (d[:,i_x]>0) & (d[:,i_y]>0)
xb = d[b_where,i_x]
yb = d[b_where,i_y]

# 2nd method
xb2=pylab.compress(b_where,d[:,i_x])
yb2=pylab.compress(b_where,d[:,i_y])

# 3rd method
i_where = pylab.where((d[:,i_x]>0) & (d[:,i_y]>0),1,0)
xi = d[i_where,i_x]
yi = d[i_where,i_y]

# 4th method
xi2=pylab.compress(i_where,d[:,i_x])
yi2=pylab.compress(i_where,d[:,i_y])

#5th method
in_where = numpy.transpose(numpy.where((d[:,i_x]>0) & (d[:,i_y]>0)))
xin = d[in_where,i_x]
yin = d[in_where,i_y]
------------------------------------------------------------------------------
 
R

Robert Kern

I am a newby in Python and I'm first looking for equivalent to things
I already manage: IDL.
For example, I want to plot a sub-set of points, selected from a
bigger set by applying some filter. In my example, I want to select
only the values> 0.
I succeed to write 5 different ways to do this, which one is the more
efficient, the quicker, the more Python?

You will want to ask numpy questions on the numpy mailing list.

http://www.scipy.org/Mailing_Lists

The 1st method. The rest just do a lot of extra work or use old APIs.
It seems the 4th method give wrong results, but I don't know why...
Thanks for your tips,
Christophe

You don't need to use "as" unless if you are renaming the modules.

--
Robert Kern

"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top