Skip to content
Advertisement

Iterate over 2D numpy array to find corresponding maximum values

I have a set of data as such:

interactions=np.array([[0,1], [0,2], [0,3], [1,2], [1, 4], [2, 1], [2,5], [2,7]])

I need to iterate over each value in the first column, find the corresponding maximum value in the second column and then store in a new array (or delete the other values from this array). For this example the final output would therefore be:

interactions=[[0, 3], [1, 4], [2,7]]

I have managed to write a piece of code that will do this for a specific column value, but can’t work out how to turn it into a loop to do the whole array:

Create an array to store values in:

p_gamma=np.amax(interactions[:,0])
zfinal=np.zeros([np.int(p_gamma)+1, 2])

Find the maximum value for each column value (this is where I need the help!):

counter=0
interactions=interactions[interactions[:,0] ==counter]
maxval=np.amax(interactions[:, 1])
interactions=interactions[interactions[:, 1] == maxval]
zfinal[0,:]=interactions

Thanks in advance for any help you can give!!!

Advertisement

Answer

The numpy method for this would be:

i = np.flatnonzero(np.diff(interactions[:, 0])) + 1   # finding indices where first column changes
np.maximum.reduceat(interactions, np.r_[0, i])        # taking maximum values between those indices

array([[0, 3],
       [1, 4],
       [2, 7]], dtype=int32)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement