# Guide package Import numpy AS NP
numpy.array merger
.concatenate()
One-dimensional array
x = np.array([1, 2, 3]) # array([1, 2, 3]) y = np.array([3, 2, 1]) # array([3, 2, 1])
np.concatenate([x, y]) # array([1, 2, 3, 3, 2, 1]) z = np.array([666, 666, 666]) # array([666, 666, 666]) np.concatenate([x, y, z]) """ array([ 1, 2, 3, 3, 2, 1, 666, 666, 666]) """
Two-dimensional array
.concatenate ((A, B, C, ...), axis = 0) : By default, axis = 0 can not write, axis is the direction of stitching, stitching direction can be understood as the number of direction changes occur after the splice is completed, 0 on the horizontal axis, vertical axis 1
axis = 0: the array in the column spliced, splicing direction as the horizontal axis and the vertical axis requires the same configuration
axis = 1: the array in rows are spliced, the splice of the longitudinal axis direction, the horizontal axis requires the same configuration
A = np.array([[1, 2, 3], [4, 5, 6]]) """ array([[1, 2, 3], [4, 5, 6]]) """ np.concatenate([A, A]) """ array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]) """ np.concatenate([A, A], axis=1) """ array([[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]]) """ np.concatenate([A, z]) # 错误 np.concatenate([A, z.reshape(1, -1)]) """ array([[ 1, 2, 3], [ 4, 5, 6], [666, 666, 666]]) """
.hstack()
Function prototype: numpy.hstack (tup) , the parameter may be a tup tuple list, or numpy array, the array must have the same shape, except that the dimension corresponding to the axis (by default, the first), the result is returned numpy array
a=[1,2,3] b=[4,5,6] np.hstack((a,b)) # array([1, 2, 3, 4, 5, 6])
a=[[1],[2],[3]] b=[[1],[2],[3]] c=[[1],[2],[3]] d=[[1],[2],[3]] np.hstack((a,b,c,d)) """ array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]) """
It is actually level (by column order) to the stacked array
.vstack ()
Function prototype: numpy.vstack (tup) , the parameter may be a tup tuple list, or numpy array, the result is returned array numpy
a=[1,2,3] b=[4,5,6] np.vstack((a,b)) """ array([[1, 2, 3], [4, 5, 6]]) """
a=[[1],[2],[3]] b=[[1],[2],[3]] c=[[1],[2],[3]] d=[[1],[2],[3]] np.vstack((a,b,c,d)) """ array([[1], [2], [3], [1], [2], [3], [1], [2], [3], [1], [2], [3]]) """
It is a vertical (line sequentially) in the stacked array to
numpy.array division
split
split(ary, indices_or_sections, axis=0):把一个数组从左到右按顺序切分
ary: To an array of cut points
indices_or_sections: If is an integer, on average by the segmentation, if an array, sliced along the axis position (left and right open-closed)
Axis: conducted tangentially along which dimension, The default is 0, horizontal segmentation; is 1, sectioned longitudinally
x = np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) x1, x2, x3, x4, x5 = np.split(x, [2, 4, 5, 7]) """ x1 --> array([0, 1]) x2 --> array([2, 3]) x3 --> array([4]) x4 --> array([5, 6]) x5 --> array([7, 8, 9]) """
A = np.arange(16).reshape(4, 4) A1, A2 = np.split(A, [2]) """ A1 --> array([[0, 1, 2, 3], [4, 5, 6, 7]]) A2 --> array([[ 8, 9, 10, 11], [12, 13, 14, 15]]) """ A1, A2 = np.split(A, [2], axis=1) """ A1 --> array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]) A2 --> array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]]) """
hsplit
The number of array by specifying the same shape to be returned, similar axis = 1
vsplit
vsplit divided along the vertical axis, similar to the axis = 0
upper, lower = np.vsplit(A, [2]) """ upper --> array([[0, 1, 2, 3], [4, 5, 6, 7]]) """ left, right = np.hsplit(A, [2]) """ left --> array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]) """