Difference between single and double Numpy matrix?

What is the difference between these two numpy objects?

import numpy as np
np.array([[0,0,0,0]])
np.array([0,0,0,0])
+4
source share
3 answers
In [71]: np.array([[0,0,0,0]]).shape
Out[71]: (1, 4)

In [72]: np.array([0,0,0,0]).shape
Out[72]: (4,)

The first is a two-dimensional array of size 1 x 4, the last is a four-dimensional one-dimensional array.

+9
source

When you defined an array with two brackets, what you really did was declare an array with an array with 4 0 inside. Therefore, if you want to access the first zero, which you will access your_array[0][0], and in the second array, you simply get access to your array[0]. Perhaps the best way to visualize this is

array: [
[0,0,0,0],
]

vs

array: [0,0,0,0]
+3
source

:

In [91]: ll=[0,1,2]
In [92]: ll1=[[0,1,2]]
In [93]: len(ll)
Out[93]: 3
In [94]: len(ll1)
Out[94]: 1
In [95]: len(ll1[0])
Out[95]: 3

ll - 3 . ll1 - 1 ; . , , , , ..

2

In [96]: np.array(ll)
Out[96]: array([0, 1, 2])
In [97]: _.shape
Out[97]: (3,)
In [98]: np.array(ll1)
Out[98]: array([[0, 1, 2]])
In [99]: _.shape
Out[99]: (1, 3)

2d. numpy , , . array(ll)[None,:] (1,3), array(ll1).ravel() (3,).

- , , Python .

+2

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


All Articles