假设我有这样一个数组:
a = np.array([0, 20, 40, 30, 60, 35, 15, 18, 2])
我有一个指数数组,我想平均:
averaging_indices = np.array([2, 4, 7, 8])
我要做的是根据数组的平均值来平均数组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]
有人能想出一个巧妙的方法吗?我能想象的唯一方法就是循环,这是非常反努比的。你知道吗
这里有一个向量化的方法^{} -
样本输入,输出-
相关问题 更多 >
编程相关推荐