Suga
Suga

Reputation: 201

ValueError: could not broadcast input array from shape (10,10) into shape (10,18)

Im using python 3.5 and am attempting a broadcast a 2d list into a 3d list.

ValueError: could not broadcast input array from shape (10,10) into shape (10,18)

Here is the code I am using that generates the error

v_level =4
M=16
n_down = M/2
n_down0 = M
residual_h_to_2h = np.zeros((v_level,M+2,M+2))
residual_h_to_2h[q][0:n_down+2][0:n_down+2] = restrict(residual_h[0:n_down0+2][0:n_down0+2])

The shape (0,18) corresponds to residual_h_to_2h. The function restrict returns a list of shape ( (M/2)+2 , (N/2)+2) where (M,N) is the shape of input list.

I am not able to understand why the shape of residual_h_to_2h[q][0:n_down+2][0:n_down+2] is reported as 10,18. It should be 10,10.

I do not seem to be able to resolve this issue on my own. Any help or links to relevant documentation would be greatly appreciated.

Upvotes: 2

Views: 18943

Answers (1)

hruske
hruske

Reputation: 2243

Ooh, now I see it.

Correct slicing with three dimensions:

residual_h_to_2h[q,0:n_down+2,0:n_down+2].shape
(10, 10)

Why?

You are slicing by the same dimension both times you're using 0:ndown+2. Original ndarray:

residual_h_to_2h.shape
(4, 18, 18)

First level is scalar and it reduces dimension:

residual_h_to_2h[q].shape
(18, 18)

Second level is slicing and it does not reduce dimension so these are actually equal and same:

residual_h_to_2h[q][0:n_down+2][0:n_down+2].shape
(10, 18)

residual_h_to_2h[q][0:n_down+2].shape
(10, 18)

It gets easier to spot if having not a times a, but a times b array:

residual_h_to_2h[q][0:10].shape
(10, 18)

residual_h_to_2h[q][0:10][0:5].shape
(5, 18)

Upvotes: 3

Related Questions