Do you know a pure numpy (i.e. no cython) way to aggregate values of an array into buckets as done in the function below, but without the for loop (for the sake of performance)?
import numpy as np
def aggregate(values, buckets, n_buckets):
aggr = np.empty((n_buckets,))
for bucket in range(n_buckets):
mask = (buckets == bucket)
aggr[bucket] = sum(values[mask]) / np.count_nonzero(mask)
return aggr
values = np.array( [0, 1, 10, 11, 2, 12], dtype=float)
buckets = np.array([0, 0, 1, 1, 0, 1])
n_buckets = 2
print aggregate(values, buckets, n_buckets)
[ 1. 11.]