在 Numpy 中计算累积收益,数组索引问题
我是使用 numpy 类的新手,并且在操作数组内容时遇到问题。这是代码:
# finance equation to apply to each element of array
for row in cum_ret:
for col in row:
if sum(row)!=0:
row[col] = prev_row[col]*(1+row[col])
else:
row[col] = 1
cum_ret[row][col] = row[col]
prev_row = row
# see changed contents
for row in cum_ret:
print row
现在我收到一条错误消息,指出使用的数组索引必须是整数或布尔类型。我明白了,因为“行”值也是一个数组,因此它无法索引数组对象。那么执行此操作的正确语法是什么,或者它们是我应该使用的方法?
预先感谢
cum_ret 数组是 float64s 的 2d ndarray,是我要修改的数组。 这是输出的一个简短片段:
[[ 0. 0. 0. 0. 0. ]
[ 0.00046187 0.00836672 0.00020435 -0.00048292 0.00342209]
[-0.07633505 -0.00514199 -0.04133778 -0.02450642 -0.01865075]
...,
[ 0.01229435 0.00175341 0.00709808 0.00213371 0.0061171 ]
[-0.0118614 -0.00994933 -0.00557095 -0.00141945 -0.00347423]
[ 0.01214725 -0.00502466 0.00537611 -0.00035537 -0.00101685]]
这是发生的情况:
Traceback (most recent call last):
File "qstk1.py", line 37, in <module>
cum_ret[row][col] = row[col]
IndexError: arrays used as indices must be of integer (or boolean) type
I am new to using the numpy class and I am having problems with manipulating the contents of the array. Here is the code:
# finance equation to apply to each element of array
for row in cum_ret:
for col in row:
if sum(row)!=0:
row[col] = prev_row[col]*(1+row[col])
else:
row[col] = 1
cum_ret[row][col] = row[col]
prev_row = row
# see changed contents
for row in cum_ret:
print row
Now I am getting an error saying that array indices used must be of integer or boolean type. I get it because the 'row' value is also an array so it can't index an array object. So what is the correct syntax for doing this, or is their a method I am supposed to use?
Thank in advance
The cum_ret array is a 2d ndarray of float64s and is the array I want to modify.
Here is a short snippet of the output:
[[ 0. 0. 0. 0. 0. ]
[ 0.00046187 0.00836672 0.00020435 -0.00048292 0.00342209]
[-0.07633505 -0.00514199 -0.04133778 -0.02450642 -0.01865075]
...,
[ 0.01229435 0.00175341 0.00709808 0.00213371 0.0061171 ]
[-0.0118614 -0.00994933 -0.00557095 -0.00141945 -0.00347423]
[ 0.01214725 -0.00502466 0.00537611 -0.00035537 -0.00101685]]
And here is were it is occurring:
Traceback (most recent call last):
File "qstk1.py", line 37, in <module>
cum_ret[row][col] = row[col]
IndexError: arrays used as indices must be of integer (or boolean) type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 cum_ret 是数组,则可以使用 numpy.sum(cum_ret, axis=1) 获取行总和和 numpy.sum(cum_ret, axis=1) != 0< /code> 立即生成对整个数组的测试。然后您可以使用 numpy.select() 来应用您的条件。
如果您在循环的开头放置一个
print row
语句,您会注意到它不是整数,而是一个 numpy 数组...这会导致您的错误。顺便说一句,您不需要任何此类循环来执行此操作。另一个考虑因素:第一行会发生什么?什么被认为是前一行?
编辑:
阅读您的评论后,我认为您想要这样的东西:
不需要循环,也不需要检查零行的条件。
If cum_ret is the array, you can use
numpy.sum(cum_ret, axis=1)
to get the row-sum andnumpy.sum(cum_ret, axis=1) != 0
to generate your test on the whole array at once. Then you can usenumpy.select()
to apply your conditions.If you put a
print row
statement at the beginning of your loop you will notice that it is not integer, rather it is a numpy array... That is causing your error. BTW, you shouldn't need any such looping to perform this operation.Another consideration: what happens on the first row? What is considered the previous row?
EDIT:
After reading your comments, I think you want something like this:
No looping required, and no need to check your condition for rows of zero.