Find unique rows in numpy.array

后端 未结 20 2856
独厮守ぢ
独厮守ぢ 2020-11-21 10:57

I need to find unique rows in a numpy.array.

For example:

>>> a # I have
array([[1, 1, 1, 0, 0, 0],
       [0, 1, 1, 1, 0, 0],
         


        
20条回答
  •  礼貌的吻别
    2020-11-21 12:03

    import numpy as np
    original = np.array([[1, 1, 1, 0, 0, 0],
                         [0, 1, 1, 1, 0, 0],
                         [0, 1, 1, 1, 0, 0],
                         [1, 1, 1, 0, 0, 0],
                         [1, 1, 1, 1, 1, 0]])
    # create a view that the subarray as tuple and return unique indeies.
    _, unique_index = np.unique(original.view(original.dtype.descr * original.shape[1]),
                                return_index=True)
    # get unique set
    print(original[unique_index])
    

提交回复
热议问题