[Numpy-discussion] filters for rows or columns

Robert Kern robert.kern@gmail....
Tue Aug 25 13:13:59 CDT 2009


On Tue, Aug 25, 2009 at 11:07, Giuseppe Aprea<giuseppe.aprea@gmail.com> wrote:
> Hi list,
>
>
> I wonder if there is any smarter way to apply a filter to a 2 dimensional array
> than a for loop:
>
> a=array(.......)
> idxList=[]
> for i in range(0,a.shape[1]):
>       if (some condition on a[:,i]):
>             idxList.append(i)

Define a "some condition on a[:,i]" that is of interest to you, and I
will show you how to do it. Roughly, you should define a function that
takes 'a' and operates on it in bulk in order to get a boolean array
of shape (a.shape[0],) evaluating the condition for each column. Then
use numpy.where() on that boolean array to get indices if you actually
need indices; frequently, you can just use the boolean array where you
wanted the indices.

-- 
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


More information about the NumPy-Discussion mailing list