shape mismatch: indexing arrays could not be broadcast together with shapes

kinder chen picture kinder chen · Sep 8, 2017 · Viewed 16.8k times · Source
j=np.arange(20,dtype=np.int)
site=np.ones((20,200),dtype=np.int)
sumkma=np.ones((100,20))

[sumkma[site[x],x] for x in range(20)]

This works, but I don't want to use for loop. When I try

sumkma[site[j],j]

I get this error:

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

How to fix the error?

Answer

tmbo picture tmbo · Sep 9, 2017

When accessing a numpy multi-dimensional array with other multi-dimensional arrays of integer type the arrays used for the indices need to have the same shape.

Numpy will happily broadcast, if possible - but for that to be possible the arrays need to have the same dimensionality, e.g. this works:

sumkma[site[j], j[:,np.newaxis]]

The np.newaxis results in j[:,np.newaxis] being two-dimensional (shape is now (20,1), whereas shape of j is one-dimensional (20,)). It now has a shape that can be broadcasted to the shape of site[j]:

>>> j.shape
(20,)
>>> site[j].shape
(20,200)
>>> j[:,np.newaxis].shape
(20,1)

The same dimensionality for the index arrays allows numpy to broadcast them to have the same shape (20,200).

https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html#indexing-multi-dimensional-arrays