Get the indices of N highest values in an ndarray
You can use numpy.argpartition on flattened version of array first to get the indices of top k items, and then you can convert those 1D indices as per the array’s shape using numpy.unravel_index: >>> arr = np.arange(100*100*100).reshape(100, 100, 100) >>> np.random.shuffle(arr) >>> indices = np.argpartition(arr.flatten(), -2)[-2:] >>> np.vstack(np.unravel_index(indices, arr.shape)).T array([[97, 99, 98], [97, 99, 99]]) … Read more