[Numpy-discussion] array operation
perry at stsci.edu
Tue Jan 21 13:02:05 EST 2003
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
More information about the NumPy-Discussion