Python numpy 1D数组是否遵循行/列规则?
我刚刚开始使用numpy,对如何使用数组感到困惑。我在numpy阵列上看到了几个堆栈溢出的答案,但它们都涉及如何获得所需的结果(我知道如何做到这一点,我只是不知道为什么需要这样做)。我所看到的共识是数组比矩阵好,因为它们是一个更基本的类,限制更少。我知道你可以转置一个数组,这对我来说意味着行和列之间有区别,但是乘法规则都会产生错误的输出(与我期望的结果相比) 以下是我随输出一起编写的测试代码:Python numpy 1D数组是否遵循行/列规则?,python,arrays,numpy,Python,Arrays,Numpy,我刚刚开始使用numpy,对如何使用数组感到困惑。我在numpy阵列上看到了几个堆栈溢出的答案,但它们都涉及如何获得所需的结果(我知道如何做到这一点,我只是不知道为什么需要这样做)。我所看到的共识是数组比矩阵好,因为它们是一个更基本的类,限制更少。我知道你可以转置一个数组,这对我来说意味着行和列之间有区别,但是乘法规则都会产生错误的输出(与我期望的结果相比) 以下是我随输出一起编写的测试代码: a = numpy.array([1,2,3,4]) print(a) >>> [1
a = numpy.array([1,2,3,4])
print(a)
>>> [1 2 3 4]
print(a.T) # Transpose
>>> [1 2 3 4] # No apparent affect
b = numpy.array( [ [1], [2], [3], [4] ] )
print(b)
>>> [[1]
[2]
[3]
[4]] # Column (Expected)
print(b.T)
>>> [[1 2 3 4]] # Row (Expected, transpose seems to work here)
print((b.T).T)
>>> [[1]
[2]
[3]
[4]] # Column (All of these are as expected,
# unlike for declaring the array as a row vector)
# The following are element wise multiplications of a
print(a*a)
>>> [ 1 4 9 16]
print(a * a.T) # Row*Column
>>> [ 1 4 9 16] # Inner product scalar result expected
print(a.T * a) # Column*Row
>>> [ 1 4 9 16] # Outer product matrix result expected
print(b*b)
>>> [[1]
[4]
[9]
[16]] # Expected result, element wise multiplication in a column
print(b * b.T) # Column * Row (Outer product)
>>> [[ 1 2 3 4]
[ 2 4 6 8]
[ 3 6 9 12]
[ 4 8 12 16]] # Expected matrix result
print(b.T * (b.T)) # Column * Column (Doesn't make much sense so I expected elementwise multiplication
>>> [[ 1 4 9 16]]
print(b.T * (b.T).T) # Row * Column, inner product expected
>>> [[ 1 2 3 4]
[ 2 4 6 8]
[ 3 6 9 12]
[ 4 8 12 16]] # Outer product result
我知道我可以使用numpy.inner()
和numpy.outer()
来实现效果(这不是问题),我只想知道我是否需要跟踪向量是行还是列
我还知道,我可以创建一个一维矩阵来表示向量,乘法的效果与预期一样。我正试图找出存储数据的最佳方法,这样当我查看代码时,就可以清楚地知道将要发生什么——现在数学看起来很混乱,也很错误
在我的应用中,我只需要使用1D和2D张量。从一个简单的角度来看,“转置”实际上只是二维结构的一个有意义的概念:
>>> import numpy
>>> arr = numpy.array([1,2,3,4])
>>> arr.shape
(4,)
>>> arr.transpose().shape
(4,)
所以,如果你想转置一些东西,你必须使它成为二维的:
>>> arr_2d = arr.reshape((4,1)) ## four rows, one column -> two-dimensional
>>> arr_2d.shape
(4, 1)
>>> arr_2d.transpose().shape
(1, 4)
另外,numpy.array(iterable,**kwargs)
有一个关键字参数ndmin
,该参数将设置为ndmin=2
根据需要在所需形状前面加上尽可能多的1
:
>>> arr_ndmin = numpy.array([1,2,3,4],ndmin=2)
>>> arr_ndmin.shape
(1, 4)
是的,有
你的问题已经回答了。虽然我假设你是Matlab用户?如果是这样,您可能会发现本指南很有用:我将尝试为您的代码添加注释
a = numpy.array([1,2,3,4])
print(a)
>>> [1 2 3 4]
print(a.T) # Transpose
>>> [1 2 3 4] # No apparent affect
a.shape
将显示(4,)
<代码>a.T.形状相同。它保持相同数量的维度,并执行唯一有意义的转置-没有变化。使其成为(4,1)
将增加一个维度,并破坏a.T.T
往返
b = numpy.array( [ [1], [2], [3], [4] ] )
print(b)
>>> [[1]
[2]
[3]
[4]] # Column (Expected)
print(b.T)
>>> [[1 2 3 4]] # Row (Expected, transpose seems to work here)
b.shape
是(4,1)
,b.T.shape
是(1,4)
。注意额外的一组[]。如果将a
创建为a=numpy.array([[1,2,3,4]])
它的形状也会是(1,4)
制作b
的简单方法是b=np.array([1,2,3,4]).T
(或b=np.array([1,2,3,4])[:,None]
或b=np.array([1,2,3,4])。重塑(-1,1)
)
将其与MATLAB进行比较
octave:3> a=[1,2,3,4]
a =
1 2 3 4
octave:4> size(a)
ans =
1 4
octave:5> size(a.')
ans =
4 1
即使没有额外的[],它也将矩阵初始化为2d
numpy
有一个模仿MATLAB的matrix
类-在MATLAB只允许二维的时候
In [75]: m=np.matrix('1 2 3 4')
In[76]:m
Out[76]:矩阵([[1,2,3,4]]
我不建议使用np.matrix
,除非它确实为代码添加了一些有用的东西
请注意,MATLAB谈到了向量,但它们实际上只是它们的矩阵,只有一个非幺正维
# The following are element wise multiplications of a
print(a*a)
>>> [ 1 4 9 16]
print(a * a.T) # Row*Column
>>> [ 1 4 9 16] # Inner product scalar result expected
此行为源自a.T==a
。正如您所指出的,*
生成元素对元素的乘法。这相当于MATLAB的*
np.点(a,a)
给出2个数组的点或矩阵积
print(a.T * a) # Column*Row
>>> [ 1 4 9 16] # Outer product matrix result expected
不,它仍在进行元素乘法
我会使用broadcasting
,a[:,None]*a[None,:]
来获取外部产品。八度音阶是模仿numpy添加的;我不知道MATLAB是否有
在下面的*
中,始终是逐元素乘法。广播产生矩阵/外积结果
print(b*b)
>>> [[1]
[4]
[9]
[16]] # Expected result, element wise multiplication in a column
A(4,1)*(4,1)=>(4,1)
。到处都是相同的形状
print(b * b.T) # Column * Row (Outer product)
>>> [[ 1 2 3 4]
[ 2 4 6 8]
[ 3 6 9 12]
[ 4 8 12 16]] # Expected matrix result
这里(4,1)*(1,4)=>(4,4)
产品。2个尺寸1
维度已被复制,因此它实际上成为(4,4)*(4,4)
。您将如何在MATLAB中使用*
print(b.T * (b.T)) # Column * Column (Doesn't make much sense so I expected elementwise multiplication
>>> [[ 1 4 9 16]]
*
是元素级的,与期望无关。在MATLAB中思考b'.*b'
print(b.T * (b.T).T) # Row * Column, inner product expected
>>> [[ 1 2 3 4]
[ 2 4 6 8]
[ 3 6 9 12]
[ 4 8 12 16]] # Outer product result
同样,*
是元素级的<代码>内部除了乘法之外还需要求和。在这里,广播再次应用(1,4)*(4,1)=>(4,4)
np.dot(b,b)
或np.trace(b.T*b)
或np.sum(b*b)
give30
当我在MATLAB中工作时,我经常检查大小
,并创建能够捕捉尺寸不匹配的测试矩阵(例如,2x3而不是2x2矩阵)。我在努比继续这样做
关键是:
数组可以是1d(甚至0d)numpy
- (4,)数组与
或(1,4)`数组不完全相同(4,1)
是元素-始终*
- 广播通常解释了
类行为外部
a*a
在Matlab中是一个错误,但在numpy中执行元素乘法,a*a'
是一个有效的内积/外积(取决于a是行向量还是列向量)在Matlab中,但是numpy中的a*a.T
仍然输出元素乘法。@Francis您是否尝试过这样做:numpy.dot(a,a.T)
或a.dot(a.T)
。尽管出于优化原因,我建议您将a.T
存储在另一个变量中。是的,我知道numpy.dot()
,numpy.outer()
等。我知道如何执行我想要的操作,我只想知道*操作符如何在1D numpy数组上工作,因为我的测试结果是不直观的。啊,我明白了。因此,问题不在于如何做到这一点,而在于它们之间的区别。你说得对,这两种情况有很大不同。矢量化的概念
print(b.T * (b.T).T) # Row * Column, inner product expected
>>> [[ 1 2 3 4]
[ 2 4 6 8]
[ 3 6 9 12]
[ 4 8 12 16]] # Outer product result