-
Notifications
You must be signed in to change notification settings - Fork 53
Add argwhere
#449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Let me copy my comment from gh-23 here, because I think it's still relevant/helpful: start of old comment Argh, why do we have both PyTorch has an This may be worth considering. The name is a little annoying though. Especially because >>> x = np.array([0, 4, 7, 0])
>>> np.nonzero(x)
(array([1, 2]),)
>>> np.where(x)
(array([1, 2]),)
>>> np.argwhere(x)
array([[1],
[2]])
>>> x[np.nonzero(x)]
array([4, 7])
>>> x[np.where(x)]
array([4, 7])
>>> x[np.argwhere(x)] # this needs a squeeze() to roundtrip for 1-D input
array([[4],
[7]]) end of old comment
Let me add a higher-dimensional example as well: >>> x.shape
(4, 3, 2)
>>> x[np.nonzero(x)]
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24])
>>> x[np.argwhere(x)].shape
(24, 3, 3, 2) There's more complex ways to use the indices returned from |
Can you give a concrete example of where |
It's trivial to implement |
Right, I guess my question is why getting all indices together into a single array matters. |
As this proposal is without a champion, I'll go ahead and close. |
Currently
nonzero
is defined in the specification, butargwhere
is not. PR ( #23 ) mentionsargwhere
, but it is not currently included.There was some discussion starting with comment ( #23 (comment) ) about whether
argwhere
should be included. Thoughargwhere
was viewed as duplicative, which is understandable.However for libraries that have arrays with unknown shape elements (like Dask).
nonzero
can run into some difficulties as the different arrays may not beconcatenate
d (at least not without an explicit override).argwhere
avoids this issue as it already returns a single array so there is no need toconcatenate
and the array can be split apart easily to satisfy thenonzero
case ( for example: dask/dask#2539 ). As a resultargwhere
becomes practically more useful to handle these unknown shape cases.Would like to discuss adding
argwhere
to the spec to handle this needThe text was updated successfully, but these errors were encountered: