Skip to content

Instantly share code, notes, and snippets.

@naoyashiga
Last active April 28, 2017 08:54
Show Gist options
  • Save naoyashiga/8f8a215932e881a3f9ec85e45d499e99 to your computer and use it in GitHub Desktop.
Save naoyashiga/8f8a215932e881a3f9ec85e45d499e99 to your computer and use it in GitHub Desktop.
値が大きい上位K件の配列インデックスを取得
#探す対象リスト: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]
@naoyashiga
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment