numpy 1D数组是否遵循行/列规则?

Fra*_*cis 4 python arrays numpy

我刚刚开始使用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
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用numpy.inner()并numpy.outer()实现效果(这不是问题),我只想知道是否需要跟踪向量是行还是列。

我也知道我可以创建一个一维矩阵来表示我的向量,并且乘法可以按预期进行。我正在尝试找出存储数据的最佳方法,以便在查看代码时可以清楚地知道将要发生的事情-现在,数学看上去只是令人困惑和错误。

我只需要为我的应用程序使用1D和2D张量。

hpa*_*ulj 5

我会尝试注释您的代码

a = numpy.array([1,2,3,4])
print(a)
>>> [1 2 3 4]

print(a.T)          # Transpose
>>> [1 2 3 4]       # No apparent affect
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

即使没有多余的[],它也将矩阵初始化为2d。

numpy有一个matrix模仿MATLAB 的类-回到MATLAB仅允许2d的时间。

In [75]: m=np.matrix('1 2 3 4')
Run Code Online (Sandbox Code Playgroud)

In [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]])
Run Code Online (Sandbox Code Playgroud)

我不建议您使用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
Run Code Online (Sandbox Code Playgroud)

此行为源自a.T == A。如您所述,*产生逐元素乘法。这等效于MATLAB .*。 np.dot(a,a)给出2个数组的点或矩阵乘积。

print(a.T * a)      # Column*Row
>>> [ 1  4  9 16]   # Outer product matrix result expected
Run Code Online (Sandbox Code Playgroud)

不,它仍在进行元素乘法。

我会使用broadcasting,a[:,None]*a[None,:]以获得外产品。Octave在模仿numpy时添加了此内容;我不知道MATLAB是否有它。

下面*始终是逐元素相乘。产生矩阵/外部产品结果的广播。

print(b*b)
>>> [[1]
     [4]
     [9]
     [16]]          # Expected result, element wise multiplication in a column
Run Code Online (Sandbox Code Playgroud)

一(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
Run Code Online (Sandbox Code Playgroud)

这里的(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]]
Run Code Online (Sandbox Code Playgroud)

*无论期望如何,元素都是元素。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
Run Code Online (Sandbox Code Playgroud)

再次*是元素化的;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类似行为