[英]numpy 2d: How to get the index of the max element in the first column for only the allowed value in the second column
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),答案类别(相同长度)和当前请求的允许类别之后,我有一个结果:
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).我需要知道最大元素的索引(在answers_weight数组中),但跳过不允许的类别(2,3)。
In final, index must be = 2 ("3.0", because "3.8" must be skipped as not-allowed by category)最后,index 必须 = 2 (“3.0”,因为“3.8”必须被跳过,因为类别不允许)
The easiest way would be to use numpy's masked_arrays to mask your weights according to allowed_categories and then find argmax
:最简单的方法是使用 numpy 的 masked_arrays 根据 allowed_categories 屏蔽您的权重,然后找到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
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.