你是否知道一种纯粹的numpy(即没有cython)方法将数组的值聚合到桶中,如下面的函数所示,但没有for循环(为了性能)?
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。]