Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

python - 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!!!

question from:https://stackoverflow.com/questions/65843134/iterate-over-2d-numpy-array-to-find-corresponding-maximum-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...