[Numpy-discussion] Counting array elements

Todd Miller jmiller at stsci.edu
Thu Oct 21 16:41:29 EDT 2004


On Thu, 2004-10-21 at 18:09, Todd Miller wrote:
> On Thu, 2004-10-21 at 18:01, Todd Miller wrote:
> > On Thu, 2004-10-21 at 14:30, Todd Miller wrote:
> > > On Thu, 2004-10-21 at 13:55, Stephen Walton wrote:
> > > > Is there some simple way of counting the number of array elements which
> > > > satisfy a certain condition?  It is easy to do
> > > > 
> > > > A[A<=1].sum()
> > > > 
> > > > to sum all the values of A which are less than 1, but there doesn't seem
> > > > to be a count() method.  I tried
> > > > 
> > > > (A<=1).sum()
> > > >
> > > > but this throws an exception at numarray 1.1.  If I try
> > > 
> > > This works now in CVS and will be part of numarray-1.2.  
> > 
> > Stephen tried this and it turns out my earlier statement was untrue,
> > (A<=1).sum() doesn't do anything reasonable, even in CVS.  The problem
> > is that sum() is written (without direct C support) to conserve
> > storage.  As a result,  it doesn't do implicit 

<drum roll> up-casting. </drum roll>

I'm pretty sure this was a conscious and discussed choice (this is
actually the 2nd time sum() has been wrong).  IMHO, the typing for sum()
should be revised because it is too dangerous the way it is now.  

Regards,
Todd







More information about the NumPy-Discussion mailing list