Does numpy 1D arrays need to follow row / column rules?

I just started using numpy, and I got confused about how to use arrays. I saw several answers on numpy arrays, but they all deal with how to get the desired result (I know how to do this, I just don't know why I need to do this). The consensus I saw is that arrays are better than matrices because they are more base class and less restrictive. I understand that you can transpose an array, which for me means that there is a difference between a row and a column, but the multiplication rules produce the wrong outputs (compared to what I expect).

Here is the test code I wrote along with the outputs:

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- , , . , , , , - .

1D 2D .

+4
3

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 roundtrip.

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,:], . 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]]

* , . 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)

  • A (4) , (4,1) (1,4) `.

  • * - .

  • outer

+3

"" :

>>> 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)
+3

, .

. , Matlab? , : MATLAB NumPy

+2

Source: https://habr.com/ru/post/1627645/


All Articles