Numpy inner product of 2 column vectors

10.3k Views Asked by At

How can I take an inner product of 2 column vectors in python's numpy

Below code does not work

import numpy as np
x = np.array([[1], [2]])
np.inner(x, x)

It returned

array([[1, 2],
       [2, 4]])`

instead of 5

4

There are 4 best solutions below

3
On BEST ANSWER

The inner product of a vector with dimensions 2x1 (2 rows, 1 column) with another vector of dimension 2x1 (2 rows, 1 column) is a matrix with dimensions 2x2 (2 rows, 2 columns). When you take the inner product of any tensor the inner most dimensions must match (which is 1 in this case) and the result is a tensor with the dimensions matching the outter, i.e.; a 2x1 * 1x2 = 2x2.

What you want to do is transpose both such that when you multiply the dimensions are 1x2 * 2x1 = 1x1.

More generally, multiplying anything with dimensions NxM by something with dimensionsMxK, yields something with dimensions NxK. Note the inner dimensions must both be M. For more, review your matrix multiplication rules

The np.inner function will automatically transpose the second argument, thus when you pass in two 2x1, you get a 2x2, but if you pass in two 1x2 you will get a 1x1.

Try this:

import numpy as np
x = np.array([[1], [2]])
np.inner(np.transpose(x), np.transpose(x))

or simply define your x as row vectors initially.

import numpy as np
x = np.array([1,2])
np.inner(x, x)
0
On

Try the following it will work

np.dot(np.transpose(a),a))
0
On

i think you mean to have:

x= np.array([1,2])

in order to get 5 as output, your vector needs to be 1xN not Nx1 if you want to apply np.inner on it

0
On

make sure col_vector has shape (N,1) where N is the number of elements

then simply sum one to one multiplication result

np.sum(col_vector*col_vector)