How to get a list of all indices of repeated elements in a numpy array?

后端 未结 6 733
你的背包
你的背包 2020-12-03 03:37

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

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 04:24

    @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.

提交回复
热议问题