'index 0 is out of bounds for axis 0 with size 0'是什么意思? [英] What does 'index 0 is out of bounds for axis 0 with size 0' mean?

查看:50
本文介绍了'index 0 is out of bounds for axis 0 with size 0'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 python 和 numpy 的新手.我运行了一个我写的代码,我收到了这条消息:'索引 0 超出轴 0 的范围,大小为 0'没有上下文,我只想弄清楚这意味着什么..问这个可能很愚蠢,但是轴 0 和大小 0 是什么意思?索引 0 表示数组中的第一个值.但我无法弄清楚轴 0 和大小 0 是什么意思.

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.

数据"是一个文本文件,两列中有很多数字.

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

推荐答案

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

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

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

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

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

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

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

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

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

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

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

  • 使用 pandas 的用户在将 SeriesDataFrame 发送到 numpy.array 时可能会发生同样的错误代码>,如下所示:
    • The same error can occur for those using pandas, when sending a Series or DataFrame to a numpy.array, as with the following:
      • pandas.Series.values or pandas.Series.to_numpy() or pandas.Series.array
      • pandas.DataFrame.values or pandas.DataFrame.to_numpy()
      1. 使用 try-except
      2. 验证数组的大小不为0
        • 如果 x.size != 0:

      这篇关于'index 0 is out of bounds for axis 0 with size 0'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆