Python numpy 2d:如何仅为第二列中允许的值获取第一列中max元素的索引
帮助找到解决问题的高性能方法: 我在神经网络(答案权重)之后有一个结果,答案的类别(相同的len)和当前请求的允许类别:Python numpy 2d:如何仅为第二列中允许的值获取第一列中max元素的索引,python,arrays,numpy,max,masked-array,Python,Arrays,Numpy,Max,Masked Array,帮助找到解决问题的高性能方法: 我在神经网络(答案权重)之后有一个结果,答案的类别(相同的len)和当前请求的允许类别: 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 =
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)
我需要知道max元素的索引(在answers\u weight数组中),但跳过不允许的类别(2,3)
最后,索引必须是=2(“3.0”,因为类别不允许跳过“3.8”)最简单的方法是使用numpy的屏蔽数组根据允许的类别屏蔽权重,然后查找
argmax
:
np.ma.masked_where(~np.isin(answers_category,categories_allowed1),answers_weight).argmax()
#2
使用遮罩的另一种方法(此方法假设唯一的最大重量):
最简单的方法是使用numpy的蒙版_数组根据允许的_类别屏蔽权重,然后查找
argmax
:
np.ma.masked_where(~np.isin(answers_category,categories_allowed1),answers_weight).argmax()
#2
使用遮罩的另一种方法(此方法假设唯一的最大重量):
我还用面具解决了这个问题
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]
我还用面具解决了这个问题
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]