我有两个索引数组。
elim=range(130,240)
tlim=range(0,610)
要索引的数组,I
, 原来的形状是 (299, 3800)
当我尝试按如下方式对其进行索引时I[elim,tlim]
我收到以下错误消息。shape mismatch: indexing arrays could not be broadcast together with shapes (110,) (610,)
我没想到会出现这样的错误。有人可以解释这里发生了什么吗?
谢谢!
最佳答案
让我们用一个指定形状的随机数组重现这个例子:
elim=range(0,610)
tlim=range(130,240)
a = np.random.rand(299, 3800)
a[tlim, elim]
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (110,) (610,)
这会引发错误,因为您使用整数索引数组来索引数组,因此 advanced indexing规则将适用。
你应该在这个例子中使用切片
a[130:240,0:610].shape
# (110, 610)
见 Understanding slice notation (NumPy 索引,只是将同一概念扩展到 n 维数组。对于索引列表,不一定可以表示为切片的情况,您有
np.ix_
.有关 numpy 索引的更多信息,this可能有帮助a[np.ix_(tlim, elim)].shape
# (110, 610)
关于python - Numpy 数组索引与其他数组会产生广播错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63831653/