Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transpose of a vector using numpy

I am having an issue with Ipython - Numpy. I want to do the following operation:

x^T.x

with formula and x^T the transpose operation on vector x. x is extracted from a txt file with the instruction:

x = np.loadtxt('myfile.txt')

The problem is that if i use the transpose function

np.transpose(x)

and uses the shape function to know the size of x, I get the same dimensions for x and x^T. Numpy gives the size with a L uppercase indice after each dimensions. e.g.

print x.shape
print np.transpose(x).shape

(3L, 5L)
(3L, 5L)

Does anybody know how to solve this, and compute x^T.x as a matrix product?

Thank you!

like image 971
Alexandre Willame Avatar asked Oct 08 '13 02:10

Alexandre Willame


1 Answers

As explained by others, transposition won't "work" like you want it to for 1D arrays. You might want to use np.atleast_2d to have a consistent scalar product definition:

def vprod(x):
    y = np.atleast_2d(x)
    return np.dot(y.T, y)
like image 76
Nathan Avatar answered Sep 21 '22 13:09

Nathan