Alexandre Willame
Alexandre Willame

Reputation: 455

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!

Upvotes: 13

Views: 28660

Answers (7)

vesszabo
vesszabo

Reputation: 441

b = np.array([1, 2, 2])
print(b)
print(np.transpose([b]))
print("rows, cols: ", b.shape)
print("rows, cols: ", np.transpose([b]).shape)

Results in

[1 2 2]
[[1]
 [2]
 [2]]
rows, cols:  (3,)
rows, cols:  (3, 1)

Here (3,) can be thought as "(3, 0)". However if you want the transpose of a matrix A, np.transpose(A) is the solution. Shortly, [] converts a vector to a matrix, a matrix to a higher dimension tensor.

Upvotes: 0

chthonicdaemon
chthonicdaemon

Reputation: 19760

This is either the inner or outer product of the two vectors, depending on the orientation you assign to them. Here is how to calculate either without changing x.

import numpy
x = numpy.array([1, 2, 3])
inner = x.dot(x)
outer = numpy.outer(x, x)

Upvotes: 1

david
david

Reputation: 127

I had the same problem, I used numpy matrix to solve it:

# assuming x is a list or a numpy 1d-array 
>>> x = [1,2,3,4,5]

# convert it to a numpy matrix
>>> x = np.matrix(x)
>>> x
matrix([[1, 2, 3, 4, 5]])

# take the transpose of x
>>> x.T
matrix([[1],
        [2],
        [3],
        [4],
        [5]])

# use * for the matrix product
>>> x*x.T
matrix([[55]])
>>> (x*x.T)[0,0]
55

>>> x.T*x
matrix([[ 1,  2,  3,  4,  5],
        [ 2,  4,  6,  8, 10],
        [ 3,  6,  9, 12, 15],
        [ 4,  8, 12, 16, 20],
        [ 5, 10, 15, 20, 25]])

While using numpy matrices may not be the best way to represent your data from a coding perspective, it's pretty good if you are going to do a lot of matrix operations!

Upvotes: 6

Nathan
Nathan

Reputation: 528

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)

Upvotes: 12

Jaime
Jaime

Reputation: 67427

What np.transpose does is reverse the shape tuple, i.e. you feed it an array of shape (m, n), it returns an array of shape (n, m), you feed it an array of shape (n,)... and it returns you the same array with shape(n,).

What you are implicitly expecting is for numpy to take your 1D vector as a 2D array of shape (1, n), that will get transposed into a (n, 1) vector. Numpy will not do that on its own, but you can tell it that's what you want, e.g.:

>>> a = np.arange(4)
>>> a
array([0, 1, 2, 3])
>>> a.T
array([0, 1, 2, 3])
>>> a[np.newaxis, :].T
array([[0],
       [1],
       [2],
       [3]])

Upvotes: 32

Alexandre Willame
Alexandre Willame

Reputation: 455

The file 'myfile.txt' contain lines such as

5.100000 3.500000 1.400000 0.200000 1
4.900000 3.000000 1.400000 0.200000 1

Here is the code I run:

import numpy as np
data = np.loadtxt('iris.txt')
x = data[1,:]

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

And I get as a result

(5L,)
(5L,)
[ 24.01   9.     1.96   0.04   1.  ]
[ 24.01   9.     1.96   0.04   1.  ]

I would be expecting one of the two last result to be a scalar instead of a vector, because x^T.x (or x.x^T) should give a scalar.

Upvotes: 0

JoshAdel
JoshAdel

Reputation: 68682

For starters L just means that the type is a long int. This shouldn't be an issue. You'll have to give additional information about your problem though since I cannot reproduce it with a simple test case:

In [1]: import numpy as np

In [2]: a = np.arange(12).reshape((4,3))

In [3]: a
Out[3]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

In [4]: a.T #same as np.transpose(a)
Out[4]:
array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

In [5]: a.shape
Out[5]: (4, 3)

In [6]: np.transpose(a).shape
Out[6]: (3, 4)

There is likely something subtle going on with your particular case which is causing problems. Can you post the contents of the file that you're reading into x?

Upvotes: 1

Related Questions