numpy 索引数组抛出越界异常
numpy index array throws out of bounds exception
有人可以指出下面代码中的错误吗?
最后一行一直报错:
tmp = img1[coords]
IndexError: index 409 is out of bounds for axis 0 with size 352
我只是将索引移动 10,所以我不明白我怎么突然在 img1 中越界了
img1 = np.random.randint(0,256, (352,870, 3), dtype=np.uint8)
img2 = np.random.randint(0,256, (44,853, 3), dtype=np.uint8)
coords = np.where(img2[:, :, 2] >= 250)
coords = np.transpose(np.transpose(coords)+(10, 10))
tmp = img1[coords]
不清楚您要实现的目标,但我认为是这样的:tmp = img1[coords[0], coords[1]]
In [1]: x = np.arange(24).reshape(2,3,4)
In [2]: idx = np.where(x%2)
In [3]: idx
Out[3]:
(array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], dtype=int32),
array([0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2], dtype=int32),
array([1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3], dtype=int32))
In [4]: x[idx]
Out[4]: array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23])
In [5]: idx1 = np.transpose(np.transpose(idx))
In [6]: idx1
Out[6]:
array([[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2],
[1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]], dtype=int32)
错误的使用方法idx1
:
In [7]: x[idx1]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-ffe1818b2257> in <module>()
----> 1 x[idx1]
IndexError: index 2 is out of bounds for axis 0 with size 2
正确的方法:
In [8]: x[tuple(idx1)]
Out[8]: array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23])
用 where
对 x
的切片进行测试(我不喜欢假设代码有效):
In [22]: idx = np.where(x[:,:,2]%3)
In [23]: idx
Out[23]: (array([0, 0, 1, 1], dtype=int32), array([0, 2, 0, 2], dtype=int32))
In [24]: idx1 = np.transpose(np.transpose(idx))
In [25]: x[idx]
Out[25]:
array([[ 0, 1, 2, 3],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[20, 21, 22, 23]])
In [26]: x[tuple(idx1)]
Out[26]:
array([[ 0, 1, 2, 3],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[20, 21, 22, 23]])
使用 n 行数组进行索引与使用 n 元组数组进行索引不同。元组的元素应用于连续的维度。数组的元素仅应用于一个维度,即第一个维度。
有人可以指出下面代码中的错误吗? 最后一行一直报错:
tmp = img1[coords]
IndexError: index 409 is out of bounds for axis 0 with size 352
我只是将索引移动 10,所以我不明白我怎么突然在 img1 中越界了
img1 = np.random.randint(0,256, (352,870, 3), dtype=np.uint8)
img2 = np.random.randint(0,256, (44,853, 3), dtype=np.uint8)
coords = np.where(img2[:, :, 2] >= 250)
coords = np.transpose(np.transpose(coords)+(10, 10))
tmp = img1[coords]
不清楚您要实现的目标,但我认为是这样的:tmp = img1[coords[0], coords[1]]
In [1]: x = np.arange(24).reshape(2,3,4)
In [2]: idx = np.where(x%2)
In [3]: idx
Out[3]:
(array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], dtype=int32),
array([0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2], dtype=int32),
array([1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3], dtype=int32))
In [4]: x[idx]
Out[4]: array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23])
In [5]: idx1 = np.transpose(np.transpose(idx))
In [6]: idx1
Out[6]:
array([[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2],
[1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]], dtype=int32)
错误的使用方法idx1
:
In [7]: x[idx1]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-ffe1818b2257> in <module>()
----> 1 x[idx1]
IndexError: index 2 is out of bounds for axis 0 with size 2
正确的方法:
In [8]: x[tuple(idx1)]
Out[8]: array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23])
用 where
对 x
的切片进行测试(我不喜欢假设代码有效):
In [22]: idx = np.where(x[:,:,2]%3)
In [23]: idx
Out[23]: (array([0, 0, 1, 1], dtype=int32), array([0, 2, 0, 2], dtype=int32))
In [24]: idx1 = np.transpose(np.transpose(idx))
In [25]: x[idx]
Out[25]:
array([[ 0, 1, 2, 3],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[20, 21, 22, 23]])
In [26]: x[tuple(idx1)]
Out[26]:
array([[ 0, 1, 2, 3],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[20, 21, 22, 23]])
使用 n 行数组进行索引与使用 n 元组数组进行索引不同。元组的元素应用于连续的维度。数组的元素仅应用于一个维度,即第一个维度。