[Numpy-discussion] Counting array elements
Stephen Walton
stephen.walton at csun.edu
Thu Oct 21 10:59:05 EDT 2004
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
sum(A<=value)
I have to nest multiple sums if A has rank greater than 1, plus the sum
overflows if A is large, apparently because boolean gets treated as
Int8. (Try A=arange(1024,shape=(32,32));sum(sum(A<=1024)). You get
zero.) The following works:
array(A<=1024,type=Int32).sum()
but is awkward. Am I missing an obvious better alternative? If not,
I'm going to file an RFE :-) .
--
Stephen Walton <stephen.walton at csun.edu>
Dept. of Physics & Astronomy, Cal State Northridge
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20041021/85482632/attachment.sig>
More information about the NumPy-Discussion
mailing list