I'm working with numpy
in python
to calculate a vector multiplication.
I have a vector x of dimensions n x 1 and I want to calculate x*x_transpose.
This gives me problems because x.T
or x.transpose()
doesn't affect a 1 dimensional vector (numpy
represents vertical and horizontal vectors the same way).
But how do I calculate a (n x 1) x (1 x n) vector multiplication in numpy
?
numpy.dot(x,x.T) gives a scalar, not a 2D matrix as I want.
Numpy with PythonThis function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix multiplication. For 1-D arrays, it is the inner product of the vectors. For N-dimensional arrays, it is a sum product over the last axis of a and the second-last axis of b.
First find the magnitude of the two vectors a and b, i.e., |→a| | a → | and |→b| | b → | . Secondly, find the cosine of the angle θ between the two vectors. Finally take a product of the magnitude of the two vectors and the and cosine of the angle between the two vectors, to obtain the dot product of the two vectors.
dot() will compute the dot product of the inputs. If both inputs are 2-dimensional arrays, then np. dot() will perform matrix multiplication.
You are essentially computing an Outer Product.
You can use np.outer
.
In [15]: a=[1,2,3]
In [16]: np.outer(a,a)
Out[16]:
array([[1, 2, 3],
[2, 4, 6],
[3, 6, 9]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With