numpy-100 icon indicating copy to clipboard operation
numpy-100 copied to clipboard

I have a better way to solve exercise 89

Open anthonyduong9 opened this issue 1 year ago • 2 comments

I think

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

or

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

would be better than

https://github.com/rougier/numpy-100/blob/916e1ecf9544719b49ece6fc135b089988b33058/100_Numpy_exercises_with_hints_with_solutions.md#L1084

because the first solution is simpler, and if it's ok to modify the array in place, the second solution is faster and uses less space.

anthonyduong9 avatar Sep 30 '24 23:09 anthonyduong9

It is written that argsort is the slow solution and Z[np.argpartition(-Z,n)[:n]] is faster because of partial ordering.

rougier avatar Oct 21 '24 13:10 rougier

I read that, but either of the solutions I wrote would be a better slow solution, right?

anthonyduong9 avatar Oct 23 '24 06:10 anthonyduong9