rougier/numpy-100

An alternative solution for Q.89

iamyifan opened this issue · 1 comments

  1. How to get the n largest values of an array (★★★)
    hint: np.argsort | np.argpartition

Z = np.arange(10000)
np.random.shuffle(Z)
n = 5

# Slow
print (Z[np.argsort(Z)[-n:]])

# Fast
print (Z[np.argpartition(-Z,n)[:n]])

np.sort() will be enough:

print(np.sort(Z)[-n:])

Nope, argpartition is better.