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
Docs are unclear about behavior of nonzero on zero-dim arrays. #13522
Sorry to be coming late to this, but might it be good to think whether this should not be classified as a bug that we can fix?
In particular, as @umanwizard wrote (#13522 (comment)), the docstring specifically states that you can extract non-zero elements by doing x[x.nonzero()], i.e., that the function returns (title of docstring), "the indices of the elements that are non-zero". Might it be an idea to do exactly that even for array scalars? I.e., return Ellipsis for the scalar-nonzero case and False for the scalar-zero case?
Though admittedly the return values are not that great. For nonzero scalar, it could also be an empty tuple, but that would seem to give the wrong impression. Unfortunately, just indexing with True gives an extra dimension:
a = np.array(5)
a[True]
# array([5])
For the scalar-zero case, it would be nicer to index with an empty array, but one cannot create a zero-dimensional empty array...
The first extra dimension is actually correct, since nonzero returns a tuple of 1D arrays. True and Falsegive correct/analogous indexing operation. But they are silly for interpretation. The other semi correct thing for nonzero would be a (0, 1) shaped array (instead of tuple) when the element is True and (0, 0) shaped when it is False.
But, neither of those fit with returning a tuple, so I am not sure we should do that (at least without some kwarg?). I guess the function is arguably wrong for 0-D currently, so a deprecation would make sense in that case.
@seberg - The problem with any index array is that will not actually index a scalar, so I think it is not in any way more useful than the current state!
Hence, my wondering whether we can make it useful, even if slightly ugly. Specifically, the start of the docstring is:
Return the indices of the elements that are non-zero.
Returns a tuple of arrays, one for each dimension of `a`,
containing the indices of the non-zero elements in that
dimension.
So, reading this strictly for a zero-dimensional array, I should get a tuple with zero arrays, i.e., (). Which of course is not very useful and if used to index the scalar would always return it unchanged independent of its value. My suggestion above was that it was not going too far from the docstring to take it to mean that one doesn't get a tuple at all, but rather something scalar-like that when used as an index gives the right answer,hence Ellipsis and False (I'd have liked True but clearly that has some advanced indexing semantics that I'm not completely sure I follow).
EDIT: might as well add the explicit example:
a = np.array(5)
a[...]
# array(5)
a[False]
# array([], dtype=int64)
Well, astype(bool) obviously completely changes the meaning. I'm trying to have my cake and eat it! Or "make the function work at least for some of its use cases also for scalars without breaking others". Maybe an empty tuple and False is still better? Then only False is the strange exception to what one would naively expect.
Well, that would mean returning True and False, but that seems strange. I frankly am starting to think if we really want to move this, maybe an error is best, with some information of what is going on.
If someone really wants to use it mostly for indexing, they probably should not be doing that to begin with ;) (although it can speed up things for repeated indexing operations).
astype(bool) completly changes the meaning, but this is what you want to change the meaning to for 0D!
In my examples above, I've been a bit silly with respect to the indexing: a[a.nonzero()] always returns a flat list with the non-zero elements, independent of the shape. So, for an array scalar, one should expect to get a 1-D array back as well, with either zero or one element. Which means returning True or False for the scalar case would work. (Long and short: ideas about Ellipsis and () were bad.)
To me, that is still slightly better than starting to error, but I feel either is better than sticking with the status quo.
Activity
umanwizard commentedon May 10, 2019
cc @mwiebe who (as far as I can tell from git blame) implemented nonzero
eric-wieser commentedon May 10, 2019
You're right that the current behavior is wrong, but I don't think there's a sensible choice to make here.
Perhaps
nonzeroshould just throw an exception if called on a 0d array?umanwizard commentedon May 10, 2019
One thing to think about is what should
a[a.nonzero()]return in each case? (a=np.array(0)anda=np.array(5))eric-wieser commentedon May 10, 2019
@umanwizard: The simple answer is "never use
x[y.nonzero()], usex[y.astype(bool)]instead"abhinavsagar commentedon May 13, 2019
@eric-wieser Should we change the docs informing the above as part of NOTE ?
eric-wieser commentedon May 13, 2019
That sounds like a good idea, although you could probably use more helpful phrasing than I used above.
abhinavsagar commentedon May 13, 2019
Got it. @umanwizard Can you please tell the docs location so that I could proceed
umanwizard commentedon May 13, 2019
Sorry, I don't work on Numpy, I am not sure where the docs are in the repository.
mattip commentedon May 13, 2019
It lives in the docstring for
nonzeroinnumpy/core/fromnumeric.py. See also how to documentabhinavsagar commentedon May 13, 2019
Thanks
mhvk commentedon May 23, 2019
Sorry to be coming late to this, but might it be good to think whether this should not be classified as a bug that we can fix?
In particular, as @umanwizard wrote (#13522 (comment)), the docstring specifically states that you can extract non-zero elements by doing
x[x.nonzero()], i.e., that the function returns (title of docstring), "the indices of the elements that are non-zero". Might it be an idea to do exactly that even for array scalars? I.e., returnEllipsisfor the scalar-nonzero case andFalsefor the scalar-zero case?Though admittedly the return values are not that great. For nonzero scalar, it could also be an empty tuple, but that would seem to give the wrong impression. Unfortunately, just indexing with
Truegives an extra dimension:For the scalar-zero case, it would be nicer to index with an empty array, but one cannot create a zero-dimensional empty array...
seberg commentedon May 23, 2019
The first extra dimension is actually correct, since nonzero returns a tuple of 1D arrays.
TrueandFalsegive correct/analogous indexing operation. But they are silly for interpretation. The other semi correct thing fornonzerowould be a(0, 1)shaped array (instead of tuple) when the element is True and(0, 0)shaped when it is False.But, neither of those fit with returning a tuple, so I am not sure we should do that (at least without some kwarg?). I guess the function is arguably wrong for 0-D currently, so a deprecation would make sense in that case.
mhvk commentedon May 23, 2019
@seberg - The problem with any index array is that will not actually index a scalar, so I think it is not in any way more useful than the current state!
Hence, my wondering whether we can make it useful, even if slightly ugly. Specifically, the start of the docstring is:
So, reading this strictly for a zero-dimensional array, I should get a tuple with zero arrays, i.e.,
(). Which of course is not very useful and if used to index the scalar would always return it unchanged independent of its value. My suggestion above was that it was not going too far from the docstring to take it to mean that one doesn't get a tuple at all, but rather something scalar-like that when used as an index gives the right answer,henceEllipsisandFalse(I'd have likedTruebut clearly that has some advanced indexing semantics that I'm not completely sure I follow).EDIT: might as well add the explicit example:
eric-wieser commentedon May 23, 2019
Trueis correct behavior here - advanced indexing always flattens (or promotes) to a 1d array.If the semantics of the function are reduced to just "something that can be indexed with", it can be reduced greatly to just:
mhvk commentedon May 23, 2019
Well,
astype(bool)obviously completely changes the meaning. I'm trying to have my cake and eat it! Or "make the function work at least for some of its use cases also for scalars without breaking others". Maybe an empty tuple andFalseis still better? Then onlyFalseis the strange exception to what one would naively expect.seberg commentedon May 23, 2019
Well, that would mean returning True and False, but that seems strange. I frankly am starting to think if we really want to move this, maybe an error is best, with some information of what is going on.
If someone really wants to use it mostly for indexing, they probably should not be doing that to begin with ;) (although it can speed up things for repeated indexing operations).
astype(bool)completly changes the meaning, but this is what you want to change the meaning to for 0D!eric-wieser commentedon May 24, 2019
I think we should deprecate this, with a message saying:
mhvk commentedon May 24, 2019
@eric-wieser - am also in favour of changing the behaviour for scalars - any preferences for what it would become?
eric-wieser commentedon May 24, 2019
I'm leaning towards
raise ValueErroruntil we can design something better alongside the oindex / vindex stuff.\The something better would be returning something like:
Used as:
Of course, now
ndarray.__getitem__needs to understand these new indexing objects.mhvk commentedon May 28, 2019
In my examples above, I've been a bit silly with respect to the indexing:
a[a.nonzero()]always returns a flat list with the non-zero elements, independent of the shape. So, for an array scalar, one should expect to get a 1-D array back as well, with either zero or one element. Which means returningTrueorFalsefor the scalar case would work. (Long and short: ideas aboutEllipsisand()were bad.)To me, that is still slightly better than starting to error, but I feel either is better than sticking with the status quo.
eric-wieser commentedon Jun 4, 2019
Deprecation PR is at #13708