gpt4 book ai didi

python : "IndexError: invalid index into a 0-size array"

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:28 28 4
gpt4 key购买 nike

显然,有很多线程与索引错误有关。但是我找不到可以帮助我的人。

我使用 numpy.loadtxt 读取函数 f(a,b)。

a, b, f = np.loadtxt(filename, delimiter=' ', usecols=(0,1,2), unpack=True)

为了获得二维绘图,我在另一个线程中找到了一个解决方案,您可以在其中对 (a,b) 平面进行光栅化并将一个整数元组 (ida, idb) 与每个点相关联。

ncols, nrows = np.round((a.max() - a.min()) / da).astype(np.int) , np.round((b.max() - b.min()) / db).astype(np.int)
ida = np.round((a - a.min()) / da - 1).astype(np.int)
idb = np.round((b - b.min()) / db - 1).astype(np.int)

然后将f放入a格中。

grid = np.empty((nrows, ncols), dtype=np.float)
grid[idb, ida] = f

这对几个数据文件很有效。但是现在,突然没有了。相反,它在到达 grid[ida,idb] 行时报告标题中的错误。所以我在数据文件中寻找错误,但我找不到任何错误。我试过了

print ida.shape
print idb.shape
print f.shape

返回

(3107,)
(3107,)
(3107,)

所以所有数组的大小都匹配得很好,尤其是它们都不是空数组。我在这里缺少什么?

谢谢,爱丽丝

最佳答案

感谢 seberg 的指点,我发现了错误(我也找到了旧的 stackoverflow 帐户):

a, b, f = np.loadtxt(filename, delimiter=' ', usecols=(0,1,2), unpack=True)

我从文件中导入了错误的列。一个碰巧是不变的。所以我得到了 nrows=0。网格是 0 大小的数组。

对不起...

关于 python : "IndexError: invalid index into a 0-size array",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20441335/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com