An alternative solution for Q.89
iamyifan opened this issue · 1 comments
iamyifan commented
- How to get the n largest values of an array (★★★)
hint: np.argsort | np.argpartitionZ = 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:])
rougier commented
Nope, argpartition is better.