IndexError:形状错配:索引数组不能与形状一起广播。

时间:2021-12-18 21:22:59
a=np.arange(240).reshape(3,4,20)
b=np.arange(12).reshape(3,4)
c=np.zeros((3,4),dtype=int)
x=np.arange(3)
y=np.arange(4)

I wanna get a 2d (3,4) shape array by the following step without loop.

我想要得到一个2d(3,4)形状的数组。

for i in x:
    c[i]=a[i,y,b[i]]
c
array([[  0,  21,  42,  63],
       [ 84, 105, 126, 147],
       [168, 189, 210, 231]])

I tried,

我试过了,

c=a[x,y,b]

but it shows

但是它显示

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

索引错误:形状错配:索引数组不能与形状(3,)(4,)(3,4)一起广播

and then I also tried to establish newaxis by [:,None], it also doesn't work.

然后我又试着建立一个新的轴,它也不起作用。

2 个解决方案

#1


3  

Try:

试一试:

>>> a[x[:,None], y[None,:], b]
array([[  0,  21,  42,  63],
       [ 84, 105, 126, 147],
       [168, 189, 210, 231]])

Discussion

You tried a[x,y,b]. Note the error message:

你尝试过(x,y,b)。注意,错误信息:

>>> a[x, y, b]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: shape mismatch: indexing arrays could not be broadcast
            together with shapes (3,) (4,) (3,4) 

The (3,) means that we need to extend x to have 3 as the first dimension and 4 as the second dimension. We do this by specifying x[:,None] (which actually allows x to be broadcast to any size second dimension).

(3,)意味着我们需要扩展x的第一个维度和第4个维度。我们通过指定x[:,None]来实现这一点(实际上,它允许将x广播到任何大小的第二个维度)。

Similarly, the error message shows that we need to map y to shape (3,4) and we do that with y[None,:].

类似地,错误消息显示我们需要映射y(3,4)和y[None,:]。

Alternative style

If one prefers, we can replace None with np.newaxis:

如果有人愿意,我们可以用np来代替。

>>> a[x[:,np.newaxis], y[np.newaxis,:], b]
array([[  0,  21,  42,  63],
       [ 84, 105, 126, 147],
       [168, 189, 210, 231]])

np.newaxis is None:

np。newaxis没有:

>>> np.newaxis is None
True

(If I recall correctly, some earlier versions of numpy used a different capitalization style for newaxis. For all versions, though, None seems to works.)

(如果我没记错的话,一些早期版本的numpy使用了不同的用于newaxis的大写样式。不过,对于所有的版本,似乎都没有效果。

#2


1  

Similar but different, hard coded not generic.

相似但不同,硬编码不通用。

>>> b = np.ravel(a)[np.arange(0,240,21)]
>>> b.reshape((3,4))
array([[  0,  21,  42,  63],
       [ 84, 105, 126, 147],
       [168, 189, 210, 231]])
>>> 

#1


3  

Try:

试一试:

>>> a[x[:,None], y[None,:], b]
array([[  0,  21,  42,  63],
       [ 84, 105, 126, 147],
       [168, 189, 210, 231]])

Discussion

You tried a[x,y,b]. Note the error message:

你尝试过(x,y,b)。注意,错误信息:

>>> a[x, y, b]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: shape mismatch: indexing arrays could not be broadcast
            together with shapes (3,) (4,) (3,4) 

The (3,) means that we need to extend x to have 3 as the first dimension and 4 as the second dimension. We do this by specifying x[:,None] (which actually allows x to be broadcast to any size second dimension).

(3,)意味着我们需要扩展x的第一个维度和第4个维度。我们通过指定x[:,None]来实现这一点(实际上,它允许将x广播到任何大小的第二个维度)。

Similarly, the error message shows that we need to map y to shape (3,4) and we do that with y[None,:].

类似地,错误消息显示我们需要映射y(3,4)和y[None,:]。

Alternative style

If one prefers, we can replace None with np.newaxis:

如果有人愿意,我们可以用np来代替。

>>> a[x[:,np.newaxis], y[np.newaxis,:], b]
array([[  0,  21,  42,  63],
       [ 84, 105, 126, 147],
       [168, 189, 210, 231]])

np.newaxis is None:

np。newaxis没有:

>>> np.newaxis is None
True

(If I recall correctly, some earlier versions of numpy used a different capitalization style for newaxis. For all versions, though, None seems to works.)

(如果我没记错的话,一些早期版本的numpy使用了不同的用于newaxis的大写样式。不过,对于所有的版本,似乎都没有效果。

#2


1  

Similar but different, hard coded not generic.

相似但不同,硬编码不通用。

>>> b = np.ravel(a)[np.arange(0,240,21)]
>>> b.reshape((3,4))
array([[  0,  21,  42,  63],
       [ 84, 105, 126, 147],
       [168, 189, 210, 231]])
>>>