What does 'index 0 is out of bounds for axis 0 with size 0' mean?

后端 未结 3 1201
孤独总比滥情好
孤独总比滥情好 2020-12-15 19:50

I am new to both python and numpy. I ran a code that I wrote and I am getting this message: \'index 0 is out of bounds for axis 0 with size 0\' Without the context, I just w

3条回答
  •  心在旅途
    2020-12-15 19:54

    This is an IndexError in python, which means that we're trying to access an index which isn't there in the tensor. Below is a very simple example to understand this error.

    # create an empty array of dimension `0`
    In [14]: arr = np.array([], dtype=np.int64) 
    
    # check its shape      
    In [15]: arr.shape  
    Out[15]: (0,)
    

    with this array arr in place, if we now try to assign any value to some index, for example to the index 0 as in the case below

    In [16]: arr[0] = 23     
    

    Then, we will get an IndexError, as below:


    IndexError                                Traceback (most recent call last)
     in 
    ----> 1 arr[0] = 23
    
    IndexError: index 0 is out of bounds for axis 0 with size 0
    

    The reason is that we are trying to access an index (here at 0th position), which is not there (i.e. it doesn't exist because we have an array of size 0).

    In [19]: arr.size * arr.itemsize  
    Out[19]: 0
    

    So, in essence, such an array is useless and cannot be used for storing anything. Thus, in your code, you've to follow the traceback and look for the place where you're creating an array/tensor of size 0 and fix that.

提交回复
热议问题