我是 python 和 numpy 的新手。我运行了一段我写的代码,我收到了这条消息:’index 0 is out of bounds for axis 0 with size 0’ 在没有上下文的情况下,我只想弄清楚这意味着什么。问这个问题可能很愚蠢但是轴 0 和大小 0 是什么意思?索引 0 表示数组中的第一个值..但我无法弄清楚轴 0 和大小 0 是什么意思。
“数据”是一个文本文件,在两列中有很多数字。
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
原文由 Seoyeon Hong 发布,翻译遵循 CC BY-SA 4.0 许可协议
在
numpy
中,索引和维度编号从 0 开始。因此axis 0
表示第一个维度。同样在numpy
中,维度的长度(大小)可以为 0。最简单的情况是:我也得到它 if
x = np.zeros((0,5), int)
,一个 0 行和 5 列的二维数组。所以在你的代码中的某个地方,你正在创建一个第一轴大小为 0 的数组。
在询问错误时,希望您告诉我们错误发生的位置。
此外,在调试此类问题时,您应该做的第一件事是打印可疑变量的
shape
(也许还有dtype
)。适用于
pandas
pandas
, when sending aSeries
orDataFrame
to anumpy.array
, as with the following:pandas.Series.values
或pandas.Series.to_numpy()
或pandas.Series.array
pandas.DataFrame.values
或pandas.DataFrame.to_numpy()
解决错误:
try-except
块if x.size != 0: