Visual understanding of high-dimensional arrays in NumPy

Basic knowledge

The ndarray object in NumPy is a multi-dimensional array of the same data type.


  • The number of rank (ndarray.ndim) axes, or the number of dimensions, is a scalar.
  • The array dimension (ndarray.shape)
    is a tuple that describes the size of the array in each dimension. Relative to a matrix, shape represents n rows and m columns. The length of the tuple is equivalent to the number of axes/dimensions, that is, the rank value.

Insert picture description here
Insert picture description here

Calculation principle when setting axis

  • Create array
    Insert picture description here

  • The output result is 3 channels, 4 rows, 2 columns
    Insert picture description here

  • Correspondence
    numpy When creating an array, each additional [] means adding a dimension, one [] can be regarded as a complete block, there are several blocks under each layer [], and the value corresponding to shape is how many.
    Insert picture description here

  • Calculation principle when axis=0
    Insert picture description here
    Insert picture description here
    Insert picture description here

  • Calculation principle when axis=1
    Insert picture description here
    Insert picture description here

  • Calculation principle when axis=2
    Insert picture description here
    Insert picture description here

High-dimensional transpose() visualization

  • The transposition of a one-dimensional array is still its own, without distinguishing between row vector and column vector: numpy.T.
  • The transposition of a two-dimensional array is the same as the matrix transposition of linear algebra: numpy.T.
  • The transpose of high-dimensional arrays requires the numpy method: transpose(). The transposition of high-dimensional arrays is more difficult to understand, and you cannot imagine the transposition of linear algebra. The transposition here is the change of the position of the underlying number, and the transformation rules follow the rules of the transpose() method.

Index conversion perspective understanding

Insert picture description here

Coordinate axis change angle understanding

  • Create a multidimensional array
    Insert picture description here

  • The expression
    3 of the high-order matrix in NumPy is that the shape of the array b is (2,2,4), which means that there are two 2x4 matrices:
    Insert picture description here

  • Position in high-dimensional coordinate system
    Insert picture description here

  • Position after coordinate axis transformation
    Insert picture description here

  • Visual changes before and after the coordinate axis changes
    Insert picture description here

  • Move the transformed position intact to the original coordinate system to obtain a new array of coordinate axis transformation thickness.
    Insert picture description here

  • In the original coordinate system, the newly placed array shape is still (2,2,4), that is, it contains two 2x4 matrices. The two matrices are sorted in order on the 0 axis, and they are the green cube and the blue cube in the figure.
    Insert picture description here

  • Changes in all 3 axes
    Insert picture description here
    Insert picture description here
    Insert picture description here
    Insert picture description here

Visualization of 1D/2D data processing

  • Create array
    Insert picture description here
    Insert picture description here

  • Array arithmetic
    Insert picture description here
    Insert picture description here
    Insert picture description here
    Insert picture description here

  • index
    Insert picture description here

  • polymerization
    Insert picture description here

  • Create matrix
    Insert picture description here
    Insert picture description here

  • Matrix arithmetic
    Insert picture description here
    Insert picture description here

  • Dot product
    Insert picture description here
    Insert picture description here

  • Matrix index
    Insert picture description here

  • Matrix aggregation
    Insert picture description here
    Insert picture description here

  • Transpose and reshape
    Insert picture description here
    Insert picture description here

  • More dimensions
    Insert picture description here
    Insert picture description here

[References]
The meaning of dimension, axis, and rank in
NumPy Visualization of NumPy data processing Visualization of
transpose() function in Numpy Visual understanding
of Numpy.transpose()
numpy matrix dimension axis in Python Parameter understanding

Guess you like

Origin blog.csdn.net/studyeboy/article/details/110953458