indexError:形状不匹配:索引阵列无法与形状一起播放(2,)(3,)

发布于 2025-02-04 15:30:29 字数 975 浏览 2 评论 0 原文

我有一个 np.ndarray Shape (5,5,5,2,2,2,10,8)命名 table 。我可以像这样成功地将其切成薄片:

table[4, [0, 1], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)]
table[4, [0, 1], 1, 1, 1, [0, 2], slice(0, 8, None)]

但是由于某种原因,当我尝试为尺寸5(长度10)指定三个值时:

table[4, [0, 1], 1, 1, 1, [0, 2, 6], slice(0, 8, None)]

我得到:

>>> IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,) 

相同的是:

table[4, [0, 1, 4], 1, 1, 1, [0, 2], slice(0, 8, None)]

这不会发生:

table[4, [0, 1, 4], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)]
table[4, [1, 0, 4], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)]

哪个输出正确的结果。

我试图在广播上阅读类似的问题,但是我仍然感到困惑为什么 numpy 无法理解此切片符号。当我沿着轴上已经有另一个数组时,当我沿着轴上拿出两个以上的点以将其切成薄片时,为什么它会感到困惑?

I have an np.ndarray of shape (5, 5, 2, 2, 2, 10, 8) named table. I can succesfully slice it like this:

table[4, [0, 1], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)]
table[4, [0, 1], 1, 1, 1, [0, 2], slice(0, 8, None)]

But for some reason when I try to specify three values for dimension 5 (of length 10) like this:

table[4, [0, 1], 1, 1, 1, [0, 2, 6], slice(0, 8, None)]

I get:

>>> IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,) 

The same is for:

table[4, [0, 1, 4], 1, 1, 1, [0, 2], slice(0, 8, None)]

This does not happen with:

table[4, [0, 1, 4], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)]
table[4, [1, 0, 4], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)]

which output the correct result.

I tried to read similar questions here on broadcasting but I was still confused why Numpy can't make sense of this slice notation. Why does it act all puzzled when I give it more than two points along an axis to slice with when there's already another array in the indices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2025-02-11 15:30:29
In [219]: table = np.zeros((5, 5, 2, 2, 2, 10, 8),int)    
In [220]: table.shape
Out[220]: (5, 5, 2, 2, 2, 10, 8)

您使用 slice 而不是的事实并不重要;同样的是,不必指定尾随的切片。

In [221]: table[4, [0, 1], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)].shape
Out[221]: (2, 10, 8)

这具有高级索引数组/长度2的列表 - 其他维度是标量或切片。因此它们消失了或“通过”。

In [222]: table[4, [0, 1], 1, 1, 1, [0, 2], slice(0, 8, None)].shape
Out[222]: (2, 8)

在这里,您有两个高级索引列表 - 长度2,因此它们一起“广播”以选择2个值(我认为这是一种“对角线”)。

In [223]: table[4, [0, 1, 4], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)].shape
Out[223]: (3, 10, 8)

与以前相同,但有3个列表。

但是,当两个列表具有不同的长度时,您会遇到错误:

In [225]: table[4, [0, 1], 1, 1, 1, [0, 2, 6], slice(0, 8, None)].shape
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [225], in <cell line: 1>()
----> 1 table[4, [0, 1], 1, 1, 1, [0, 2, 6], slice(0, 8, None)].shape

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,) 

如果一个列表为(2,1),则可以工作 - 它在一个维度中选择2个,而另一个列表则在另一个维度中选择:

In [226]: table[4, [[0], [1]], 1, 1, 1, [0, 2, 6], slice(0, 8, None)].shape
Out[226]: (2, 3, 8)

索引中的“广播”遵循相同的规则就像添加(或乘法)数组时一样。

(2,) with (2,) => (2,)
(2,1) with (3,) => (2,3)
(2,) with (3,) error

In [227]: np.ones(2)+np.ones(2)
Out[227]: array([2., 2.])

In [228]: np.ones(2)+np.ones(3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [228], in <cell line: 1>()
----> 1 np.ones(2)+np.ones(3)

ValueError: operands could not be broadcast together with shapes (2,) (3,) 

In [229]: np.ones((2,1))+np.ones(3)
Out[229]: 
array([[2., 2., 2.],
       [2., 2., 2.]]

编辑

查看更简单的2D数组:

In [261]: arr = np.arange(6).reshape(2,3)

In [262]: arr
Out[262]: 
array([[0, 1, 2],
       [3, 4, 5]])

如果我用2(2,)阵列索引,则获得2个值:

In [264]: arr[np.array([0,1]), np.array([1,2])]
Out[264]: array([1, 5])

但是如果我用(2,1)和(2,)索引,我会得到(2,2)形状结果。请注意[1,5]值在哪里:

In [265]: arr[np.array([0,1])[:,None], np.array([1,2])]
Out[265]: 
array([[1, 2],
       [4, 5]])

ix _ 是用于构建此类“笛卡尔”索引数组的方便工具。例如,我得到的3个列表:

In [266]: np.ix_([1,2],[3,4,5],[6,7])
Out[266]: 
(array([[[1]],
 
        [[2]]]),
 array([[[3],
         [4],
         [5]]]),
 array([[[6, 7]]]))

In [267]: [i.shape for i in np.ix_([1,2],[3,4,5],[6,7])]
Out[267]: [(2, 1, 1), (1, 3, 1), (1, 1, 2)]

这些列表将从3D(或更大)数组中选择一个形状(2,3,2)的块。

正式地,这在

(您的切片都在最后。当切片在中间出现切片时,此索引有些细微差别。请参阅有关组合组合高级的小节如果出现了基本索引。)

In [219]: table = np.zeros((5, 5, 2, 2, 2, 10, 8),int)    
In [220]: table.shape
Out[220]: (5, 5, 2, 2, 2, 10, 8)

The fact that you use slice instead of : doesn't matter; same for the fact that the trailing slices don't have to be specified.

In [221]: table[4, [0, 1], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)].shape
Out[221]: (2, 10, 8)

This has an advanced indexing array/list of length 2 - the other dimensions are either scalars or slices. So they disappear or 'pass through'.

In [222]: table[4, [0, 1], 1, 1, 1, [0, 2], slice(0, 8, None)].shape
Out[222]: (2, 8)

Here you have two advanced indexing lists - both length 2, so they 'broadcast' together to select 2 values (I think of this as a kind of 'diagonal').

In [223]: table[4, [0, 1, 4], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)].shape
Out[223]: (3, 10, 8)

Same as before but with a length 3 list.

But when the 2 lists have different length you get an error:

In [225]: table[4, [0, 1], 1, 1, 1, [0, 2, 6], slice(0, 8, None)].shape
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [225], in <cell line: 1>()
----> 1 table[4, [0, 1], 1, 1, 1, [0, 2, 6], slice(0, 8, None)].shape

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,) 

If one list is (2,1), then it works - it selects 2 in one dimension, and 3 in the other:

In [226]: table[4, [[0], [1]], 1, 1, 1, [0, 2, 6], slice(0, 8, None)].shape
Out[226]: (2, 3, 8)

In indexing, 'broadcasting' follows the same rules as when adding (or multiplying) arrays.

(2,) with (2,) => (2,)
(2,1) with (3,) => (2,3)
(2,) with (3,) error

In [227]: np.ones(2)+np.ones(2)
Out[227]: array([2., 2.])

In [228]: np.ones(2)+np.ones(3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [228], in <cell line: 1>()
----> 1 np.ones(2)+np.ones(3)

ValueError: operands could not be broadcast together with shapes (2,) (3,) 

In [229]: np.ones((2,1))+np.ones(3)
Out[229]: 
array([[2., 2., 2.],
       [2., 2., 2.]]

edit

Look at a simpler 2d array:

In [261]: arr = np.arange(6).reshape(2,3)

In [262]: arr
Out[262]: 
array([[0, 1, 2],
       [3, 4, 5]])

If I index with 2 (2,) arrays I get 2 values:

In [264]: arr[np.array([0,1]), np.array([1,2])]
Out[264]: array([1, 5])

But if I index with a (2,1) and (2,), I get a (2,2) shape result. Note where the [1,5] values are:

In [265]: arr[np.array([0,1])[:,None], np.array([1,2])]
Out[265]: 
array([[1, 2],
       [4, 5]])

ix_ is a handy tool for constructing such a "cartesian" set of indexing arrays. For example 3 lists I get:

In [266]: np.ix_([1,2],[3,4,5],[6,7])
Out[266]: 
(array([[[1]],
 
        [[2]]]),
 array([[[3],
         [4],
         [5]]]),
 array([[[6, 7]]]))

In [267]: [i.shape for i in np.ix_([1,2],[3,4,5],[6,7])]
Out[267]: [(2, 1, 1), (1, 3, 1), (1, 1, 2)]

Together those will select a block of shape (2,3,2) from a 3d (or larger) array.

Formally this is described in https://numpy.org/doc/stable/user/basics.indexing.html#advanced-indexing

(Your slices are all at the end. There is a nuance to this indexing when slices occur in the middle. See the subsection about Combining advanced and basic indexing if that arises.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文