Skip to content Skip to sidebar Skip to footer

Numpy Find Indices Of Groups With Same Value

I have a numpy array of zeros and ones: y=[1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1] I want to calculate the indices of groups of ones (or zeros). So for the above example the resul

Solution 1:

We can do something like this that works for any generic array -

defislandinfo(y, trigger_val, stopind_inclusive=True):
    # Setup "sentients" on either sides to make sure we have setup# "ramps" to catch the start and stop for the edge islands# (left-most and right-most islands) respectively
    y_ext = np.r_[False,y==trigger_val, False]

    # Get indices of shifts, which represent the start and stop indices
    idx = np.flatnonzero(y_ext[:-1] != y_ext[1:])

    # Lengths of islands if needed
    lens = idx[1::2] - idx[:-1:2]

    # Using a stepsize of 2 would get us start and stop indices for each islandreturnlist(zip(idx[:-1:2], idx[1::2]-int(stopind_inclusive))), lens

Sample run -

In[320]: yOut[320]: array([1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1])

In[321]: islandinfo(y, trigger_val=1)[0]Out[321]: [(0, 2), (8, 9), (16, 19)]In[322]: islandinfo(y, trigger_val=0)[0]Out[322]: [(3, 7), (10, 15)]

Alternatively, we can use diff to get the sliced comparisons and then simply reshape with 2 columns to replace the step-sized slicing to give ourselves a one-liner -

In [300]: np.flatnonzero(np.diff(np.r_[0,y,0])!=0).reshape(-1,2) - [0,1]
Out[300]: 
array([[ 0,  2],
       [ 8,  9],
       [16, 19]])

Post a Comment for "Numpy Find Indices Of Groups With Same Value"