Numpy: Shape operation of array (Ndarray) operation


shape manipulation

The operations on the shape of the array can be divided into three types: in-place variable dimension, view variable dimension, and copy variable dimension, each of which has corresponding functions to implement operations. First you need to understand the basic concepts of views and replicas: a replica is a copy of the data. A view is to share a piece of data with the original data.

In-place variable dimension

Modifying the shape of the array in memory will change the shape of the original array.

method illustrate
ndarray.resize() Change the shape of an array in-place
ndarray.shape = () Assign a value directly to the shape property of an array

ndarray.resize()

ndarray.resize(new_shape, refcheck=True)

Change the shape and size of the array in place.

Parameter Description:

  • new_shape: Receives a tuple of integers, or integers. The tuple is the changed array shape, such as (2, 3) with two rows and three columns.
  • refcheck: Receives a value of type bool. Defaults to True, if changed to False, the reference count will not be checked.

return value:

  • none

Example:

>>> a = np.arange(1,7)
>>> a
array([1, 2, 3, 4, 5, 6])
>>> a.resize((2, 3)) # 或者a.resize(2, 3)
>>> a
array([[1, 2, 3],
       [4, 5, 6]])

ndarray.shape

Direct assignment to the properties of ndarray.shape can also perform in-place dimension changes. An example is as follows:

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

View variable dimension

View variable dimension only changes the shape of the array output, while the shape of the array in memory remains the same.

method illustrate
ndarray.reshape () Returns an array view of the new shape
ndarray.ravel() returns a flattened array

ndarray.reshape ()

Returns an array view of the new shape.

ndarray.reshape(shape, order='C')

Parameter Description:

  • shape: shape, accepts a tuple or an integer.
  • order: Defaults to 'C'.

return value:

  • array view

Example:

>>> a = np.arange(1,7)
>>> a
array([1, 2, 3, 4, 5, 6])
>>> a.reshape((2,3)) # 或者a.reshape(2,3)
array([[1, 2, 3],
       [4, 5, 6]])

ndarray.ravel()

ndarray.ravel([order])

Returns a stretched and flattened array, the data is not independent, and the data is shared with the original array.

Parameter Description:

  • order: optional {'C', 'F', 'A', 'K'}

return value:

  • ndarray

Example:

>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> x
array([[1, 2, 3],
       [4, 5, 6]])
>>> x.ravel() 
array([1, 2, 3, 4, 5, 6])
>>> x # 数组x本身没有发生变化
array([[1, 2, 3],
       [4, 5, 6]])

Copy variable dimension

Output a copy of the array, and the original array belongs to two separate arrays, does not affect the shape of the original array.

method illustrate
ndarray.flatten () Returns a copy flattened into a one-dimensional array.

ndarray.flatten ()

ndarray.flatten(order='C')

Returns a copy expanded into a one-dimensional array.

Parameter Description:

  • order: optional {'C', 'F', 'A', 'K'}, default 'C'

Return value: ndarray

  • A copy of a 1D array.

Example:

>>> a = np.array([[1,2], [3,4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.flatten() # 返回的是副本,数据与原数组a独立。
array([1, 2, 3, 4])

For the above three introductions to changing the shape of an array, you can choose the corresponding transformation method according to the actual application scenario. There are corresponding functions under each changing method. In the early learning, you should first master what functions the functions can achieve. You don’t need to memorize the syntax. The parameters of the function are really different. Stopped using the process mastered.

Guess you like

Origin blog.csdn.net/shield911/article/details/124225072
Recommended