Index Out of Bounds Error:Python

IndexError: index out of bounds

From a quick look, it seems the problem is the for loop, instead of using for i in [N[0]], are you sure it's not for i in range(N[0])?

for i in [N[0]] is rather redundant because the list only contains one element, ie. N[0], so the list of size 800 becomes out of index when you try to seek dydt[800] <-- as the max index is 799.

Like so:

In [9]: N
Out[9]: [800, 400, 800]

In [10]: for i in [N[0]]: # ie. i == N[0] == 800
....: dydt[i] = 0
....:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-10-04daa45ec403> in <module>()
1 for i in [N[0]]: # ie. i == N[0] == 800
----> 2 dydt[i] = 0
3

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

In [11]: for i in range(N[0]):
....: dydt[i] = 0
....:

Index out of bounds error : Python

numpy linspace official documentation

xn = np.linspace(0,Nx,2000)

in here 0 is start value, Nx is end value and 2000 is the number of values to be generated.

returned object of linspace is something like :

[0, 0.5, 1, ... , ] #2000 elements i.e. a row containing 2000 elements

x=data[:,0]
x is something like :

[[1],[2],[3],[4], .... ,[]] #column vector with number of elements in text data

now row size of x can not be greater than 1 ie index has to be less than 1 or equal to 0

and you can't compare these two arrays

[ ... ] and [[], [], [], [], ... ]

are incomparable (you can compare the first element though.)

you could use this : reshape

refer to this answer : What does -1 mean in numpy reshape?

row_x = x.reshape(-1)

will give you a row vector.

and you can convert to column format by:

col_x = row_x.reshape(-1,1)

I would appreciate edits as I am currently at work and thus wrote the answer in hurry.

Index out of bounds error that I cannot resolve

If you want to access one next element in an array using index while iterating through the array, you would need to iterate up to the second last element. Here, you are using rooms[r][c+1] and rooms[r+1][c] considering r and c as indices. While in the last iteration, your index goes out of bounds. So, here you'd need to iterate the loop to the second last index. Try the following code:

import random
import numpy as np

class Room:
def __init__(self, name, contents):
self.name = name
self.contents = contents

rooms = np.zeros((10, 10))

emptyRooms = []

halfHeight = int(len(rooms[1]) / 2)
halfWidth = int(len(rooms[0]) / 2)

rooms[halfWidth][halfHeight] = 1

for r in range(len(rooms) - 1):
for c in range(len(rooms) - 1):
if rooms[r][c] == 1:
if rooms[r][c-1] != 1:
rooms[r][c-1] = 1
if rooms[r][c+1] != 1:
rooms[r][c+1] = 1
if rooms[r-1][c] != 1:
rooms[r-1][c] = 1
if rooms[r+1][c] != 1:
rooms[r+1][c] = 1

print(rooms)

Here I'm initializing the loop up to range(len(rooms) - 1) instead of range(len(rooms)). I hope it will resolve the issue.

Getting following error in python: index 100 is out of bounds for axis 0 with size 100. Went through other solutions but I found it hard to understand

It looks like you're trying to index a numpy array. Arrays, and just about everything in python and programming in general, are 0-indexed. That means their indices start at 0 instead of 1, which means that the maximum index in an array with 100 items is 99.

Python numpy array: Index error, Index out of bounds

In numpy you can index arrays of size N up to index N-1 (along a given axis), otherwise you will get the IndexError you are seeing. In order to check how high can you go with an index, you can print target_q.shape. In your case it will tell you (10, 1), which means that if you index target_q[i, j], then i can be maximally 9 and j can be maximally 0.
What you do in your line target_q[batch_index, actions] is you insert actions as so called fancy indexing on the second position (j) and actions is full of ones. Thus, you are trying to many times index with 1, whereas the highest allowed index value is 0.
What would work would be:

import numpy as np

batch_size = 10
target_q = np.ones((10, 1))
# changed to zeros below
actions = np.zeros((10, ), dtype=int)
batch_index = np.arange(batch_size, dtype=np.int32)
print(actions)
print(target_q.shape)
print(target_q[batch_index, 0])
print(target_q[batch_index, actions])

that prints:

[0 0 0 0 0 0 0 0 0 0]
(10, 1)
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]


Related Topics



Leave a reply



Submit