ValueError: shapes (3,3,1) and (3,1) not aligned: 1 (dim 2) != 3 (dim 0)

Viewed 629

I am trying to multiply some matrices in python, using the np.dot function.I have a three by three array that I want to multiply by a three by one

ValueError: shapes (3,3,1) and (3,1) not aligned: 1 (dim 2) != 3 (dim 0)

What exactly does the third dimension on the array mean? Is there a way to get rid of it?

1 Answers

A (3,3,1) means that you have a vector of 3 two dimensional vectors. Take this as example:

a = np.random.rand(3,3,1)
print(a)

[[[0.08233029]
  [0.21532053]
  [0.88495997]]

 [[0.59743708]
  [0.97966668]
  [0.44927175]]

 [[0.40792714]
  [0.85891152]
  [0.22584841]]]

As above, there are 3 vectors of two dimensions with 3 numbers in a vector. In order to remove it, just use np.reshape to do the trick.

a = np.reshape(a, [3,3])
print(a)

[[0.08233029 0.21532053 0.88495997]
 [0.59743708 0.97966668 0.44927175]
 [0.40792714 0.85891152 0.22584841]]

From here onwards, you can do your np.dot to obtain your result

Related