Matrix multiplication shape error

Hi, sorry this might be a silly mistake from me, I seem to have the shapes right for the X_train and w matrices X.shape (99,4) and w.shape (4,). But X@w still complains that the operands are not broadcast correctly. Can you please help?

f_wb = X @ w + b

ValueError: operands could not be broadcast together with shapes (99,) (4,)

The error tells you the shapes are (99,) and (4,) so these shapes cannot be multiplied. Try to find out whi X is not in the right shape.

Hi, thanks for your quick response and pointing me in the right direction. I had reversed the variables returned from the compute_gradient_matrix function. Was assigning dj_db to dj_dw. Hence the shape error. It seems to be working fine now.

1 Like

Hey, @Srikanth_Adya , I have also encountered this problem yesterday, while I was writing code for Linear regression.

Earlier my linear regression model function was:

f_wb = w @ X + b

I have handle this problem by swapping places of X to w and vice-versa.

And later, I found why this happening.

According to official numpy docs:

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing (i.e. rightmost) dimension and works its way left. Two dimensions are compatible when

  1. they are equal, or
  2. one of them is 1.

So, it means the shapes of element while doing broadcasting (mutliplication / dot product or any other broadcasting operation), it checks the rightmost element’s dimension they must be equal.