The function of the shape function is to read the length of the matrix. For example, shape[0] is the length of the first dimension of the matrix, which is equivalent to the number of rows. Its input parameters can be an integer representing a dimension or a matrix. The shape function returns a tuple representing the dimensions of an array (matrix). Examples are as follows:
1. When an array (matrix) has only one dimension, shape only shape[0], which returns the number of elements in the one-dimensional array (matrix). In layman's terms, it returns the number of columns, because a one-dimensional array has only one row. In one-dimensional case, the array created by array can be regarded as a list (or one-dimensional array). When creating () and [ ], it is OK, but multi-dimensional cannot be like this. Use [ ] here, please see the following example:
>>> a=([1,2]) >>> a array([1, 2]) >>> (2L,) >>> [0] 2L >>> [1] Traceback (most recent call last): File "<pyshell#63>", line 1, in <module> [1] IndexError: tuple index out of range #The last error is because the one-dimensional array has only one dimension, and can be accessed using or [0]
>>> a=((1,2)) >>> a array([1, 2]) #This uses two()pack,The array is the same as before
2. When an array has two dimensions (i.e. rows and columns), like our logical thinking, the returned tuple represents the number of rows and columns of the array. Please see the following example:
>>> a=([[1,2],[3,4]]) #Note that the two-dimensional array should be wrapped with () and []. Type print a and you will get an array (matrix) wrapped with 2 []>>> a array([[1, 2], [3, 4]]) >>> (2L, 2L) >>> b=([[1,2,3],[4,5,6]]) >>> b array([[1, 2, 3], [4, 5, 6]]) >>> (2L, 3L)
3. When the array is three-dimensional, you need to wrap it with one () and two []. Type print a and you will get an array (matrix) wrapped with 3 []. Please see the following example:
>>> a=([[[1,2],[3,4]]]) >>> a array([[[1, 2], [3, 4]]]) >>> (1L, 2L, 2L)
The tuple returned here represents the number of elements contained in each of the three dimensions.
The so-called element is the number of elements in one dimension, the number of rows and columns in two dimensions, the number of [0] in three dimensions represents the number of blocks created, and the number of rows and columns in each block (each block is two-dimensional). For example:
>>> a=([2,2,3])#Create two arrays of 2 rows and 3 columns (matrix)>>> a array([[[ 1., 1., 1.], [ 1., 1., 1.]], [[ 1., 1., 1.], [ 1., 1., 1.]]])
Summary: When creating an array using(),
One-dimensional can be directly ([1,2,3]) or ((1,2,3)), and when outputting (print) is:
>>> print a [1 2 3]
There is a [] package outside;
To use two-dimensional ([[1,2,3],[1,2,3]]), wrap the list to be input with one () and one [], and when outputting (print) is
>>> print a [[1 2 3] [1 2 3]]
There are two [] packages outside;
To use three-dimensional ([[[1,2,3],[1,2,3]]]), wrap the list to be input with one () and two [], and when outputting (print) is
>>> print a [[[1 2 3] [1 2 3]]]
There are three [] packages outside;
For higher dimensions, we will study it later
This is the end of this article about the detailed explanation of the usage of shape functions in Numpy. For more related content on the usage of shape functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!