2

我有一个二维数组(行*列)行表示axis0,列表示aixs1。

例子:

a=np.array([[10,20,30],[40,50,60]])

现在,当我尝试访问该值(沿轴 0 超出范围)时,我正在低于正确的异常。

print(a[-3][-1])

    IndexError: index -3 is out of bounds for `axis 0` with size 2

注意:当我沿着axis1尝试时,它仍然显示相同的axis0,它应该是axis1

print(a[-2][5])
IndexError: index 5 is out of bounds for axis 0 with size 3
4

1 回答 1

3

这是因为您首先将子集a设置为一维向量。

>>> a[-2]
array([10, 20, 30])

>>> np.array([10, 20, 30])[5]
IndexError: index 5 is out of bounds for axis 0 with size 3

但是,如果一次对两个维度进行切片,则会得到预期的错误:

>>> a[-2, 5]
IndexError: index 5 is out of bounds for axis 1 with size 3
于 2021-10-05T07:13:03.973 回答