简体   繁体   中英

what's the difference between these two numpy array shape?

In [136]: s = np.array([[1,0,1],[0,1,1],[0,0,1],[1,1,1]])                                                          

In [137]: s                                                                                                        
Out[137]: 
array([[1, 0, 1],
       [0, 1, 1],
       [0, 0, 1],
       [1, 1, 1]])

In [138]: x = s[0:1]                                                                                               

In [139]: x.shape                                                                                                  
Out[139]: (1, 3)

In [140]: y = s[0]                                                                                                 

In [141]: y.shape                                                                                                  
Out[141]: (3,)

In [142]: x                                                                                                        
Out[142]: array([[1, 0, 1]])

In [143]: y                                                                                                        
Out[143]: array([1, 0, 1])

In the above code, x's shape is (1,3) and y's shape is(3,).

(1,3): 1 row and 3 columns
(3,): How many rows and columns in this case?

Does (3,) represent 1-dimension array?

In practice, if I want to iterate through the matrix row by row, which way should I go?

for i in range(len(x)):
    row = x[i]
    # OR
    row = x[i:i+1]

First, you can get the number of dimensions of an numpy array array through len(array.shape) .

An array with some dimensions of length 1 is not equal to an array with those dimensions removed, for example:

>>> a = np.array([[1], [2], [3]])
>>> b = np.array([1, 2, 3])
>>> a
array([[1],
       [2],
       [3]])
>>> b
array([1, 2, 3])
>>> a.shape
(3, 1)
>>> b.shape
(3,)
>>> a + a
array([[2],
       [4],
       [6]])
>>> a + b
array([[2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])

Conceptually, the difference between an array of shape (3, 1) and one of shape (3,) is like the difference between the length of [100] and 100 .

[100] is a list that happens to have one element. It could have more, but right now it has the minimum possible number of elements .

On the other hand, it doesn't even make sense to talk about the length of 100 , because it doesn't have one.

Similarly, the array of shape (3, 1) has 3 rows and 1 column, while the array of shape (3,) has no columns at all . It doesn't even have rows, in a sense; it is a row, just like 100 has no elements, because it is an element.

For more information on how differently shaped arrays behave when interacting with other arrays, you can see the broadcasting rules .

Lastly, for completeness, to iterate through the rows of a numpy array, you could just do for row in array . If you want to iterate through the back axes, you can use np.moveaxis , for example:

>>> array = np.array([[1, 2], [3, 4], [5, 6]])
>>> for row in array:
...     print(row)
...
[1 2]
[3 4]
[5 6]
>>> for col in np.moveaxis(array, [0, 1], [1, 0]):
...     print(col)
...
[1 3 5]
[2 4 6]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM