Assuming an (1-d) array, is it possible to calculate the average on given groups of diifferent size without looping? Instead of
avgs = [One_d_array[groups[i]].mean() for i in range(len(groups))]
Something like
avgs = np.mean(One_d_array, groups)
Basically I want to do this:
M = np.arange(10000)
np.random.shuffle(M)
M.resize(100,100)
groups = np.random.randint(1, 10, 100)
def means(M, groups):
means = []
for i, label in enumerate(groups):
means.extend([M[i][groups == j].mean() for j in set(p).difference([label])])
return means
This runs at
%timeit means(M, groups)
100 loops, best of 3: 12.2 ms per loop
Speed up of 10 times or so would be already great