Reputation: 23
Help find a high-performance way to solve the problem: I have a result after neural-network(answers_weight), a category for answers(same len) and allowed categories for current request:
answers_weight = np.asarray([0.9, 3.8, 3, 0.6, 0.7, 0.99]) # ~3kk items
answers_category = [1, 2, 1, 5, 3, 1] # same size as answers_weight: ~3kk items
categories_allowed1 = [1, 5, 8]
res = np.stack((answers_weight, answers_category), axis=1)
I need to know the index(in answers_weight array) of max element, but skip not allowed categories(2,3).
In final, index must be = 2("3.0", because "3.8" must be skipped as not-allowed by category)
Upvotes: 1
Views: 86
Reputation: 12407
The easiest way would be to use numpy's masked_arrays to mask your weights according to allowed_categories and then find argmax
:
np.ma.masked_where(~np.isin(answers_category,categories_allowed1),answers_weight).argmax()
#2
Another way of doing it using masks (this one assumes unique max weight):
mask = np.isin(answers_category, categories_allowed1)
np.argwhere(answers_weight==answers_weight[mask].max())[0,0]
#2
Upvotes: 3
Reputation: 7676
I also solved this problem using a mask
inds = np.arange(res.shape[0])
# a mask is an array [False True False False True False]
mask = np.all(res[:,1][:,None] != categories_allowed1,axis=1)
allowed_inds = inds[mask]
# max_ind is not yet the real answer because the not allowed values are not taken into account
max_ind = np.argmax(res[:,0][mask])
real_ind = allowed_inds[max_ind]
Upvotes: 1