1

group the rows having same first element and find max according to last column.

Like row 0 and 1 have same starting element so go to last column and find max from it and return that row without looping .

utl = np.array ([[ 21. ,            0.01      ],
                 [ 21. ,            0.02      ],
                 [ 26. ,            0.04      ],
                 [ 26. ,            0.03      ],
                 [ 26. ,            0.03      ],
                 [ 34. ,            0.03      ],
                 [ 34. ,            0.09      ],
                 [ 26. ,            0.03      ]])           
                 


output must be

[ 21. ,            0.02      ]
[ 34. ,            0.09      ]
[ 26. ,            0.04      ]

2
  • Is the final order important? Commented Jan 20, 2022 at 14:02
  • no, final order not important Commented Jan 20, 2022 at 14:04

2 Answers 2

2

If the final order of the rows is not important:

# sort by first and second column
a = utl[np.lexsort((utl[:,1], utl[:,0]))]

# get positions of group change
# as we want the max, we take the last row per group
_, idx = np.unique(a[:,0], return_index=True)
idx2 = (idx-1)%a.shape[0]  # or idx2 = np.r_[idx[1:]-1, [a.shape[0]-1]]

# split
a[idx2]

output:

array([[21, 0.02],
       [26, 0.04],
       [34, 0.09]])
solution for the min
a = utl[np.lexsort((utl[:,1], utl[:,0]))]
_, idx = np.unique(a[:,0], return_index=True)
a[idx]
Sign up to request clarification or add additional context in comments.

5 Comments

That's not what sort(axis=0) does...
Which is why your answer doesn't match. You likely want lexsort. No need for unique at all.
@MadPhysicist thanks, oversight from my side, I fixed it. If you have a better solution, you can provide it ;)
unique performs a second sort. I posted a lexsort solution that only does one sort followed by diff and indexing
@MadPhysicist thanks for the details and solution, from a practical perspective, however, it doesn't seem to have a very different timing. Maybe once the array is sorted an additional "sorting" is not really causing much loss
1

If you use np.lexsort, you can get the maximum by index:

sort_idx = np.lexsort(utl.T[::-1])

The differences in the sorted first column are going to tell you which index to grab from the second:

max_idx = np.r_[np.flatnonzero(np.diff(utl[idx, 0])), len(idx) - 1]
min_idx = np.r_[0, np.flatnonzero(np.diff(utl[idx, 0])) + 1]

The results can be extracted immediately:

minima = utl[idx[min_idx], 1]
maxima = utl[idx[max_idx], 1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.