The shape and reshape functions of numpy arrays in Python

np.shape() and np.reshape() functions

Reference blog post: https://blog.csdn.net/qq_28618765/article/details/78083895

最近,主要在做一些数据分析与机器视觉相关项目,经常使用到np.reshape(),np.reshape()函数。虽然对它们有一个基本认识,但是每次用具体使用方法及意义总是模糊,这里自己总结一下。

shape function
The shape function in Python is mainly used to return the dimension of the array. In actual use, reshape is not commonly used, but because the function name is similar to reshape, it is easy to learn and remember by comparing the two. The np.shape() function is used as follows.
np.shape() function
It can be seen that the shape function is mainly used to return the array structure, and query different depth dimensions with 0, 1, 2... respectively. When the array does not have a dimension at this depth, an error will be reported. Note: The shape function is followed by square brackets [ ], and the reshape function is followed by parentheses ( ). Additionally, the entire array structure can be returned with y.shape.
insert image description here
reshape function
The reshape function mainly uses the dimensionality reconstruction of the array, which is very common in the processing of vectors.
insert image description here
The official introduction to the use of reshape is y.reshape((2,3)), but the abbreviation y.reshape(2,3) will not go wrong.

insert image description here
In addition, in the reshape function, the parameter can be assigned a value of -1, but the condition is: on dimensions other than the -1 dimension, it can only be used when the parameter value is determined and the array structure is unique, that is, the number of -1 cannot exceed 1 at most indivual. Finally, notice the usage of y.reshape(-1,) and y.reshape(, -1) is wrong.
Finally, the new array generated by the reshape function shares memory with the original array, so if one of the original array or the new array changes, the other changes accordingly.
insert image description here

Guess you like

Origin blog.csdn.net/qq_38606680/article/details/90145643