Reputation: 294278
Returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements: the indices of the input array that give the unique values, the indices of the unique array that reconstruct the input array, and the number of times each unique value comes up in the input array.
Therefore, numpy.unique
must be performing a sorting algorithm. Is there a way to deduce the sorted-ness from the outputs of numpy.unique
. Asked another way, can we derive the same results of an argsort
purely from the output of numpy.unique
so I don't have to sort twice.
Consider arrays a
and b
as sample data.
import numpy as np
from string import ascii_letters
np.random.seed([3,1415])
n = 10000
a = np.random.randint(100, size=n)
b = np.random.choice(list(ascii_letters), n)
Upvotes: 1
Views: 460
Reputation: 294278
I just wrote my own unique function
def unique(a):
s = a.argsort()
a_ = a[s]
return a_[np.append(True, (a_[1:] != a_[:-1]))], s
timing
@hpaulj is correct. Notice that my custom unique
is just as fast as np.unique
when np.unique
is asked to return an inverse
array. Otherwise, when it just returns unique
values, it is a bit faster.
Upvotes: 1