For example, given matrix
array([[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[ 0, 1, 2, 3, 4, 5],
[24, 25, 26, 27, 28, 29]])
and top_n=3, it should return
array([[24, 25, 26, 27, 28, 29],
[18, 19, 20, 21, 22, 23],
[12, 13, 14, 15, 16, 17]])
This function should return a np.ndarray of shape (top_n, arr.shape[-1]), given the input 2D matrix arr.
Here's what I tried:
def select_rows(arr, top_n):
"""
This function selects the top_n rows that have the largest sum of entries
"""
sel_rows = np.argsort(-arr,axis=1)[:top_n]
return sel_rows
I also tried:
sel_rows = (-arr).argsort(axis=-1)[:, :top_n]
to no avail.
-is less efficient that slicing the data at the end. For the small sample this isn't an issue, but casting all the values to negative in a large array will be somewhat slower, which is verified with a%%timeittest.