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!!!
question from:
https://stackoverflow.com/questions/65843134/iterate-over-2d-numpy-array-to-find-corresponding-maximum-values 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…