I have a 2D array and each row has 3 elements.
I want to use NumPy for finding the unique values with respect to the first two components of the rows. In addition, for all of the elements which appear more than once I want to keep the one having the minimum value for the third components among all the one with the same first and second components.
I am only coming up with clumsy ideas and cannot figure out how this should be done.
Try:
# sort the array by the last column
orders = np.argsort(arr[:,-1])
# rearrange the array increasingly by last column
arr = arr[orders]
# use `unique` to find the unique pairs
# `return_index` option returns the indexes of the first occurrence for each pair
# since last column is increasing, first instance means minimum value
uniques, indexes = np.unique(arr[:,:-1], axis=0, return_index=True)
# extract the first instances together with the last column
out = arr[indexes]
Example data:
np.random.seed(1)
arr = np.random.randint(0,2, (10,3))
arr[:,-1] = np.random.randint(0,5,10)
Output:
array([[0, 0, 1],
[0, 1, 0],
[1, 0, 1],
[1, 1, 1]])