Transpose of a vector using numpy
Asked Answered
T

7

13

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!

Tranquil answered 8/10, 2013 at 2:21 Comment(2)
Does anyone know why does this work: x[:,None]Lycopodium
The actual code used by the OP is given in the answer below.Analiese
G
12

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)
Gumdrop answered 8/10, 2013 at 12:22 Comment(0)
C
32

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]])
Chloramphenicol answered 8/10, 2013 at 3:56 Comment(0)
G
12

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)
Gumdrop answered 8/10, 2013 at 12:22 Comment(0)
R
6

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!

Roarke answered 4/12, 2015 at 3:58 Comment(0)
B
1

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?

Bicker answered 8/10, 2013 at 2:41 Comment(0)
T
1

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)
Trudey answered 4/12, 2015 at 4:18 Comment(0)
T
0

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.

Tranquil answered 8/10, 2013 at 3:54 Comment(2)
You can get what you are after as np.dot(x, x). The * operator in numpy represents element-wise multiplication, not matrix multiplication.Chloramphenicol
This isn't an answer, and should be made as an edit to your question instead. Two points: (1) Originally you said that numpy wasn't changing the shape of a (3L, 5L) array, which was very surprising, but isn't not surprising at all that (5L,) stays as (5L,); it's a 1-D object, and its transpose is itself. Was your original report in error? (2) x*np.transpose(x) is an elementwise multiplication of two 1-D arrays, and therefore a 1-D array.Ecclesiastes
Z
0
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.

Zarah answered 28/8, 2018 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.