Cyberlander - 4 months ago 51

Python Question

I have a problem to understand the matrix multiplication in numpy.

For example I have the following matrix (2d numpy array):

`a = [ [ 1. 1. ]`

[ 1. 2. ]

[ 1. 3. ] ]

And the following row vector theta:

`theta = [ 1. 1. ]`

The only way to multiply a with theta would be to transform

theta in a column vector first and then I would get the result:

`result = [ [ 2. ]`

[ 3. ]

[ 4. ] ]

When I multiply the matrix and the row vector (without transforming)

`result = np.dot(a,theta)`

I get this:

`result = [ 2. 3. 4. ]`

How is this even possible? I mean, I didn't transform the matrix.

Can you please tell me how this numpy multiplication works?

Thank you for your attention.

Answer

No, you're multiplying numpy array with another numpy array (not a matrix with a vector), although it looks like that. This is because, in essence, numpy arrays are not the same as matrices. And the dot product treats it that way as well.

If you write out the array and multiply, then you will see why. It's just the dot product (element-wise multiplication) of each row in the array `'a'`

with the vector `'theta'`

.

PS: (*matrices are 2-D while arrays are not limited to any dimension*)

Also, please take a look at this answer and this excellent answer