Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dot product of two 1D vectors in numpy

Tags:

python

numpy

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.

like image 803
Vjeetje Avatar asked Apr 08 '14 23:04

Vjeetje


People also ask

How does dot product work numpy?

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.

How do you find the dot product of two vectors?

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.

Is numpy dot matrix multiplication?

dot() will compute the dot product of the inputs. If both inputs are 2-dimensional arrays, then np. dot() will perform matrix multiplication.


1 Answers

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]])
like image 91
Nitish Avatar answered Sep 28 '22 03:09

Nitish