Find the row indexes of several values in a numpy array

前端 未结 6 1963
醉话见心
醉话见心 2020-11-22 02:01

I have an array X:

X = np.array([[4,  2],
              [9,  3],
              [8,  5],
              [3,  3],
              [5,  6]])

And

6条回答
  •  难免孤独
    2020-11-22 02:19

    Here is a pretty fast solution that scales up well using numpy and hashlib. It can handle large dimensional matrices or images in seconds. I used it on 520000 X (28 X 28) array and 20000 X (28 X 28) in 2 seconds on my CPU

    Code:

    import numpy as np
    import hashlib
    
    
    X = np.array([[4,  2],
                  [9,  3],
                  [8,  5],
                  [3,  3],
                  [5,  6]])
    
    searched_values = np.array([[4, 2],
                                [3, 3],
                                [5, 6]])
    
    #hash using sha1 appears to be efficient
    xhash=[hashlib.sha1(row).digest() for row in X]
    yhash=[hashlib.sha1(row).digest() for row in searched_values]
    
    z=np.in1d(xhash,yhash)  
    
    ##Use unique to get unique indices to ind1 results
    _,unique=np.unique(np.array(xhash)[z],return_index=True)
    
    ##Compute unique indices by indexing an array of indices
    idx=np.array(range(len(xhash)))
    unique_idx=idx[z][unique]
    
    print('unique_idx=',unique_idx)
    print('X[unique_idx]=',X[unique_idx])
    

    Output:

    unique_idx= [4 3 0]
    X[unique_idx]= [[5 6]
     [3 3]
     [4 2]]
    

提交回复
热议问题