Numpy provides concatenate, append, stack class (including hsatck, vstack, dstack, row_stack, column_stack), r_ and c_ and other classes and functions for array stitching operations.
The characteristics and differences of the various functions are labeled below:
concatenate | The axis parameter is provided to specify the direction of the splice |
---|---|
append | The default is to ravel and then splice into one-dimensional arrays, but you can also specify axis |
stack | The axis parameter is provided for generating new dimensions |
hstack | Horizontal splicing, splicing the columns along the rows |
vstack | Vertical splicing, splicing rows along columns |
dstack | Splice along the third axis (depth direction) |
column_stack | Horizontal splicing, splicing the columns along the rows |
row_stack | Vertical splicing, splicing rows along columns |
r_ | Vertical splicing, splicing rows along columns |
c_ | Horizontal splicing, splicing the columns along the rows |
Direct Merge
Combines two one-dimensional arrays into a two-dimensional array:
import torch import numpy as np import as plt a = (0,15,0.1) b = 1.088 * a + 0.638 + () * 10 print(,) points = ([a,b]) print() (150,) (150,) (2, 150)
append splice
append(arr, values, axis=None)
arr | Duplication of the arrays to be merged (the special home page is a duplicate, so it takes a lot more memory) |
values | Used to merge in the values copied from the above array. These values must match the shape of arr if the following parameter axis is specified (everything outside of shape[axis] is equal), otherwise there is no requirement. |
axis | Axes to be merged. |
>>> import numpy as np >>> ar1 = ([[1,2,3], [4,5,6]]) >>> ar2 = ([[7,8,9], [11,12,13]]) >>> (ar1, ar2) # ravel flattens and splices, so the return value is a 1-dimensional array. array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13]) >>> (ar1, ar2, axis=0) # Splice along the first axis, in this case the direction of the rows array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [11, 12, 13]]) >>> (ar1, ar2, axis=1) # Splice along the second axis, in this case the direction of the columns array([[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 11, 12, 13]])
concatenate splice
concatenate(a_tuple, axis=0, out=None)
a_tuple: | The arrays to be merged are given in the form of tuples |
axis | The axis to be merged, default is 0 |
>>> import numpy as np >>> ar1 = ([[1,2,3], [4,5,6]]) >>> ar2 = ([[7,8,9], [11,12,13]]) >>> ar1 array([[1, 2, 3], [4, 5, 6]]) >>> ar2 array([[ 7, 8, 9], [11, 12, 13]]) >>> ((ar1, ar2)) # Here the first axis (axis 0) is the row direction array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [11, 12, 13]]) >>> ((ar1, ar2),axis=1) # Here's a splice along the second axis, the column direction # array([[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 11, 12, 13]]) >>> ar3 = ([[14,15,16]]) # 2-dimensional array with shape (1, 3) >>> ((ar1, ar3)) # General concatenate operation of the array shape needs to be consistent, of course, if the array in the splicing axis direction of the size is not the same, you can also complete the >>> ((ar1, ar3)) # ar3 is consistent in the axis1 direction although the length is not consistent in the axis0 direction, so it can be spliced along axis0 array([[ 1, 2, 3], [ 4, 5, 6], [14, 15, 16]]) >>> ((ar1, ar3), axis=1) # ar3 and ar1 are not the same length in axis0 direction, so an error is reported
hstack
>>> ((ar1,ar2)) # Horizontal splicing, splicing of columns along the rows array([[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 11, 12, 13]])
vstack
>>> ((ar1,ar2)) # Vertical splicing, splicing rows along columns array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [11, 12, 13]])
vstack
>>> ((ar1,ar2)) # For a 2-dimensional array, splice along the third axis (depth direction), which is equivalent to stack(axis=-1) array([[[ 1, 7], [ 2, 8], [ 3, 9]], [[ 4, 11], [ 5, 12], [ 6, 13]]])
column_stack and row_stack
>>> np.column_stack((ar1,ar2)) # Horizontal splicing, splicing of columns along the rows array([[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 11, 12, 13]]) >>> np.row_stack((ar1,ar2)) # Vertical splicing, splicing rows along columns array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [11, 12, 13]])
np.r_ and np.c_
Commonly used to quickly generate ndarray data
>>> np.r_[ar1,ar2] # Vertical splicing, splicing rows along columns array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [11, 12, 13]]) >>> np.c_[ar1,ar2] # Horizontal splicing, splicing of columns along the rows array([[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 11, 12, 13]])
to this article on the numpy array merge and matrix splicing implementation of the article is introduced to this, more related numpy array merge and matrix splicing content, please search for my previous articles or continue to browse the following articles hope that you will support me in the future more!