[Numpy-discussion] filters for rows or columns
Giuseppe Aprea
giuseppe.aprea at gmail.com
Tue Aug 25 19:18:11 EDT 2009
On Tue, Aug 25, 2009 at 8:13 PM, Robert Kern<robert.kern at gmail.com> wrote:
> On Tue, Aug 25, 2009 at 11:07, Giuseppe Aprea<giuseppe.aprea at 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
Hi, I would like to do something like this
a=array([[1,2,3,4],[5,6,7,8],[4,5,6,0]])
idxList=[]
for i in range(0,a.shape[1]):
if len(nonzero(a[:,i])[0])==1: #want to extract column indices
of those columns which only have one non vanishing element
idxList.append(i)
I already used where on !D array but I don't know if there is some function or
some kind of syntax which allow you to evaluate a condition for each
column(row).
regards
g
More information about the NumPy-Discussion
mailing list