繁体   English   中英

NumPy数组IndexError:索引99超出了大小为1的轴0的范围

[英]NumPy array IndexError: index 99 is out of bounds for axis 0 with size 1

我正在使用Python 3.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)] ,但是我绝对不知道如何修改代码并替换它。

是的,该错误是由于您的线路引起的

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

错误讯息

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

表示您尝试在大小为1的轴0(唯一的轴)上csrf索引99( int(i-1)值为99),因此只能访问的索引为0。

此外,您的示例代码也不是最小,完整和可验证的示例 变量close来自何处?

也许您想在下一行中使用n而不是i

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

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

或者,也许您想预先用值初始化csrf

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM