Find unique rows in numpy.array

后端 未结 20 2872
独厮守ぢ
独厮守ぢ 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 11:40

    np.unique works given a list of tuples:

    >>> np.unique([(1, 1), (2, 2), (3, 3), (4, 4), (2, 2)])
    Out[9]: 
    array([[1, 1],
           [2, 2],
           [3, 3],
           [4, 4]])
    

    With a list of lists it raises a TypeError: unhashable type: 'list'

    0 讨论(0)
  • 2020-11-21 11:41

    Based on the answer in this page I have written a function that replicates the capability of MATLAB's unique(input,'rows') function, with the additional feature to accept tolerance for checking the uniqueness. It also returns the indices such that c = data[ia,:] and data = c[ic,:]. Please report if you see any discrepancies or errors.

    def unique_rows(data, prec=5):
        import numpy as np
        d_r = np.fix(data * 10 ** prec) / 10 ** prec + 0.0
        b = np.ascontiguousarray(d_r).view(np.dtype((np.void, d_r.dtype.itemsize * d_r.shape[1])))
        _, ia = np.unique(b, return_index=True)
        _, ic = np.unique(b, return_inverse=True)
        return np.unique(b).view(d_r.dtype).reshape(-1, d_r.shape[1]), ia, ic
    
    0 讨论(0)
  • 2020-11-21 11:42

    Beyond @Jaime excellent answer, another way to collapse a row is to uses a.strides[0] (assuming a is C-contiguous) which is equal to a.dtype.itemsize*a.shape[0]. Furthermore void(n) is a shortcut for dtype((void,n)). we arrive finally to this shortest version :

    a[unique(a.view(void(a.strides[0])),1)[1]]
    

    For

    [[0 1 1 1 0 0]
     [1 1 1 0 0 0]
     [1 1 1 1 1 0]]
    
    0 讨论(0)
  • 2020-11-21 11:42

    The most straightforward solution is to make the rows a single item by making them strings. Each row then can be compared as a whole for its uniqueness using numpy. This solution is generalize-able you just need to reshape and transpose your array for other combinations. Here is the solution for the problem provided.

    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]])
    
    uniques, index = np.unique([str(i) for i in original], return_index=True)
    cleaned = original[index]
    print(cleaned)    
    

    Will Give:

     array([[0, 1, 1, 1, 0, 0],
            [1, 1, 1, 0, 0, 0],
            [1, 1, 1, 1, 1, 0]])
    

    Send my nobel prize in the mail

    0 讨论(0)
  • 2020-11-21 11:45

    np.unique works by sorting a flattened array, then looking at whether each item is equal to the previous. This can be done manually without flattening:

    ind = np.lexsort(a.T)
    a[ind[np.concatenate(([True],np.any(a[ind[1:]]!=a[ind[:-1]],axis=1)))]]
    

    This method does not use tuples, and should be much faster and simpler than other methods given here.

    NOTE: A previous version of this did not have the ind right after a[, which mean that the wrong indices were used. Also, Joe Kington makes a good point that this does make a variety of intermediate copies. The following method makes fewer, by making a sorted copy and then using views of it:

    b = a[np.lexsort(a.T)]
    b[np.concatenate(([True], np.any(b[1:] != b[:-1],axis=1)))]
    

    This is faster and uses less memory.

    Also, if you want to find unique rows in an ndarray regardless of how many dimensions are in the array, the following will work:

    b = a[lexsort(a.reshape((a.shape[0],-1)).T)];
    b[np.concatenate(([True], np.any(b[1:]!=b[:-1],axis=tuple(range(1,a.ndim)))))]
    

    An interesting remaining issue would be if you wanted to sort/unique along an arbitrary axis of an arbitrary-dimension array, something that would be more difficult.

    Edit:

    To demonstrate the speed differences, I ran a few tests in ipython of the three different methods described in the answers. With your exact a, there isn't too much of a difference, though this version is a bit faster:

    In [87]: %timeit unique(a.view(dtype)).view('<i8')
    10000 loops, best of 3: 48.4 us per loop
    
    In [88]: %timeit ind = np.lexsort(a.T); a[np.concatenate(([True], np.any(a[ind[1:]]!= a[ind[:-1]], axis=1)))]
    10000 loops, best of 3: 37.6 us per loop
    
    In [89]: %timeit b = [tuple(row) for row in a]; np.unique(b)
    10000 loops, best of 3: 41.6 us per loop
    

    With a larger a, however, this version ends up being much, much faster:

    In [96]: a = np.random.randint(0,2,size=(10000,6))
    
    In [97]: %timeit unique(a.view(dtype)).view('<i8')
    10 loops, best of 3: 24.4 ms per loop
    
    In [98]: %timeit b = [tuple(row) for row in a]; np.unique(b)
    10 loops, best of 3: 28.2 ms per loop
    
    In [99]: %timeit ind = np.lexsort(a.T); a[np.concatenate(([True],np.any(a[ind[1:]]!= a[ind[:-1]],axis=1)))]
    100 loops, best of 3: 3.25 ms per loop
    
    0 讨论(0)
  • 2020-11-21 11:46

    As of NumPy 1.13, one can simply choose the axis for selection of unique values in any N-dim array. To get unique rows, one can do:

    unique_rows = np.unique(original_array, axis=0)

    0 讨论(0)
提交回复
热议问题