I want to write a function func() like this:
import numpy as np
a = np.array([0, 1, 1, 0, 0, 2, 3, 0, 1])
b = np.array([1, 1, 4, 5, 1, 4, 1, 9, 1])
ab = np.array([[0, 1, 1, 0, 0, 2, 3, 0, 1],
[1, 1, 4, 5, 1, 4, 1, 9, 1]])
grp = np.where(a == 0)[0] # [0, 3, 4, 7]
def func():
# return the index of np.max(b[grp]) in a, here it is 7
np.argmax(b[grp]) can only return return the index of np.argmax(b[grp]) in a[grp], here it is 3
I know that dict can do this but numpy seems not supporting dict sorting and spliting like b[grp]
Any help is welcome, thanks!
>Solution :
You can use the argmax of filtered b to index the grp
$ grp[np.argmax(b[grp])]
7