Understanding np.min behaviour with nan
I'm having some trouble understanding the behaviour of np.min when used with NaN. In the documentation of np.amin it says: "NaN values are propagated, that is if at least one item is NaN, the corresponding min value will be NaN as well. ". It doesn't say that in some circumstances there will be RuntimeWarning's raised (presumably depending on the errstate). Consider the following:
import numpy as np np.min([1, np.nan]) /Users/andrew/miniconda3/envs/dev3/lib/python3.6/site-packages/numpy/core/_methods.py:29: RuntimeWarning: invalid value encountered in reduce return umr_minimum(a, axis, None, out, keepdims) nan np.min([1, 2, np.nan]) nan np.min([np.nan, 1, 2]) nan np.min([np.nan, 1]) nan
Why is there a RuntimeWarning for the first example, but not the others? Is this expected behaviour? -- _____________________________________ Dr. Andrew Nelson _____________________________________
Further to my last message, why is the warning only raised once? ```
import numpy as np np.min([1, np.nan]) /Users/andrew/miniconda3/envs/dev3/lib/python3.6/site-packages/numpy/core/_methods.py:29: RuntimeWarning: invalid value encountered in reduce return umr_minimum(a, axis, None, out, keepdims) nan np.min([1, np.nan]) nan
On Wed, Jan 10, 2018 at 3:03 PM, Andrew Nelson
Further to my last message, why is the warning only raised once?
```
import numpy as np np.min([1, np.nan])
/Users/andrew/miniconda3/envs/dev3/lib/python3.6/site-packages/numpy/core/_methods.py:29: RuntimeWarning: invalid value encountered in reduce
return umr_minimum(a, axis, None, out, keepdims) nan
np.min([1, np.nan]) nan
This is default behavior for warnings. https://docs.python.org/3/library/warnings.html#the-warnings-filter It also explains the previous results. The same warning would have been issued from the same place in each of the variations you tried. Since the warnings mechanism had already seen that RuntimeWarning with the same message from the same code location, they were not printed. -- Robert Kern
On 10 January 2018 at 23:03, Andrew Nelson
Further to my last message, why is the warning only raised once?
Warnings are only raised once because that is the default behaviour for warnings. The warning is issued every time but only displayed on the first occurence. You can control this with the warnings module: https://docs.python.org/3.6/library/warnings.html Gerrit.
The same warning would have been issued from the same place in each of the variations you tried.
That's not the case, I tried with np.min([1, 2, 3, np.nan]) in a fresh interpreter and no warning was raised. Furthermore on my work computer (conda python3.6.2 with pip installed numpy) I can't get the problem to show at all:
import numpy as np np.version.version '1.14.0' np.min([1., 2., 3., 4., np.nan]) nan np.min([1., 2., 3., np.nan, 4.]) nan np.min([1., 2., np.nan, 3., 4.]) nan np.min([1., np.nan, 2., 3., 4.]) nan np.min([np.nan, 1., 2., 3., 4.]) nan np.min([np.nan, 1.]) nan np.min([np.nan, 1., np.nan]) nan np.min([1., np.nan]) nan np.seterr(all='raise') {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'} np.min([1., np.nan]) nan np.min([np.nan, 1.]) nan np.min([np.nan, 1., 2., 3., 4.]) nan np.min([np.nan, 1., 2., 3., 4.]) nan
The context for these questions is the sudden CI fails I'm observing for scipy on appveyor - https://ci.appveyor.com/project/scipy/scipy/build/1.0.1444/job/n05ptntm0xxjk...
On Wed, Jan 10, 2018 at 5:24 PM, Andrew Nelson
The same warning would have been issued from the same place in each of the variations you tried.
That's not the case, I tried with np.min([1, 2, 3, np.nan]) in a fresh interpreter and no warning was raised.
Furthermore on my work computer (conda python3.6.2 with pip installed numpy) I can't get the problem to show at all:
import numpy as np np.version.version '1.14.0' np.min([1., 2., 3., 4., np.nan]) nan np.min([1., 2., 3., np.nan, 4.]) nan np.min([1., 2., np.nan, 3., 4.]) nan np.min([1., np.nan, 2., 3., 4.]) nan np.min([np.nan, 1., 2., 3., 4.]) nan np.min([np.nan, 1.]) nan np.min([np.nan, 1., np.nan]) nan np.min([1., np.nan]) nan np.seterr(all='raise') {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'} np.min([1., np.nan]) nan np.min([np.nan, 1.]) nan np.min([np.nan, 1., 2., 3., 4.]) nan np.min([np.nan, 1., 2., 3., 4.]) nan
The context for these questions is the sudden CI fails I'm observing for scipy on appveyor - https://ci.appveyor.com/project/scipy/scipy/build/1.0. 1444/job/n05ptntm0xxjklvt
Different compilers/libraries respond differently to nans. If the change in scipy is recent, probably compiler flags or something similar has changed in the test environment. Chuck
participants (4)
-
Andrew Nelson
-
Charles R Harris
-
Gerrit Holl
-
Robert Kern