From 08d8225fac69942264f69a2cd606ce15d84bcd34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Gouzien?= Date: Thu, 29 Jun 2017 23:54:11 +0200 Subject: [PATCH 1/5] Exemples for np.arg[min|max|sort] on how to retrieve indices for d-dim arrays. --- numpy/core/fromnumeric.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index a8c2fd2fb088..6311d22adf08 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -890,6 +890,13 @@ def argsort(a, axis=-1, kind='quicksort', order=None): array([[0, 1], [0, 1]]) + >>> np.unravel_index(np.argsort(x, axis=None), x.shape) + (array([0, 1, 1, 0]), array([0, 0, 1, 1])) + >>> x[np.unravel_index(np.argsort(x, axis=None), x.shape)] + array([0, 2, 2, 3]) + >>> 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', '>> np.argmax(a, axis=1) array([2, 2]) + >>> np.unravel_index(np.argmax(a, axis=None), a.shape) + (1, 2) >>> b = np.arange(6) >>> b[1] = 5 @@ -1007,6 +1016,8 @@ def argmin(a, axis=None, out=None): array([0, 0, 0]) >>> np.argmin(a, axis=1) array([0, 0]) + >>> np.unravel_index(np.argmin(a, axis=None), a.shape) + (0, 0) >>> b = np.arange(6) >>> b[4] = 0 From 364a57579fb47f931a45b99c1c84439d49cf8df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Gouzien?= Date: Fri, 30 Jun 2017 01:32:34 +0200 Subject: [PATCH 2/5] Exemples for np.arg[min|max|sort] on how to retrieve indices for d-dim arrays: second proposition. --- numpy/core/fromnumeric.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 6311d22adf08..4a91199f3c23 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -890,10 +890,9 @@ def argsort(a, axis=-1, kind='quicksort', order=None): array([[0, 1], [0, 1]]) + Indices for sorting the 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])) - >>> x[np.unravel_index(np.argsort(x, axis=None), x.shape)] - array([0, 2, 2, 3]) >>> list(zip(*np.unravel_index(np.argsort(x, axis=None), x.shape))) [(0, 0), (1, 0), (1, 1), (0, 1)] @@ -958,6 +957,8 @@ def argmax(a, axis=None, out=None): array([1, 1, 1]) >>> 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) @@ -1016,6 +1017,8 @@ def argmin(a, axis=None, out=None): array([0, 0, 0]) >>> 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) From 2b514e32e41be3f37d817ed270f0275211d04c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Gouzien?= Date: Fri, 30 Jun 2017 01:44:52 +0200 Subject: [PATCH 3/5] Exemples for np.arg[min|max|sort] : use of "coordinates" instead of "indices". --- numpy/core/fromnumeric.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 4a91199f3c23..298846a9d863 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -890,7 +890,7 @@ def argsort(a, axis=-1, kind='quicksort', order=None): array([[0, 1], [0, 1]]) - Indices for sorting the elements of a N-dimensional array: + Coordinates 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])) >>> list(zip(*np.unravel_index(np.argsort(x, axis=None), x.shape))) @@ -958,7 +958,7 @@ 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: + Coordinates of the maximal elements of a N-dimensional array: >>> np.unravel_index(np.argmax(a, axis=None), a.shape) (1, 2) @@ -1018,7 +1018,7 @@ 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: + Coordinates of the minimum elements of a N-dimensional array: >>> np.unravel_index(np.argmin(a, axis=None), a.shape) (0, 0) From 51d0710d93211474bf11f7fcc04fc9fb6ea477d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Gouzien?= Date: Fri, 4 Aug 2017 10:11:59 +0200 Subject: [PATCH 4/5] Revert "coordinates" to "indices". --- numpy/core/fromnumeric.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 298846a9d863..e0596bdc2e3a 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -890,7 +890,7 @@ def argsort(a, axis=-1, kind='quicksort', order=None): array([[0, 1], [0, 1]]) - Coordinates of the sorted elements of a N-dimensional array: + 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])) >>> list(zip(*np.unravel_index(np.argsort(x, axis=None), x.shape))) @@ -958,7 +958,7 @@ def argmax(a, axis=None, out=None): >>> np.argmax(a, axis=1) array([2, 2]) - Coordinates of the maximal elements of a N-dimensional array: + Indices of the maximal elements of a N-dimensional array: >>> np.unravel_index(np.argmax(a, axis=None), a.shape) (1, 2) @@ -1018,7 +1018,7 @@ def argmin(a, axis=None, out=None): >>> np.argmin(a, axis=1) array([0, 0]) - Coordinates of the minimum elements of a N-dimensional array: + Indices of the minimum elements of a N-dimensional array: >>> np.unravel_index(np.argmin(a, axis=None), a.shape) (0, 0) From cf43db7b52677d38ea8809aeb05c49e2155dc931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Gouzien?= Date: Mon, 7 Aug 2017 22:03:04 +0200 Subject: [PATCH 5/5] Doc : show that the result of the exemple is directly usable as indices. --- numpy/core/fromnumeric.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index e0596bdc2e3a..a42904c62ba2 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -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)] @@ -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) + >>> np.testing.assert_equal(a[(1, 2)], np.max(a)) >>> b = np.arange(6) >>> b[1] = 5 @@ -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