Detailed Explanation of the Usage of shape Function in Numpy

  • 2021-10-13 08:07:15
  • OfStack

The function of shape is to read the length of the matrix. For example, shape [0] is the length of the first dimension of the read matrix, which is equivalent to the number of rows. Its input parameter can be 1 integer representing dimension or 1 matrix. The shape function returns a tuple that represents the dimension of an array (matrix), as shown in the following example:

1. When the array (matrix) has only one dimension, shape only has shape [0], which returns the number of elements in the 1-dimensional array (matrix). In popular terms, it returns the number of columns, because the 1-dimensional array has only one row. In the 1-dimensional case, array can be regarded as list (or 1-dimensional array), which can be created with () and [], but multidimensional can't be like this. Here, use [], please see the following example:


>>> a=np.array([1,2])
>>> a
array([1, 2])
>>> a.shape
(2L,)
>>> a.shape[0]
2L
>>> a.shape[1]
Traceback (most recent call last):
 File "<pyshell#63>", line 1, in <module>
  a.shape[1]
IndexError: tuple index out of range  # The last error was reported because 1 Dimensional array only 1 Dimensions, you can use a.shape Or a.shape[0] To access 



>>> a=np.array((1,2))
>>> a
array([1, 2]) # This uses two () packages, the resulting array and the preceding 1 Sample 

2. When an array has two dimensions (i.e. rows and columns), like our logical thinking 1, the tuples returned by a. shape represent the number of rows and columns of the array. Please see the following example:


>>> a=np.array([[1,2],[3,4]])  # Attention 2 The dimension array should be used with () and []1 Pick up the package and type print a  Will get 1 Personal use 2 A [] Wrapped array (matrix) 
>>> a
array([[1, 2],
    [3, 4]])
>>> a.shape
(2L, 2L)
>>> b=np.array([[1,2,3],[4,5,6]])
>>> b
array([[1, 2, 3],
    [4, 5, 6]])
>>> b.shape
(2L, 3L)

3. When the array is 3-dimensional, to wrap it in one () and two [], typing print a will give you an array (matrix) wrapped in three [], as shown in the following example:


>>> a=np.array([[[1,2],[3,4]]])
>>> a
array([[[1, 2],
    [3, 4]]])
>>> a.shape
(1L, 2L, 2L)

The tuple returned here represents the number of elements contained in each of the three dimensions.
The so-called elements are the number of elements in 1 dimension, the number of rows and columns in 2 dimension, the number of blocks created in a. shape "0" in 3 dimension, and the number of rows and columns per block (every block is 2 dimension) in a. shape "1" and a. shape "2", for example:


>>> a=np.ones([2,2,3])# Create two 2 Row 3 Array of columns (matrix) 
>>> a
array([[[ 1., 1., 1.],
    [ 1., 1., 1.]],
 
    [[ 1., 1., 1.],
    [ 1., 1., 1.]]])

Summary: When you create an array using np. array (),

One dimension can be directly np. array ([1, 2, 3]) or np. array ((1, 2, 3)), and the output (print) is:


>>> print a
[1 2 3]

There is a [] package outside;

For the 2D, np. array ([[1, 2, 3], [1, 2, 3]]) is used, the list to be input is wrapped with a () and a [], and the output (print) is


>>> print a
[[1 2 3]
 [1 2 3]]

There are two [] parcels outside;

For 3D, use np. array ([[1, 2, 3], [1, 2, 3]]), wrap the list to be input with one () and two [], and output (print) is


>>> print a
[[[1 2 3]
 [1 2 3]]]

There are three [] parcels outside;

For higher dimensions, we will study it later


Related articles: