Numpy
Numpy (48)
알 수 없는 사용자
2022. 9. 14. 19:25
728x90
How to get the positions of top n values from a numpy array?
# Input
np.random.seed(100)
a = np.random.uniform(1,50, 20)
# 1
print(a.argsort())
# output
[18 7 3 10 15]
# 2
np.argpartition(-a, 5)[:5]
# output
[15 10 3 7 18]
# Below methods will get you the values.
# 1:
a[a.argsort()][-5:]
# 2:
np.sort(a)[-5:]
# 3:
np.partition(a, kth=-5)[-5:]
# 4:
a[np.argpartition(-a, 5)][:5]