
I am trying to see if I can use where() or choose() to do this. I can't really figure it out. I have a 2-d array data where each row is an observation and each column is an attribute of the observation: data = [[.3, .2, 2.3,...] <- observation 1 [.7, 1.2, .4...] <- observation 2 ...]] I have another 1-d array that contains a code for the class of object: class = [0,1,0,1,1,3,2,0,...] where class[i] = the class of the ith object in the data array. Thus, observation 1 above is class 0, observation 2 is class 1, and so on. I want to select all objects of a given class from data array. I can do this with a loop for i in range(ndat): if class == 0: do something .... Is there a way to use where() or choose() to do this? Would it be more efficient? Thanks, Dave

Dave Bazell writes:
I am trying to see if I can use where() or choose() to do this. I can't really figure it out.
I have a 2-d array data where each row is an observation and each column is an attribute of the observation:
data = [[.3, .2, 2.3,...] <- observation 1 [.7, 1.2, .4...] <- observation 2 ...]]
I have another 1-d array that contains a code for the class of object:
class = [0,1,0,1,1,3,2,0,...]
Note that using class is illegal, it is a reserved keyword.
where class[i] = the class of the ith object in the data array. Thus, observation 1 above is class 0, observation 2 is class 1, and so on.
I want to select all objects of a given class from data array. I can do this with a loop
I assume you mean you want to select all the rows corresponding to all the observations where the code for the class corresponding to that observation equals some particular value. If so then for numarray this ought to work. index = nonzero(code==1) # want indices of all the obs where class code = 1 selected_obs = data[index] (or in one line if you wish: selected_obs = data[nonzero(code==1)] )
for i in range(ndat): if class == 0: do something ....
Is there a way to use where() or choose() to do this? Would it be more efficient?
Perry

Perry Greenfield wrote:
If so then for numarray this ought to work.
index = nonzero(code==1) # want indices of all the obs where class code = 1 selected_obs = data[index]
of for Numeric, use take(): selected_obs = take(data,nonzero(code == 1),1) (this will select columns coresponding to where the code == 1, which is how I read your question) By the way, choose() and where() do something similar, but give you an array back that is the saem size as the one you start with, with some (or all) of the elements replaced. take() gives you a smaller array that is a subset of the original one, which I think is what you want here. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (3)
-
Chris Barker
-
Dave Bazell
-
Perry Greenfield