diff options
author | Élie Gouzien <elie.gouzien@ens.fr> | 2017-06-29 23:54:11 +0200 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2017-10-05 11:47:53 -0600 |
commit | a94c15fcd6456c6df3a060a5fdae797649eb787e (patch) | |
tree | ffc8b190af2d199d7f6f1b811f85699cd6725d4c /numpy/core/fromnumeric.py | |
parent | 04c43f177dcf156ab85118898d30870a38df70cc (diff) | |
download | numpy-a94c15fcd6456c6df3a060a5fdae797649eb787e.tar.gz |
DOC: Add examples for np.arg[min|max|sort]
Show how to retrieve indices for d-dim arrays.
[ci skip]
Diffstat (limited to 'numpy/core/fromnumeric.py')
-rw-r--r-- | numpy/core/fromnumeric.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index a94be7b4d..78b156f21 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -885,6 +885,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)] + Sorting with keys: >>> x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')]) @@ -947,6 +955,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) + >>> np.testing.assert_equal(a[(1, 2)], np.max(a)) + >>> b = np.arange(6) >>> b[1] = 5 >>> b @@ -1003,6 +1016,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 |