Starfruit Python Advanced Lecture 17-Array array (seven) three-dimensional array and n-dimensional array index and value (with detailed illustration)

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

 

Three-dimensional array index and value

Create a numpy three-dimensional array z as follows:

>>> import numpy as np
>>> z=np.array([[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]])
>>> print(z)
[[[ 1  2  3  4]
  [ 5  6  7  8]]

 [[ 9 10 11 12]
  [13 14 15 16]]]

The three-dimensional array does not have the concept of rows and columns of the matrix, but we can use the shape () function to view its dimension information:

>>> np.shape(z)
(2, 2, 4)

The dimension of the three-dimensional array z is (2, 2, 4), is it quite difficult to understand!

In fact , transforming (2, 2, 4) into (2, (2, 4)) , that is, two (2, 4) matrices , becomes a three-dimensional array!

Illustrated as follows, understood to be a three-dimensional array z is 2 th (2, 4) in the plane of the matrix configuration

We continue to use the index method of the two-dimensional array to view the three-dimensional array, as follows:

If [] starts with a number or a single colon , it means to take all the elements of the matrix plane , for example:

>>> z[0]             #取第0个矩阵平面的元素
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
>>> z[1]             #取第1个矩阵平面的元素
array([[ 9, 10, 11, 12],
       [13, 14, 15, 16]])
>>> z[:1]            #取从0开始到1-1=0个矩阵平面的元素
array([[[1, 2, 3, 4],
        [5, 6, 7, 8]]])

Icon:

If [] starts with a colon comma (ie :, ), it means to take the row elements of the matrix plane , for example:

>>> z[:,0]       #取所有矩阵平面的第0行
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])
>>> z[:,1]       #取所有矩阵平面的第1行
array([[ 5,  6,  7,  8],
       [13, 14, 15, 16]])

Icon:

Taken to specify a matrix plane in a row , using [matrix plane numbers] [row number] represented

Taken to specify a matrix plane of a column , using [planar matrix number] [column number :,] represented

Examples:

>>> z[0][1]          #取第0个矩阵平面的第1行
array([5, 6, 7, 8])
>>> z[1][0]          #取第1个矩阵平面的第0行
array([ 9, 10, 11, 12])
>>> z[0][:,2]        #取第0个矩阵平面的第2列
array([3, 7])   
>>> z[1][:,3]        #取第1个矩阵平面的第3列
array([12, 16])

Icon:

If [] starts with two colons and commas (ie :,:, ), it means to take the column elements of the matrix plane , for example:

>>> z[:,:,0]         #取所有矩阵平面的第0列
array([[ 1,  5],
       [ 9, 13]])
>>> z[:,:,2]         #取所有矩阵平面的第2列
array([[ 3,  7],
       [11, 15]])

Icon:

Take a specific element of a three-dimensional array and use [matrix plane number] [row number] [column number] to represent it, for example:

>>> z[0][1][2]     #取第0个矩阵平面第1行第2列的数据
7
>>> z[1][1][1]     #取第1个矩阵平面第1行第1列的数据
14

Icon:

Index and value of n-dimensional array

For n-dimensional arrays that are more than three-dimensional, it is difficult to draw graphics in an intuitive way, but we can still follow the principle of three-dimensional arrays for indexing and value

The shape of an n-dimensional array can be expressed as:

The rightmost  x1 is a one-dimensional (where the elements are ultimately stored), x2 is the number of one-dimensional elements , x3 is the number of two-dimensional elements ... The leftmost xn is the number of n-1 dimension elements.

Examples:

(6,2,7,3,4) is a five-dimensional array s, with four elements in one dimension, three one-dimensional elements in two dimensions, seven two-dimensional elements in three dimensions, and four-dimensional elements The number of 2 three-dimensional elements, five-dimensional has 6 four-dimensional elements, the total number of elements is 4 * 3 * 7 * 2 * 6 = 1008

Then when s [a] has only a pair of square brackets, there are 6 elements in total, ranging from s [0] to s [5])

When s [a] [b] has two pairs of brackets, a total of 6 * 2 = 12 elements, ranging from s [0] [0] to s [0] [1], and then from s [1] [ 0] to s [1] [1] ... until s [5] [0] to s [5] [1]

When s [a] [b] [c] has three pairs of brackets, a total of 6 * 2 * 7 = 84 elements, ranging from s [0] [0] [0] to s [0] [0] [ 6], then from s [0] [1] [0] to s [0] [1] [6],

From s [1] [0] [0] to s [5] [1] [6]

……

Therefore, you can take multiple loops to traverse the elements of the five-dimensional array s, from the innermost layer to the outermost layer:

First level loop: from s [0] [0] [0] [0] [ 0 ] to s [0] [0] [0] [0] [ 3 ] (4 loops)

Second layer loop: from s [0] [0] [0] [ 0 ] [0] to s [0] [0] [0] [ 2] [3] (3 cycles)

Third level loop: from s [0] [0] [ 0 ] [0] [0] to s [0] [0] [ 6 ] [2] [3] (7 loops)

Fourth level loop: from s [0] [ 0 ] [0] [0] [0] to s [0] [ 1 ] [6] [2] [3] (2 loops)

Fifth cycle: from s [ 0 ] [0] [0] [0] [0] to s [ 5 ] [1] [6] [2] [3] (6 cycles)

During the traversal, each layer of the loop must perform the corresponding number of the next layer of loops, so a total of 6 * 2 * 7 * 3 * 4 = 1008 cycles.

 

to sum up

A three-dimensional array is a plurality a two-dimensional matrix of plane configuration

If [] starts with a number or a single colon , it means taking all the elements of the matrix plane

If [] starts with a colon comma (ie :, ), it means to take the row elements of the matrix plane

Taken to specify a matrix plane in a row , using [matrix plane numbers] [row number] represented

Taken to specify a matrix plane of a column , using [planar matrix number] [column number :,] represented

If [] starts with two colons and commas (ie :,:, ), it means to take the column elements of the matrix plane

Take a specific element of a three-dimensional array and use [matrix plane number] [row number] [column number] to express

The shape of an n-dimensional array can be expressed as:

The rightmost  x1 is a one-dimensional (where the elements are ultimately stored), x2 is the number of one-dimensional elements , x3 is the number of two-dimensional elements ... The leftmost xn is the number of n-1 dimension elements.

When traversing an n-dimensional array, each layer of the loop must perform the corresponding number of cycles of the next layer, so a total of xn * xn-1 * ...... * x3 * x2 * x1 cycles.

 

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

If you think this chapter is helpful to you, welcome to follow, comment and like! Github welcomes your Follow and Star!

Published 55 original articles · won praise 16 · views 6111

Guess you like

Origin blog.csdn.net/yty_7/article/details/104702256