Note

Numpy (48) 본문

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]

'Numpy' 카테고리의 다른 글

Numpy (50)  (0) 2022.09.16
Numpy (49)  (0) 2022.09.15
Numpy (47)  (0) 2022.09.13
Numpy (46)  (0) 2022.09.12
Numpy (45)  (0) 2022.09.11
Comments