python - Numpy 数组索引与其他数组会产生广播错误

标签 python arrays numpy multidimensional-array indexing

我有两个索引数组。

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/

相关文章:

Python - 从文本中删除一些标点符号

python - 我想向 pandas 数据框添加新索引

PHP在关联数组前面加上文字键?

python - 无法在 numpy 数组中插入三个连续的零

Python 将字符串转换为分类 - numpy

python - psutil.WindowsService.username() 返回空白

python - 预测和拟合之间的keras形状不匹配

Javascript - 使用数组传递变量

javascript - 数组在 jQuery 中未按预期运行

python - 如果切片不能解决内存错误,如何合并两个大的 numpy 数组?