gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:55:51 24 4
gpt4 key购买 nike

我有两个索引数组。

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/

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