Home > OS >  dot product of (n,) shaped arrays to give an n by n array
dot product of (n,) shaped arrays to give an n by n array

Time:03-06

Say you have an array X of shape (n,),

import numpy as np

n = 10
X = np.random.rand(n)

and you want to make the following dot product XX^T (by X^T I mean the transpose of X). The result should give an n by n matrix. However using

np.dot(X, X.T)

will give a scalar. It's like if it does X^T X instead. Unless you do the following

X = np.reshape(X, (X.shape[0], 1))
np.dot(X, X.T)

Is there a way to do it without having to reshape the numpy vector?

CodePudding user response:

If both a and b are 1-D arrays, numpy.dot(a, b) returns the inner product of vectors (without complex conjugation).

You can use the numpy.outer function instead:

np.outer(X, X)
  • Related