NumPy数组索引器错误:索引99超出大小为1的轴0的界限

2024-05-23 20:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python3.6,当我试图使用NumPy数组引用时,遇到了一个索引错误。在

这是我的代码:

import numpy as np

length1 = 35
length2 = 20
siglength = 10

csrf = np.array([])

sm = 2.0 / 35
sm2 = 2.0 / 20

for n in range(0, 198) :

    if n == 0 :
        i = 100
        t = i - 100
        setcsf = t - 0 * sm + 0
        csrf = np.append(csrf, setcsf)

    else :
        i = (close[n] / close[int(n+1)]) * 100
        t = i - 100
        setcsf = t - csrf[int(i-1)] * sm + csrf[int(i-1)]
        csrf = np.append(csrf, setcsf)
print(csrf)

但结果是:

Traceback (most recent call last):
File "test.py", line 64, in <module>
setcsf = t - csrf[int(i-1)] * sm + csrf[int(i-1)]
IndexError: index 99 is out of bounds for axis 0 with size 1

我认为问题出在第64行setcsf = t - csrf[int(i-1)] * sm + csrf[int(i-1)],但我绝对不知道如何修改和替换代码。在


Tags: 代码inimportnumpyforclose错误np
1条回答
网友
1楼 · 发布于 2024-05-23 20:06:37

是的,错误是由于你的线路

setcsf = t - csrf[int(i-1)] * sm + csrf[int(i-1)]

错误消息

IndexError: index 99 is out of bounds for axis 0 with size 1

表示您试图在轴0(其唯一的轴)上访问索引99(int(i-1)的值99)为csrf,而它的大小只有1,因此您可以访问的唯一索引将是0。在

另外,示例代码不是Minimal, Complete, and Verifiable example。变量close来自哪里?在

也许你想用n而不是i,就像下面这一行?在

^{pr2}$

这是有意义的,因为n-1将始终引用前一个循环运行的索引。你不会得到IndexError。在

或者您想预先用值初始化csrf?在

csrf = np.array([0] * 198)

相关问题 更多 >