Translate

What error is 'index ○○ is out of bounds for axis ○ with size ○'

 When you often run a python script using spyder or something like that, you run into all sorts of errors.


Amai also always spits out error codes in python, debugging and creating analysis codes, but error codes are not easy to remember no matter how many times you make them happen.


Sweetness usually uses the for loop a lot, but here is an error code that I often come across these days,

The error code is "index ○○ is out of bounds for axis ○ with size ○".

This time, as a reminder, I would like to summarize why this error occurs and what can be done to solve it.


index ○○ is out of bounds for axis ○ with size ○ is an error when specifying a range of numpy



If you read through the error "index ○○ is out of bounds for axis ○ with size ○" often, this is what it says. The number you specified as the range of the index has exceeded the range of the matrix for that data.

The nuance is like this. In other words, the value you specified when you extracted the numpy matrix is beyond the range of the actual data, so it cannot be executed. I understood it to be saying, "The value you specified in the numpy matrix extraction is beyond the range of the actual data and cannot be executed.

In my case, it seems that I had exceeded the range of values specified in the for loop when I noticed it.

In my common python code, I often create a numpy matrix of values to be specified as indexes in advance at the stage before the for loop, and then specify them sequentially in the for loop, starting from the first row of the matrix.

In such cases, the number of iterations of the for loop is usually wrong, and the error "index ○○ is out of bounds for axis ○ with size ○" seems to be caused by trying to specify the value of a row that does not exist.

If it is happening inside the for loop, check the number of iterations of the for loop.

Assuming that the "index ○○ is out of bounds for axis ○ with size ○" error is happening in the for loop, I think the first thing to check is the number of iterations of the for loop.

I also fix the code here and it works fine most of the time.

One area that I personally think is important in figuring out that the for loop is the cause is to identify how many times it is happening in the for loop.

for i in range..... You would write a for loop like this, but check to see where the value of i that you are using as the variable stops. In most cases, the for loop stops at the last row of the matrix where the value of i is determined outside the loop.

If it stops in the middle, then the problem may be with the matrix outside the loop itself.

In any case, the key to solving "index ○○ is out of bounds for axis ○ with size ○" is to use the two points mentioned above in the python code.

Specifying a range of matrices in a for loop is error prone.

Python separates the for loop with a hierarchy of intents, so it is easy to understand visually, but even in python, the for loop is error-prone. In addition to "index ○○ is out of bounds for axis ○ with size ○," various other errors can occur.

The key to deciphering errors is to identify the number of times the error occurs in the for loop, whether the error originates from the original data or from the part that specifies the number of times, and to deal with the error.

However, if you can master the for loop, not only the readability of your python scripts, but also your work efficiency will improve tremendously, so I feel that the shortcut to progress is to learn to use it as an ally (although I am not very good with sweetness and cannot say that I have mastered it very well.