简体   繁体   中英

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

I am using Python 3.6 and I got a index error when I try to use NumPy array reference.

Here is my code:

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)

But the result is:

 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 

I think the problem is line 64 setcsf = t - csrf[int(i-1)] * sm + csrf[int(i-1)] , but I definitely don't know how to modify the code and replace it.

Yes, the error is due to your line

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

The error message

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

says you tried to access index 99 ( int(i-1) has value 99) of csrf on axis 0 (its only axis) when it only has a size of 1, so the only index you could have accessed would be 0.

Additionally, your example code is not a Minimal, Complete, and Verifiable example . Where does variable close come from?

Maybe you want to use n instead of i like in the following line?

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

That would make sense, since n-1 will always reference the index of the former loop run. You wouldn't get IndexError .

Or maybe you want to initialize csrf with values beforehand?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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