Numpy depth of decryption in shape

wedge

Regardless of the data analysis, data mining, machine learning, etc., will often encounter shape the operation of the array. Then you have ever wondered why a one-dimensional array, can shape into a high-dimensional arrays of any dimension of it.

NumPy also provides efficient data shape operation, it is how to achieve this operation, what data structure to use, what is the principle?

After figure out this problem, go use NumPy, TensorFlow, it will be much clearer moments.

Decryption shape

A one-dimensional array, a length of 12, why can change a two-dimensional (12,1) or (2,6), D (12,1,1) or (2,3,2), four-dimensional (12,1, 1,1) or (2,3,1,2) do? In short, why can change it to any number of dimensions

Here we take a look, first import numpy

import numpy as np

# 创建一个数组a,从0开始,间隔为2,包含12个元素
a = np.arange(0, 24, 2)

# 打印一下
print(a)  # [ 0  2  4  6  8 10 12 14 16 18 20 22]

As the array a, NumPy will be interpreted into two structures, a buffer, and a view.

A schematic view of the buffer is as follows:

is a view to explain the structure of the buffer, such as data types, flags and other information:

print(a.dtype)  # int32
print(a.flags)
"""
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
"""

Using a [6] to access the index array a 6 element. See achieved from behind, will NumPy a secondary axis, the axis value of 0-11. Mode is shown below:

Therefore, by means of the axis i, a [6] will be indexed to the element 12, as follows:

At this point, we have to establish the concept of a shaft. Next, do a reshape change, a change of shape for the array (2,6):

b = a.reshape((2, 6))
print(b)
"""
[[ 0  2  4  6  8 10]
 [12 14 16 18 20 22]]
"""

At this time, NumPy create two axes, the value is assumed to be i, j, i is 0 to 1, j is a value of 0 to 5, a schematic diagram is as follows:

Use b[1][2]or b[1, 2]acquisition element 16 to:

print(b[1, 2])  # 16

The value is divided into two shafts 1 and 2, as shown below, the element 16 is positioned to:

Usually, some may confuse the two shape, (12,) and (12,1), the former is in fact a shaft, which two shafts, a schematic diagram are as follows.

The former is a shaft, ranging from 0 to 11; the latter two axes, I-axis values ​​from 0 to 11, J-axis values ​​from 0-0.

At this point, we have to establish the concept of two axes. In addition, several figures seen through the above, no matter how the shape change, change that view, under the buffer remains unchanged.

Subsequently, up to three axes, a change in the shape of an array of (2,3,2):

c = a.reshape((2, 3, 2))
print(c) 
"""
[[[ 0  2]
  [ 4  6]
  [ 8 10]]

 [[12 14]
  [16 18]
  [20 22]]]
"""

C array has three axes, the values ​​are 0 to 1,0 to 2,0 to 1, a schematic diagram is shown below:

Note that experience, i, j, k three axes, the distribution of their values. If the value of i axis to remove the cells 1:

Actually it corresponds to the first half of the elements of the array c:

c = a.reshape((2, 3, 2))
print(c[0: 1])
"""
[[[ 0  2]
  [ 4  6]
  [ 8 10]]]
"""

So far, reshape already finished three axes, say an interesting question.

Remember, the original one-dimensional array a right? It is a total of 12 elements, then we change it to an array c, shape is (2,3,2), then how to upgrade to 4-dimensional or in any dimension it?

Dimension 4 can be: (1,2,3,2), a schematic diagram is as follows:

See, i the index value only 0 axis, which is called free dimensions, may be inserted between any arbitrary axis of the original array. For example, the dimensions may be 5: (1,2,1,3,2):

At this point, you should fully understand the magic reshape operations:

  • buffer 是个一维数组,永远不变;
  • 变化的 shape 通过 view 传达;
  • 取值仅有 0 的轴为自由轴,它能变化出任意维度。

About reshape operation, the last to say that, after reshape the array, only the view of the view of the original array, copy the behavior of the elements did not happen, so as to ensure reshape operations more efficient.

import numpy as np

a = np.array([1, 2, 3, 4])
b = a.reshape((2, 2))

b[0, 0] = 100
print(a)  # [100   2   3   4]

b is a one view, b changes will affect a. Similar golang feeling a little slice ah.

After understand the mysteries reshape the operation, I believe we have established the concept of multi-axis and the axis, which is the flexible use of high-dimensional array helpful.

Guess you like

Origin www.cnblogs.com/traditional/p/12510615.html