8000 DOC: Add unravel_index examples to np.arg[min|max|sort] by ElieGouzien · Pull Request #9333 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Doc : show that the result of the exemple is directly usable as indices.
  • Loading branch information
ElieGouzien committed Aug 7, 2017
commit cf43db7b52677d38ea8809aeb05c49e2155dc931
4 changes: 4 additions & 0 deletions numpy/core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,8 @@ def argsort(a, axis=-1, kind='quicksort', order=None):
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)]
Copy link
Member
@eric-wieser eric-wieser Sep 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be clearer as:

>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape)
>>> ind
(array([0, 1, 1, 0]), array([0, 0, 1, 1]))
>>> x[ind]  # same as np.sort(x, axis=None)
<whatever it is>

Repeating the call to unravel_index is obscuring the point, and assert_equal isn't that useful in docs. (we don't run doctests)


Expand Down Expand Up @@ -961,6 +963,7 @@ def argmax(a, axis=None, out=None):
Indices of the maximal elements of a N-dimensional array:
>>> np.unravel_index(np.argmax(a, axis=None), a.shape)
(1, 2)
Copy link
Member

Choose a reason for hiding this comment

The 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 a, and the same for the argsort example

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -1021,6 +1024,7 @@ def argmin(a, axis=None, out=None):
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
Expand Down
0