[Numpy-discussion] Counting array elements

Peter Verveer verveer at embl-heidelberg.de
Mon Oct 25 09:44:04 CDT 2004

On 25 Oct 2004, at 18:19, Stephen Walton wrote:

> On Mon, 2004-10-25 at 10:26 +0200, Peter Verveer wrote:
>> On 25 Oct 2004, at 04:17, Stephen Walton wrote:
>>>
>>> I don't think we need sumall.  The methods and the functions should
>>> simply work the same way.  If one wants sumall, use A.flat.sum() or,
>>> if
>>> you can't use the methods or attributes on your old version of
>>> Python,
>>> sum(ravel(A)).
>>
>> I think this may be inefficient, because ravel and flat may make a
>> copy
>> of the data. Also I think using flat/ravel in such a way is plain ugly
>> and a complex way to do it.
>
> You may be right about the copying, I couldn't say.  I don't think
> sum(ravel(A)) looks any worse than sum(sum(sum(A))) for a rank 3 array,
> but ugly is in the eye of the beholder.

It does not look worse, I agree with that! But I would argue it should
have been sum(A) in the first place to sum over al axes... The sumall
would not have been needed, and summing over one (or a sub-set) axis
could have been implemented as a an optional argument to sum().
>
>> In my opinion functions that calculate a statistic like sum
>> should return the total in the first place, rather then over a single
>> axis.
>
> It depends on the data.  I use rank-2 arrays which are images and are
> therefore homogeneous.  Even there, though, I often want the sum of all
> rows or all columns.  For heterogeneous data (e.g., columns of
> different
> Y's as a function of X), the present sum() makes sense.  In other
> words,
> we will always need ways to sum over just one dimension and over all
> dimensions.  By analogy with MATLAB (I'm guessing), sum() in Numeric
> and
> numarray does a one-D sum.

I agree it is a useful feature, and it should still be possible to do
that using an optional axis argument, even better I would love to be
able to sum over several axes in one go, I find the one-dimensional
character of reduce limiting, but I digress. In any case, I suppose we
will stick with the current behaviour for backwards compatibility.

Cheers, Peter