Ask Your Question

How can I resolve the ValueError regarding incompatible shapes between (None, 20, 9) and (None, 9)?

asked 2022-05-22 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-01-21 12:00:00 +0000

david gravatar image

The ValueError regarding incompatible shapes between (None, 20, 9) and (None, 9) can be resolved by reshaping the (None, 9) array to (None, 1, 9) so that it can be broadcasted with the (None, 20, 9) array in the operation.

You can use the np.newaxis attribute to achieve this. Here's an example:

import numpy as np

# sample arrays
a = np.random.rand(10, 20, 9)
b = np.random.rand(10, 9)

# reshape b to (None, 1, 9)
b = np.reshape(b, (b.shape[0], 1, b.shape[1]))

# perform operation
c = a * b

In the above code, we first reshape the b array using np.reshape to add the new axis of size 1 in the second dimension. This results in the shape of (None, 1, 9).

We then perform the operation between a and b, resulting in the shape of (None, 20, 9) which is broadcasted over the (None, 1, 9).

This should resolve the ValueError issue.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-05-22 11:00:00 +0000

Seen: 12 times

Last updated: Jan 21 '22