[Numpy-discussion] Choosing the lesser value
Charles R Harris
charlesr.harris@gmail....
Mon Sep 7 15:16:32 CDT 2009
On Mon, Sep 7, 2009 at 2:46 PM, <nathanielpeterson08@gmail.com> wrote:
> Let
>
> a=np.ma.masked_invalid(np.array([-1,np.nan,-2,-3, np.nan]))
> b=np.ma.masked_invalid(np.array([-2,-3, -1,np.nan,np.nan]))
>
> I'd like to choose the lesser element (component-wise) of a and b.
>
> If the two elements are comparable, I want the lesser element.
> If one element is a number and the other is nan, then I want the number.
> And if both elements are nan, then I'd like resultant element be nan.
>
> In other words, I'd like the result to equal
>
> np.array([-2,-3,-2,-3,np.nan])
>
> This is what I've tried:
>
> #!/usr/bin/env python
> import numpy as np
> a=np.ma.masked_invalid(np.array([-1,np.nan,-2,-3,np.nan]))
> b=np.ma.masked_invalid(np.array([-2,-3, -1,np.nan,np.nan]))
> very_large_num=10
> a=np.ma.filled(a,very_large_num)
> print(a)
> # [ -1. 10. -2. -3. 10.]
> b=np.ma.filled(b,very_large_num)
> print(b)
> # [ -2. -3. -1. 10. 10.]
> result=np.ma.where(np.ma.less(a,b),a,b)
> print(result)
> # [ -2. -3. -2. -3. 10.]
>
> And this almost works, except that:
>
> [*] when both elements are nan, I'm getting my fill value (0) instead of
> nan.
> [*] I can guarantee all elements of a and b are either nan or less than
> some very_large_num, but if there is a solution that works without declaring
> very_large_num, I'd prefer that.
>
> What is the numpy way to choose lesser values in this situation?
> __
The fmin ufunc has the behaviour you want.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090907/267ed369/attachment.html
More information about the NumPy-Discussion
mailing list