[SciPy-user] allclose friend

Tom Johnson tjhnson@gmail....
Fri Jan 11 12:24:20 CST 2008


On Jan 11, 2008 12:44 AM, lorenzo bolla <lbolla@gmail.com> wrote:
> What do you expect to be handled NaN?
> It looks like allclose() gives False with NaN, because NaN==NaN is always
> False.
> Shouldn't it use numpy.isnan?
> Can't you use simply == to obtain a boolean array?
>
> In [21]: x
> Out[21]: array([  1.,   2.,  Inf,  NaN])
>
> In [22]: y
> Out[22]: array([  1.,   0.,  Inf,  NaN])
>
> In [23]: z
> Out[23]: array([  1.,   2.,  Inf,  NaN])
>
> In [24]: x == y
> Out[24]: array([ True, False,  True, False], dtype=bool)
>
> In [25]: x == z
> Out[25]: array([ True,  True,  True, False], dtype=bool)
>
> In [26]: numpy.allclose(x,y)
> Out[26]: False
>
> In [27]: numpy.allclose(x,z)
> Out[27]: False
>
> L.

Well, I'm not really interested in *only* testing NaN.  My goal is for
floating point comparisons...if my data happens to have NaN or infs
being compared to floats/inf/NaN, then it want these handled properly
as well.   Most of all, I want a *single* command to do this.

I tried vectorizing allclose, but it doesn't seem to allow me to
specify atol or rtol anymore (which is required).

>>> from scipy import allclose, vectorize
>>> allclose_v = vectorize(allclose)
>>> allclose_v([5.000000001,5,5],[5,5,inf])
array([ True,  True, False], dtype=bool)
>>> allclose_v([1.000000001,NaN],[1,NaN])
array([ True, True], dtype=bool)
>>> v_allclose([1.00000000,inf],[1,NaN])
array([ True, False], dtype=bool)

...and it fails for inf comparisons...

>>> v_allclose([1.00000001,inf],[1,inf])
<type 'exceptions.IndexError'>: 0-d arrays can't be indexed

Certainly, this should be handled properly giving [True, True].


Does that clarify the request?  One function for elementwise float
comparisons, properly handling inf, NaN....with atol, rtol options.


More information about the SciPy-user mailing list