From 30028685861eeb77390196242a9f6bd70cc6c0e5 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Fri, 29 Dec 2023 19:10:19 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/unique/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/numpy/unique/README.md b/numpy/unique/README.md index 8064265..0f212bc 100644 --- a/numpy/unique/README.md +++ b/numpy/unique/README.md @@ -25,3 +25,37 @@ numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, > * the indices of the unique array that reconstruct the input array > * the number of times each unique value comes up in the input array +**unique can be used on multi-dimensional arrays. However, the results are strange since empty places need to be filled for shaping the results into one common matrix.** + +```python +import numpy as np + + +a = np.arange(10, 21) +print(a) # -> [10 11 12 13 14 15 16 17 18 19 20] +idx = np.r_[0:5, 3:8] +print(idx) # -> [0 1 2 3 4 3 4 5 6 7] +print(a[idx]) # -> [10 11 12 13 14 13 14 15 16 17] + +print(np.unique(idx)) # -> [0 1 2 3 4 5 6 7] +print(np.unique(a[idx])) # -> [10 11 12 13 14 15 16 17] +``` + +## There are more return arguments available + +```python +import numpy as np + + +a = np.r_[0:5, 3:8] +print(a) # -> [0 1 2 3 4 3 4 5 6 7] + +values, unique_index = np.unique(a, return_index=True) +_, unique_inverse = np.unique(a, return_inverse=True) +_, unique_counts = np.unique(a, return_counts=True) + +print(values) # -> [0 1 2 3 4 5 6 7] +print(unique_index) # -> [0 1 2 3 4 7 8 9] +print(unique_inverse) # -> [0 1 2 3 4 3 4 5 6 7] +print(unique_counts) # -> [1 1 1 2 2 1 1 1] +```