Skip to content

[ENH] Use np.argpartition for efficient top-k selection instead of np.argsort #2805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 26, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions aeon/similarity_search/series/_commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ def _extract_top_k_from_dist_profile(
top_k_distances = np.full(k, np.inf, dtype=np.float64)
ub = np.full(k, np.inf)
lb = np.full(k, -1.0)
# Could be optimized by using argpartition
sorted_indexes = np.argsort(dist_profile)

k = min(k, len(dist_profile))
partitioned = np.argpartition(dist_profile, k)[:k]
sorted_indexes = partitioned[np.argsort(dist_profile[partitioned])]
_current_k = 0
if not allow_trivial_matches:
_current_j = 0
Expand All @@ -165,8 +167,7 @@ def _extract_top_k_from_dist_profile(
break
_current_j += 1
else:
_current_k += min(k, len(dist_profile))
dist_profile = dist_profile[sorted_indexes[:_current_k]]
dist_profile = dist_profile[sorted_indexes]
dist_profile = dist_profile[dist_profile <= threshold]
_current_k = len(dist_profile)

Expand Down
Loading