Search code examples
pythonnumpythresholdnumpy-ndarray

Find indices of values greater than a threshold by row in a numpy 2darray


I have a 2darray as follows. I want to find the indexes of values above a threshold (e.g., 0.7) by each rows in the array.

items= np.array([[1.        , 0.40824829, 0.03210806, 0.29488391, 0.        ,
        0.5       , 0.32444284, 0.57735027, 0.        , 0.5       ],
       [0.40824829, 1.        , 0.57675476, 0.48154341, 0.        ,
        0.81649658, 0.79471941, 0.70710678, 0.57735027, 0.40824829],
       [0.03210806, 0.57675476, 1.        , 0.42606683, 0.        ,
        0.        , 0.92713363, 0.834192  , 0.        , 0.73848549],
       [0.29488391, 0.48154341, 0.42606683, 1.        , 0.        ,
        0.29488391, 0.52620136, 0.51075392, 0.20851441, 0.44232587],
       [0.        , 0.        , 0.        , 0.        , 0.        ,
        0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.5       , 0.81649658, 0.        , 0.29488391, 0.        ,
        1.        , 0.32444284, 0.28867513, 0.70710678, 0.        ],
       [0.32444284, 0.79471941, 0.92713363, 0.52620136, 0.        ,
        0.32444284, 1.        , 0.93658581, 0.22941573, 0.81110711],
       [0.57735027, 0.70710678, 0.834192  , 0.51075392, 0.        ,
        0.28867513, 0.93658581, 1.        , 0.        , 0.8660254 ],
       [0.        , 0.57735027, 0.        , 0.20851441, 0.        ,
        0.70710678, 0.22941573, 0.        , 1.        , 0.        ],
       [0.5       , 0.40824829, 0.73848549, 0.44232587, 0.        ,
        0.        , 0.81110711, 0.8660254 , 0.        , 1.        ]])

indices_items = np.argwhere(items>= 0.7)

This (indices_items) returns

array([[0, 0],
       [1, 1],
       [1, 5],
       [1, 6],
       [1, 7],
       [2, 2],
       [2, 6],
       [2, 7],
       [2, 9],
       [3, 3],
       [5, 1],
       [5, 5],
       [5, 8],
       [6, 1],
       [6, 2],
       [6, 6],
       [6, 7],
       [6, 9],
       [7, 1],
       [7, 2],
       [7, 6],
       [7, 7],
       [7, 9],
       [8, 5],
       [8, 8],
       [9, 2],
       [9, 6],
       [9, 7],
       [9, 9]], dtype=int64)

How can i can get the indices by rows as below? row0 -> [0] row1-> [0,1,5,6,7] row2-> [2,6,7,9] row3-> [3] row4-> [] #this should be empty list as there are no values above the threshold...


Solution

  • Get the row, col with np.where, then use np.searchsorted to get the intervaled-indices on row-array and use those to split col-array -

    In [38]: r,c = np.where(items>= 0.7)
    
    In [39]: np.split(c,np.searchsorted(r,range(1,items.shape[0])))
    Out[39]: 
    [array([0], dtype=int64),
     array([1, 5, 6, 7], dtype=int64),
     array([2, 6, 7, 9], dtype=int64),
     array([3], dtype=int64),
     array([], dtype=int64),
     array([1, 5, 8], dtype=int64),
     array([1, 2, 6, 7, 9], dtype=int64),
     array([1, 2, 6, 7, 9], dtype=int64),
     array([5, 8], dtype=int64),
     array([2, 6, 7, 9], dtype=int64)]