Hacker News new | ask | show | jobs
by 00ajcr 3032 days ago
There are some nice exercises here, good work.

For question 48 it might be simpler to just write

  np.sort(a)[-5:]
instead of using argsort() and then using fancy indexing. Better yet, use

  np.partition(a, kth=-5)[-5:]
which scales linearly with the size of the array.

Also, the one-hot encoding puzzle (51) would be more efficiently solved using

  (arr[:, None] == np.unique(arr)).view(np.int8)
In general, `for` loops over NumPy arrays should be avoided where at all possible.
1 comments

Thanks for the suggestion, I will factor those in.