Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy column and row vectors

Tags:

python

numpy

Why is it in numpy possible to multiply a 2x2 matrix by a 1x2 row vector?

import numpy as np

I = np.array([[1.0, 0.0], [0.0, 1.0]])
x = np.array([2.0,3.0])

In: I * x
Out: array([[ 2.,  0.], [ 0.,  3.]])

Transposing x does also make no sense. A row vector stays a row vector?

In: x.T
Out: array([ 2.,  3.])

From a mathematical point of view the representation is very confusing.

like image 637
Matthias Avatar asked Mar 10 '26 15:03

Matthias


1 Answers

Numpy arrays are not vectors. Or matrices for that matters. They're arrays.

They can be used to represent vectors, matrices, tensors or anything you want. The genius of numpy however is to represent arrays, and let the user decide on their meaning.

One operation defined on arrays is the (termwise) multiplication. In addition, broadcasting lets you operate on arrays of different shapes by 'extending' it in the missing dimensions, so your multiplication is really the (termwise) multiplication of:

[[1.0, 0.0], [0.0, 1.0]] * [[2.0, 2.0], [3.0, 3.0]]

If you want to use the dot product, in the matricial sense, you should use the .dot method, which does exactly that: interpret its inputs as vectors/matrices/tensors, and do a dot product.

like image 166
val Avatar answered Mar 12 '26 07:03

val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!