I have a set of data as such:
JavaScript
x
2
1
interactions=np.array([[0,1], [0,2], [0,3], [1,2], [1, 4], [2, 1], [2,5], [2,7]])
2
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:
JavaScript
1
2
1
interactions=[[0, 3], [1, 4], [2,7]]
2
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:
JavaScript
1
3
1
p_gamma=np.amax(interactions[:,0])
2
zfinal=np.zeros([np.int(p_gamma)+1, 2])
3
Find the maximum value for each column value (this is where I need the help!):
JavaScript
1
6
1
counter=0
2
interactions=interactions[interactions[:,0] ==counter]
3
maxval=np.amax(interactions[:, 1])
4
interactions=interactions[interactions[:, 1] == maxval]
5
zfinal[0,:]=interactions
6
Thanks in advance for any help you can give!!!
Advertisement
Answer
The numpy
method for this would be:
JavaScript
1
7
1
i = np.flatnonzero(np.diff(interactions[:, 0])) + 1 # finding indices where first column changes
2
np.maximum.reduceat(interactions, np.r_[0, i]) # taking maximum values between those indices
3
4
array([[0, 3],
5
[1, 4],
6
[2, 7]], dtype=int32)
7