简体   繁体   English

numpy 2d:如何仅获取第二列中允许值的第一列中最大元素的索引

[英]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.

相关问题 根据 2D numpy 数组的第二列中的最大值查找 1D numpy 数组 - Finding the 1D numpy array based on the max value in the second column of a 2D numpy array Numpy:获取2D数组最小值的列和行索引 - Numpy: get the column and row index of the minimum value of a 2D array 如何从二维数组+最大值索引中获取每一列的最大值 - how to get the max of each column from an 2d array + index of the max value 从此列的值中检索 2d numpy 数组中的列索引 - Retrieve column index in 2d numpy array from the value of this column 如何按第二列对 numpy 2D 数组的部分进行排序? - How to sort parts of a numpy 2D array by second column? 如何根据第一列获取最大值,然后索引其他列的值,如果条件不匹配,则基于第二列? - How to get the max values based on first column, then index other columns values, if criteria not match, then based on second column? 如何从numpy 2d获取行,其中列值最大的是其他列的组? - How to get rows from numpy 2d where column value is maximum from group by other column? 如何获取二维列表中索引的第二个元素 - How to get the second element of index in 2d list 如何找到第一列具有特定值且第二列具有最大值的数组中的行索引? - how do I find the index of row in an array that the first column has a specific value and the second column has the max value? 如何在numpy中获取2D数组列的辅助因子的视图 - How to get a view on a cofactor of a 2D array column in numpy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM