我刚刚开始使用numpy,并且对如何使用数组感到困惑。我已经在numpy数组上看到了几个Stack Overflow答案,但是它们都处理如何获得所需的结果(我知道如何做到这一点,我只是不知道为什么我需要这样做)。我已经看到的共识是,数组比矩阵更好,因为它们是更基本的类,并且限制较少。我知道您可以转置数组,这对我来说意味着行和列之间是有区别的,但是乘法规则都会产生错误的输出(与我的预期相比)。
这是我与输出一起编写的测试代码:
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张量。
我将尝试注释您的代码
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.shape
是一样的 它保持相同数量的尺寸,并且执行了唯一有意义的转置-没有任何变化。这样做(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]).reshape(-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
有一个matrix
模仿MATLAB的类-回到MATLAB仅允许2d的时间。
In [75]: m=np.matrix('1 2 3 4')
在[76]中:m Out [76]:矩阵([[1、2、3、4]])
In [77]: m.shape
Out[77]: (1, 4)
In [78]: m=np.matrix('1 2; 3 4')
In [79]: m
Out[79]:
matrix([[1, 2],
[3, 4]])
我不建议您使用,np.matrix
除非它确实为您的代码添加了一些有用的东西。
注意MATLAB所说的vectors
,但实际上它们matrix
只是一个非一维的。
# 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.dot(a,a)
给出2个数组的点或矩阵乘积。
print(a.T * a) # Column*Row
>>> [ 1 4 9 16] # Outer product matrix result expected
不,它仍在进行元素乘法。
我会使用broadcasting
,a[:,None]*a[None,:]
以获得外产品。Octave在模仿numpy时添加了此内容;我不知道MATLAB是否有它。
下面*
始终是逐元素相乘。广播产生矩阵/外部产品结果。
print(b*b)
>>> [[1]
[4]
[9]
[16]] # Expected result, element wise multiplication in a column
一(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)
产品。1
复制了2个尺寸尺寸,因此实际上变成了(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]]
*
无论期望如何,元素都是元素。b' .* b'
在MATLAB中思考。
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
再次*
是元素化的;inner
除乘法外,还需要求和。此处再次适用广播(1,4)*(4,1)=>(4,4)
。
np.dot(b,b)
或np.trace(b.T*b)
或np.sum(b*b)
给予30
。
在MATLAB中工作时,我经常检查size
,并创建了可捕捉尺寸不匹配的测试矩阵(例如2x3而不是2x2矩阵)。我将继续以numpy的方式进行操作。
关键的事情是:
numpy
数组可能是1d(甚至是0d)
(4,)数组与a(4,1)
或(1,4)`不完全相同。
*
是元素化的-始终如此。
广播通常会导致outer
类似行为
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句