I\'m trying to get the index of all repeated elements in a numpy array, but the solution I found for the moment is REALLY inefficient for a large (>20000 elements) input
@gg349's solution packaged up into a function:
def better_np_unique(arr):
sort_indexes = np.argsort(arr)
arr = np.asarray(arr)[sort_indexes]
vals, first_indexes, inverse, counts = np.unique(arr,
return_index=True, return_inverse=True, return_counts=True)
indexes = np.split(sort_indexes, first_indexes[1:])
for x in indexes:
x.sort()
return vals, indexes, inverse, counts
It's essentially the same as np.unique
but returns all indices, not just the first indices.