假设我有一个如下所示的数组:
a = np.array([0, 20, 40, 30, 60, 35, 15, 18, 2])
我有一个索引数组,我想在它们之间求平均值:
averaging_indices = np.array([2, 4, 7, 8])
我想要做的是根据averaging_indices数组对数组a的元素进行平均。为了清楚起见,我想取平均值:
np.mean(a[0:2]), np.mean(a[2:4]), np.mean(a[4:7]), np.mean(a[7,8]), np.mean(a[8:])
我想返回一个具有正确尺寸的数组,在本例中为
result = [10, 35, 36.66, 18, 2]
有人能想出一个巧妙的方法来做到这一点吗?我能想到的唯一方法是循环,这是非常反numpy的。
最佳答案
这是一种矢量化方法 np.bincount
-
# Create "shifts array" and then IDs array for use with np.bincount later on
shifts_array = np.zeros(a.size,dtype=int)
shifts_array[averaging_indices] = 1
IDs = shifts_array.cumsum()
# Use np.bincount to get the summations for each tag and also tag counts.
# Thus, get tagged averages as final output.
out = np.bincount(IDs,a)/np.bincount(IDs)
示例输入、输出 -
In [60]: a
Out[60]: array([ 0, 20, 40, 30, 60, 35, 15, 18, 2])
In [61]: averaging_indices
Out[61]: array([2, 4, 7, 8])
In [62]: out
Out[62]: array([ 10. , 35. , 36.66666667, 18. , 2. ])
关于python - 根据另一个数组的索引取一个数组的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35411879/