1
我有三个六维Numpy
阵列形式如何广播numpy的2D阵列到6D阵列
Weights
MyValue
WeightedValue = Weight * MyValue
的(有关python 3.4运行)我想确定的加权平均MyValue
加权为Weights
'axis-j
(其范围可以从0到4),除了轴5
是恒定的。
(所以当j=2
时,我们在0,1,3,
和4
之间取平均值)。
然后我打算借此平均和由Weights
相乘以及WeightedValue
我打算做此减去所述产品是
NewArray = WeightedValue - Weight * fn(Weighted Value, Weights)
NewMyValue = NewArray/Weights
的fn()
将是MyValue
平均定义为:
the sum of Weighted Value using 4 axes (all except j and 5)
< divided by >---------------------------------------------------------------
the sum of Weights across 4 axes (all except j and 5)
我的问题如下:
平均值是2D阵列,我需要fn()
以生成6D阵列,即广播的2D-结果比其他4维
我可以作为最后的手段创造了一系列循环遍历轴j
和轴5
。对于第二轴(J = 1)循环将如下
import numpy as np
result = np.zeros((dim0,dim1,dim2,dim3,dim4,dim5))
for var1 in range(dim1):
for var5 in range(dim5):
result[:,dim1,:,:,:,dim5] = AverageValue[dim1,dim5]
但我希望有一个更直接和一般的方式
考虑添加单独运行的示例代码。如果你知道如何用循环做到这一点,使用它们,我们可以帮助你删除它们。看看['如何问一个好问题'](http://stackoverflow.com/help/how-to-ask)。 – farenorth 2014-10-03 21:56:15
我们可以运行的一个小例子以及您想要的输出结果可能会非常有用。这样,潜在的回答者就能确切地知道你想要做什么。 – 2014-10-03 22:14:45