You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
np.set_printoptions(sign='legacy') edge case for 0d array #9804
After #9139 got merged, I tried using np.set_printoptions(sign='legacy') to see whether doctests were passing for scikit-learn with numpy > 1.13. I bumped into the following edge case:
numpy 1.13
importnumpyasnpnp.array(1e-10)
Output:
array(1e-10)
numpy 1.14.0.dev0
import numpy as np
np.set_printoptions(sign='legacy')
np.array(1e-10)
Output:
array( 1.00000000e-10)
I was expecting that with np.set_printoptions(sign='legacy'), the behaviour would match exactly on numpy 1.13 and numpy 1.14.0.dev0.
Note that it only happens if the number in the array is smaller than the suppress argument in np.set_printoptions. There are probably work-arounds we can use for scikit-learn doctests, like using np.set_printoptions(sign='legacy', suppress=True) to ensure the scientific notation is not used.
For the 0d case you are talking about we are still working on two competing PRs, #9139 and #9201. (These were actually the motivation for all the printing changes). However, I think they both change the 0d printing relative to 1.13 close to the way you described.
In particular, in 1.13, a typical 0d float array prints as array(1.0), but with those PRs in 1.14 it will print array(1.). Maybe we can somehow finagle things so sign='legacy' changes them too, but it seems difficult, but we will think about it.
Essentially it boils down to the fact that the inner element of 0d and 1d arrays now prints the same, so we will get:
>>>np.array([1e-10]) # unchanged from 1.13 to 1.14 with sign=legacyarray([1.00000000e-10])
>>>np.array(1e-10) # changes in 1.14 to match the 1d behaviorarray(1.00000000e-10)
Oh that's true, I guess the current output was the plan for repr.
Still, maybe we can think of a way of tweaking sign=legacy to use the old .item() computation... I remember considering it but it didn't look super easy.
Actually, probably an easy way to make the sign=legacy case use the old 0d code is just to put a special case in array2string like if legacy is True and arr.shape == (): return style(x.item()).
In that case instead of using sign='legacy' to turn on legacy mode, we would add a new array2string argument like legacy=False. We would also have to keep the style arg.
FWIW the output in my last comment (without the space) is the output of #9139
In which case it sounds like @lesteve is not using as new a version of numpy as they claim to be?
Either way, I think there are two ways of looking at this:
We care about not breaking doctests. Add some more compatibility flags as you suggest.
We care about a stupidly long repr of 1e10. In particular, it seems inconsistent that we show far more of the mantissa when the exponent is large (1.13.1):
@eric-wieser, at first glance, I think we shoud just make a separate PR to adjust the precision in the exp notation. It does seem inconsistent and ugly. Hooray for 10.**arange(-5,0) looking nicer!
In which case it sounds like @lesteve is not using as new a version of numpy as they claim to be?
I am pretty sure that I was using a numpy 1.14 dev version after #9139 was fixed, otherwise I would not have been able to use sign='legacy'.
Maybe we can somehow finagle things so sign='legacy' changes them too, but it seems difficult, but we will think about it.
Warning shot downstream that we won't be held hostage by doctests. Grrr...
As a scikit-learn developer I completely understand this ;-). As I said I think we can work around the problem without requiring numpy changes. I just wanted to report the behaviour so that you can decide what to do about it. And answering "tough luck" is certainly an option I would say, if covering all sign='legacy' edge cases is way too costly.
It's a bit hard for me to keep track of all the PRs related to numpy array str/repr related work but do feel free to ping me and I can test some changes on the scikit-learn doctests if that's helpful.
Activity
ahaldane commentedon Oct 2, 2017
For the 0d case you are talking about we are still working on two competing PRs, #9139 and #9201. (These were actually the motivation for all the printing changes). However, I think they both change the 0d printing relative to 1.13 close to the way you described.
In particular, in 1.13, a typical 0d float array prints as
array(1.0), but with those PRs in 1.14 it will printarray(1.). Maybe we can somehow finagle things sosign='legacy'changes them too, but it seems difficult, but we will think about it.Essentially it boils down to the fact that the inner element of 0d and 1d arrays now prints the same, so we will get:
ahaldane commentedon Oct 2, 2017
By the way, I was aware of this, which is why in the release note I said "setting
sign='legacy'mostly reproduces the 1.13 behavior" :)We'll see if we can remove the "mostly".
eric-wieser commentedon Oct 2, 2017
Surely those PRs only affect
str, and this is aboutrepr?ahaldane commentedon Oct 2, 2017
Oh that's true, I guess the current output was the plan for repr.
Still, maybe we can think of a way of tweaking
sign=legacyto use the old.item()computation... I remember considering it but it didn't look super easy.eric-wieser commentedon Oct 2, 2017
The leading space here looks like a clear bug, but I think the extra digits are a feature.
ahaldane commentedon Oct 3, 2017
FWIW the output in my last comment (without the space) is the output of #9139, and therefore probably #9201 too. So I think the space is fixed.
ahaldane commentedon Oct 3, 2017
Actually, probably an easy way to make the
sign=legacycase use the old 0d code is just to put a special case inarray2stringlikeif legacy is True and arr.shape == (): return style(x.item()).In that case instead of using
sign='legacy'to turn on legacy mode, we would add a new array2string argument likelegacy=False. We would also have to keep the style arg.eric-wieser commentedon Oct 3, 2017
In which case it sounds like @lesteve is not using as new a version of numpy as they claim to be?
Either way, I think there are two ways of looking at this:
reprof1e10. In particular, it seems inconsistent that we show far more of the mantissa when the exponent is large (1.13.1):legacy=Falsemode?charris commentedon Oct 3, 2017
My argument for getting all these changes in is
In any case, I think we are pretty close to having at least a workable solution for doctests.
MAINT: create a "legacy" print mode to account for whitespace and 0d …
ahaldane commentedon Oct 3, 2017
@eric-wieser, at first glance, I think we shoud just make a separate PR to adjust the precision in the exp notation. It does seem inconsistent and ugly. Hooray for
10.**arange(-5,0)looking nicer!lesteve commentedon Oct 3, 2017
I am pretty sure that I was using a numpy 1.14 dev version after #9139 was fixed, otherwise I would not have been able to use
sign='legacy'.As a scikit-learn developer I completely understand this ;-). As I said I think we can work around the problem without requiring numpy changes. I just wanted to report the behaviour so that you can decide what to do about it. And answering "tough luck" is certainly an option I would say, if covering all sign='legacy' edge cases is way too costly.
It's a bit hard for me to keep track of all the PRs related to numpy array str/repr related work but do feel free to ping me and I can test some changes on the scikit-learn doctests if that's helpful.
ahaldane commentedon Oct 4, 2017
Oh I see the confusion. My output was for
sign='-', @lesteve's was forsign='legacy'.