-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
DOC: Add unravel_index examples to np.arg[min|max|sort] #9333
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
Changes from all commits
08d8225
364a575
2b514e3
51d0710
cf43db7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -890,6 +890,14 @@ def argsort(a, axis=-1, kind='quicksort', order=None): | |
array([[0, 1], | ||
[0, 1]]) | ||
|
||
Indices of the sorted elements of a N-dimensional array: | ||
>>> np.unravel_index(np.argsort(x, axis=None), x.shape) | ||
(array([0, 1, 1, 0]), array([0, 0, 1, 1])) | ||
>>> from np.testing import assert_equal | ||
>>> assert_equal(x[(array([0, 1, 1, 0]), array([0, 0, 1, 1]))], np.sort(x, axis=None)) | ||
>>> list(zip(*np.unravel_index(np.argsort(x, axis=None), x.shape))) | ||
[(0, 0), (1, 0), (1, 1), (0, 1)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be clearer as:
Repeating the call to |
||
|
||
Sorting with keys: | ||
|
||
>>> x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')]) | ||
|
@@ -952,6 +960,11 @@ def argmax(a, axis=None, out=None): | |
>>> np.argmax(a, axis=1) | ||
array([2, 2]) | ||
|
||
Indices of the maximal elements of a N-dimensional array: | ||
>>> np.unravel_index(np.argmax(a, axis=None), a.shape) | ||
(1, 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to show that the result of this can be used to index directly into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure, it's committed. Let me know if you like the way I've written it. |
||
>>> np.testing.assert_equal(a[(1, 2)], np.max(a)) | ||
|
||
>>> b = np.arange(6) | ||
>>> b[1] = 5 | ||
>>> b | ||
|
@@ -1008,6 +1021,11 @@ def argmin(a, axis=None, out=None): | |
>>> np.argmin(a, axis=1) | ||
array([0, 0]) | ||
|
||
Indices of the minimum elements of a N-dimensional array: | ||
>>> np.unravel_index(np.argmin(a, axis=None), a.shape) | ||
(0, 0) | ||
>>> np.testing.assert_equal(a[(0, 0)], np.min(a)) | ||
|
||
>>> b = np.arange(6) | ||
>>> b[4] = 0 | ||
>>> b | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a short description here to explain the example? That would also be appreciated in the other examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about "Retrieving global sorting indices:" ?
I'm not native English speaker so don't hesitate to propose a reformulated version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe: "Indices along each axis for sorting the flattened array"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Numpy superpowers come from the fact that the user can forget that he lives in a flat space, so I prefer avoiding a description of how it works to privilege what we get. See my new commit for my next intent (which is far from perfect).