【发布时间】:2013-07-19 19:03:29
【问题描述】:
我有一个数组,例如 (2,1000) 形状。我需要沿轴 = 1 获得累积乘积,这不是问题,但如果我的数字低于 1 - 它们很快会变为零,如果它们高于 1 - 它们会很快达到 Inf。问题是,在每次产品操作之后,是否有任何方法可以在没有循环的情况下沿轴 = 0(即按总和)标准化每一列?
a = np.random.randint(1, 10, (2,1000)).astype('float')
p = np.cumprod(a, axis=1)
print p[:,-1]
这给了我 [inf inf]
a = np.random.random((2,1000))
p = np.cumprod(a, axis=1)
print p[:,-1]
这给了我 [0. 0.]
我想要类似的东西 [0.5, 0.5]
这就像现在的部分解决方案:
vars = 100
a = np.random.random((vars, 1000))
p = np.ones((vars, 1))
step_window = 100
step = int(a.shape[1]/step_window)
for i in range(step):
temp = np.cumprod(a[:, i*step_window:(i+1)*step_window], axis=1)
temp[:,-1] /= temp[:,-1].sum()
p *= temp[:,-1].reshape((vars, 1))
p /= p.sum() '
【问题讨论】:
-
您应该将部分解决方案添加到问题中,而不是将其留在评论中。
-
感谢您的建议,已添加
标签: python arrays numpy normalization