I do not have access to bottleneck, so in this example I use argsort, but you can use it in the same way:
import numpy as np
N = 4
a = np.random.random(20).reshape(4, 5)
print(a)
a_1d = a.flatten()
idx_1d = a_1d.argsort()[-N:]
x_idx, y_idx = np.unravel_index(idx_1d, a.shape)
for x, y, in zip(x_idx, y_idx):
print(a[x][y])
chw21 source
share