From a85c17727fc573f1d0cc2e993792a8694eac581e Mon Sep 17 00:00:00 2001 From: jaimefrio Date: Wed, 27 Aug 2014 22:52:19 -0700 Subject: ENH: Speed up `unique` with `return_inverse` This replaces a sort with fancy indexing. --- numpy/lib/arraysetops.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'numpy/lib/arraysetops.py') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index 2d98c35d2..5ace7146a 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -204,8 +204,9 @@ def unique(ar, return_index=False, return_inverse=False, return_counts=False): ret += (perm[flag],) if return_inverse: iflag = np.cumsum(flag) - 1 - iperm = perm.argsort() - ret += (np.take(iflag, iperm),) + inv_idx = np.empty_like(ar, dtype=np.intp) + inv_idx[perm] = iflag + ret += (inv_idx,) if return_counts: idx = np.concatenate(np.nonzero(flag) + ([ar.size],)) ret += (np.diff(idx),) -- cgit v1.2.1