I have two numpy arrays, users
and dat
. For each user in users
I need to find the data related to the user in dat
and count the number of unique values. I need to process a case where len(users)=200000
and len(dat)=2800000
. Currently I am not exploiting the fact that dat
is sorted, making the method very slow. How do I do this?
The value 'other' in dat
merely shows that other values will be present in the structured array as well.
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