我有两个 numpy 数组,users
并且dat
. 对于每个用户,users
我需要找到与用户相关的数据dat
并计算唯一值的数量。我需要处理一个案例 wherelen(users)=200000
和len(dat)=2800000
. 目前我没有利用dat
排序的事实,使方法非常慢。我该怎么做呢?
值“其他”dat
仅表明结构化数组中也将存在其他值。
import numpy as np
users = np.array([111, 222, 333])
info = np.zeros(len(users))
dt = [('id', np.int32), ('group', np.int16), ('other', np.float)]
dat = np.array([(111, 1, 0.0), (111, 3, 0.0), (111, 2, 0.0), (111, 1, 0.0),
(222, 1, 0.0), (222, 1, 0.0), (222, 4, 0.0),
(333, 2, 0.0), (333, 1, 0.0), (333, 2, 0.0)],
dtype=dt)
for i, u in enumerate(users):
u_dat = dat[np.in1d(dat['id'], u)]
uniq = set(u_dat['group'])
info[i] = int(len(uniq))
print info