Last active
April 28, 2017 08:54
-
-
Save naoyashiga/8f8a215932e881a3f9ec85e45d499e99 to your computer and use it in GitHub Desktop.
値が大きい上位K件の配列インデックスを取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#探す対象リスト:my_arrayはnumpy | |
#例:上位3件 | |
K = 3 | |
# ソートはされていない上位k件のインデックス | |
unsorted_max_indices = np.argpartition(-my_array, K)[:K] | |
# 上位k件の値 | |
y = my_array[unsorted_max_indices] | |
# 大きい順にソートし、インデックスを取得 | |
indices = np.argsort(-y) | |
# 類似度上位k件のインデックス | |
max_k_indices = unsorted_max_indices[indices] |
Author
naoyashiga
commented
Apr 28, 2017
- numpy argsortを使うと普通の配列よりもソートが早い
- numpy.argsort — NumPy v1.12 Manual
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment