Python 'index 0 is out of bounds for axis 0 with size 0'是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41492288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:03:48  来源:igfitidea点击:

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

pythonnumpyindexingerror-handlingindex-error

提问by Seoyeon Hong

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 want to figure out what this means.. It might be silly to ask this but what do they mean by axis 0 and size 0? index 0 means the first value in the array.. but I can't figure out what axis 0 and size 0 mean.

我是 python 和 numpy 的新手。我运行了一个我写的代码,我收到了这条消息:'index 0 is out of bounds for axis 0 with size 0'没有上下文,我只想弄清楚这意味着什么......问这个可能很愚蠢但是他们所说的轴 0 和尺寸 0 是什么意思呢?索引 0 表示数组中的第一个值。但我无法弄清楚轴 0 和大小 0 是什么意思。

The 'data' is a text file with lots of numbers in two columns.

“数据”是一个文本文件,两列中有很多数字。

x = np.linspace(1735.0,1775.0,100)
column1 = (data[0,0:-1]+data[0,1:])/2.0
column2 = data[1,1:]
x_column1 = np.zeros(x.size+2)
x_column1[1:-1] = x
x_column1[0] = x[0]+x[0]-x[1]
x_column1[-1] = x[-1]+x[-1]-x[-2]
experiment = np.zeros_like(x)
for i in range(np.size(x_edges)-2):
    indexes = np.flatnonzero(np.logical_and((column1>=x_column1[i]),(column1<x_column1[i+1])))
    temp_column2 = column2[indexes]
    temp_column2[0] -= column2[indexes[0]]*(x_column1[i]-column1[indexes[0]-1])/(column1[indexes[0]]-column1[indexes[0]-1])
    temp_column2[-1] -= column2[indexes[-1]]*(column1[indexes[-1]+1]-x_column1[i+1])/(column1[indexes[-1]+1]-column1[indexes[-1]])
    experiment[i] = np.sum(temp_column2)   
return experiment

回答by hpaulj

In numpy, index and dimension numbering starts with 0. So axis 0means the 1st dimension. Also in numpya dimension can have length (size) 0. The simplest case is:

在 中numpy,索引和维度编号从 0 开始。因此axis 0表示第一个维度。同样在numpy一个维度中可以有长度(大小)0。最简单的情况是:

In [435]: x = np.zeros((0,), int)
In [436]: x
Out[436]: array([], dtype=int32)
In [437]: x[0]
...
IndexError: index 0 is out of bounds for axis 0 with size 0

I also get it if x = np.zeros((0,5), int), a 2d array with 0 rows, and 5 columns.

我也得到它 if x = np.zeros((0,5), int),一个 0 行和 5 列的二维数组。

So someplace in your code you are creating an array with a size 0 first axis.

因此,在您的代码中的某个地方,您正在创建一个大小为 0 的第一个轴的数组。

When asking about errors, it is expected that you tell us where the error occurs.

在询问错误时,希望您告诉我们错误发生的位置。

Also when debugging problems like this, the first thing you should do is print the shape(and maybe the dtype) of the suspected variables.

此外,在调试此类问题时,您应该做的第一件事是打印可疑变量的shape(可能还有dtype)。

回答by Daniel Abud

Essentially it means you don't have the index you are trying to reference. For example:

从本质上讲,这意味着您没有要引用的索引。例如:

df = pd.DataFrame()
df['this']=np.nan
df['my']=np.nan
df['data']=np.nan
df['data'][0]=5 #I haven't yet assigned how long df[data] should be!
print(df)

will give me the error you are referring to, because I haven't told Pandas how long my dataframe is. Whereas if I do the exact same code but I DO assign an index length, I don't get an error:

会给我你所指的错误,因为我没有告诉 Pandas 我的数据帧有多长。而如果我执行完全相同的代码但我确实分配了索引长度,则不会出现错误:

df = pd.DataFrame(index=[0,1,2,3,4])
df['this']=np.nan
df['is']=np.nan
df['my']=np.nan
df['data']=np.nan
df['data'][0]=5 #since I've properly labelled my index, I don't run into this problem!
print(df)

Hope that answers your question!

希望这能回答你的问题!

回答by kmario23

This is an IndexErrorin 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.

这是IndexErrorPython 中的一个,这意味着我们正在尝试访问张量中不存在的索引。下面是一个非常简单的例子来理解这个错误。

# 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 arrin place, if we now try to assign any value to some index, for example to the index 0as in the case below

有了这个数组arr,如果我们现在尝试将任何值分配给某个索引,例如分配给索引,0如下例所示

In [16]: arr[0] = 23     

Then, we will get an IndexError, as below:

然后,我们将得到一个IndexError,如下所示:


IndexError                                Traceback (most recent call last)
<ipython-input-16-0891244a3c59> in <module>
----> 1 arr[0] = 23

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

IndexError                                Traceback (most recent call last)
<ipython-input-16-0891244a3c59> in <module>
----> 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 0thposition), which is not there (i.e. it doesn't exist because we have an array of size 0).

原因是我们试图访问一个索引(这里是0位置),它不存在(即它不存在,因为我们有一个 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 0and fix that.

所以,本质上,这样的数组是无用的,不能用于存储任何东西。因此,在您的代码中,您必须遵循回溯并寻找创建大小数组/张量的位置0并修复它。