Skip to content

Instantly share code, notes, and snippets.

@yang-zhang
Created June 1, 2018 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yang-zhang/215e567c8c3ade431b701bf80eb5d3d6 to your computer and use it in GitHub Desktop.
Save yang-zhang/215e567c8c3ade431b701bf80eb5d3d6 to your computer and use it in GitHub Desktop.
Get indices of n largest elements in a numpy array
# https://stackoverflow.com/questions/6910641/how-to-get-indices-of-n-maximum-values-in-a-numpy-array
import numpy as np
top_n = 3
arr = np.array([1, 3, 2, 4, 5])
arr.argsort()[-top_n:][::-1]
# array([4, 3, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment