I am having an issue with Ipython - Numpy. I want to do the following operation:
我对Ipython - Numpy有意见。我想做以下操作:
x^T.x
with x belonging to R^n, and x^T the transpose operation on vector x. x is extracted from a txt file with the instruction:
属于R ^ n x,x ^ T向量x x上转置操作提取从txt文件指令:
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.
并使用大小的形状函数知道x,我得到同样的尺寸x,x ^ T。Numpy在每个维度之后都给出一个L大写标记的大小。如。
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?
有人知道如何解决这个问题,并计算x ^ T。x作为矩阵乘积?
Thank you!
谢谢你!
6 个解决方案
#1
6
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:
正如其他人所解释的,转置不会像你希望的那样在一维数组中“工作”。你可能想用np。至少有一个一致的标量积定义:
def vprod(x):
y = np.atleast_2d(x)
return np.dot(y.T, y)
#2
27
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,)
.
np。转置做的是反转形状元组,即你给它提供一个形状数组(m, n),它返回一个形状数组(n, m),你给它提供一个形状数组(n,)…它返回相同的数组(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.:
你所隐含期望的是numpy将你的1D向量作为一个二维的形状数组(1,n)它会被转置成一个(n, 1)向量。Numpy自己不会这么做,但是你可以告诉它那是你想要的,例如:
>>> 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]])
#3
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:
对于初学者来说,我只是说这个类型是一个很长的int类型,这应该不是问题。你将不得不提供更多关于你的问题的信息,因为我不能用一个简单的测试用例来重现它:
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
?
在你的特殊情况下,可能有一些微妙的事情正在发生,它正在引起问题。你能把读到的文件的内容贴在x上吗?
#4
1
I had the same problem, I used numpy matrix to solve it:
我也有同样的问题,我用numpy矩阵来求解:
# 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!
虽然使用numpy矩阵可能不是从编码的角度表示数据的最佳方式,但是如果您要进行大量的矩阵操作,那么它是相当不错的!
#5
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
.
这是两个向量的内积或外积,取决于你给它们的方向。这是如何在不改变x的情况下计算。
import numpy
x = numpy.array([1, 2, 3])
inner = x.dot(x)
outer = numpy.outer(x, x)
#6
0
The file 'myfile.txt' contain lines such as
文件的myfile。txt包含诸如。
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.
我将期待的两个最后的结果是一个标量,而不是一个矢量,因为x ^ T。x(或x.x ^ T)应该给一个标量。
#1
6
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:
正如其他人所解释的,转置不会像你希望的那样在一维数组中“工作”。你可能想用np。至少有一个一致的标量积定义:
def vprod(x):
y = np.atleast_2d(x)
return np.dot(y.T, y)
#2
27
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,)
.
np。转置做的是反转形状元组,即你给它提供一个形状数组(m, n),它返回一个形状数组(n, m),你给它提供一个形状数组(n,)…它返回相同的数组(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.:
你所隐含期望的是numpy将你的1D向量作为一个二维的形状数组(1,n)它会被转置成一个(n, 1)向量。Numpy自己不会这么做,但是你可以告诉它那是你想要的,例如:
>>> 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]])
#3
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:
对于初学者来说,我只是说这个类型是一个很长的int类型,这应该不是问题。你将不得不提供更多关于你的问题的信息,因为我不能用一个简单的测试用例来重现它:
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
?
在你的特殊情况下,可能有一些微妙的事情正在发生,它正在引起问题。你能把读到的文件的内容贴在x上吗?
#4
1
I had the same problem, I used numpy matrix to solve it:
我也有同样的问题,我用numpy矩阵来求解:
# 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!
虽然使用numpy矩阵可能不是从编码的角度表示数据的最佳方式,但是如果您要进行大量的矩阵操作,那么它是相当不错的!
#5
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
.
这是两个向量的内积或外积,取决于你给它们的方向。这是如何在不改变x的情况下计算。
import numpy
x = numpy.array([1, 2, 3])
inner = x.dot(x)
outer = numpy.outer(x, x)
#6
0
The file 'myfile.txt' contain lines such as
文件的myfile。txt包含诸如。
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.
我将期待的两个最后的结果是一个标量,而不是一个矢量,因为x ^ T。x(或x.x ^ T)应该给一个标量。