我正在处理一个非常大的数据集(大约7500万个条目),并且试图将运行我的代码所需的时间大大缩短(现在循环需要几天),并且保持极低的内存使用率。
我有两个相同长度的numpy数组(clients
和units
)。我的目标是获取第一个列表(clients
)中出现值的每个索引的列表,然后在第二个列表中的每个索引处找到条目的总和。
这是我尝试过的(np
是以前导入的numpy库)
# create a list of each value that appears in clients
unq = np.unique(clients)
arr = np.zeros(len(unq))
tmp = np.arange(len(clients))
# for each unique value i in clients
for i in range(len(unq)) :
#create a list inds of all the indices that i occurs in clients
inds = tmp[clients==unq[i]]
# add the sum of all the elements in units at the indices inds to a list
arr[i] = sum(units[inds])
有谁知道一种方法,可以让我找到这些总和而无需遍历其中的每个元素unq
?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句