根据索引计算Numpy Array中唯一值的和



我的任务是高效而美观地计算总和。我有两个数组

dst = [[1 2 3 4 5]
[2 3 4 5 6]
[1 1 2 2 3]
[7 8 9 9 3]]

ids = [[1 1 2 1 3]
[2 2 1 1 3]
[3 3 2 1 1]
[2 2 1 3 3]
[1 2 3 2 1]]

对于dst中的每一行,我需要计算id中唯一元素的sum,并返回sum的最大值和索引数。

Example for first row: I have 3 unique number in ids in first row [1,2,3].
indices for 1 = [0,1,3]  for 2 = [2] for 3 = [4] 
For 1: sum is sum of dst[0][0] + dst[0][1] + dst[0][3] = 1 + 2 + 4 = 7. 
For 2: sum is dst[0][2] = 3 
For 3: sum is dst[0][4] = 5. 
max(sum) = 7
number = 3
Total: [3,7] - for first row

我不知道如何使用Numpy函数有效而简单地做到这一点。我使用了经典的python,但是这个解决方案太慢了。

您可以尝试像这样获得唯一索引:

indices = [np.unique(row) for row in ids]

,然后计算总和:

sums = [np.sum(dst[i][indices[i]]) for i in range(len(dst))] 

最新更新