[Numpy-discussion] MaskedArray and the min, max, sum, prod Methods

Alexander Michael lxander.m@gmail....
Fri Jan 4 11:01:14 CST 2008


On Jan 4, 2008 10:42 AM, Pierre GM <pgmdevlist@gmail.com> wrote:
> On Friday 04 January 2008 10:27:32 Alexander Michael wrote:
>
> > Hmm. I liked the base ndarray behavior as it makes a lot of sense to
> > me and provides an easy default that avoids needing to check the
> > result between steps.
>
> I must admit I have troubles conceptualizing the product of an empty array, so
> the ndarray behavior is a bit puzzling to me.

I conceptualize the prod method as the foldl (i.e. reduce) combinator for the
multiplication operator. Combinators of this kind are "initialized" with the
identity value of the operator being applied, which is 1 for multiplication
and 0 for summation (for the integers and reals). These are very rigourous
definitions from category theory and adhered to within the computer science
community (but I'm just a voyeur, so someone correct me if I am wrong). As
for the masking, at least I am prone to think "skip masked elements during
iteration", which may not apply beyond 1D (which could be the issue you
are solving). Concretely, this is how I would write my own prod function:

def ma_prod(a):
    p = 1.0
    for x in a.data[~a.mask]:
        p *= x
    return p

> > Does MaskedArray do this to be compatible
> > with the original ma, or is there a theoretically good reason for it
> > that I am missing?
>
> Yes for the part. For the second, well, I think there's a difference between a
> maskedarray of a given size, where all values are masked, and an empty array.
> Any operation on a fully-masked array should result in maskedarray.masked.

All the CS mumbo-jumbo aside, the real question appears to be are fully masked
arrays empty or undefined? I would consider them conceptually empty, if only
because it makes for a convenient continuity as the number of masked elements
increases from one to the whole of the array.

Thanks for the hard work on the new MaskedArray. I'm attempting to be
constructive by raising these issues, so please let me know if my comments are
otherwise.

Regards,
Alex


More information about the Numpy-discussion mailing list