Realization of numpy Array Combination and Matrix Splicing

  • 2021-10-13 08:08:34
  • OfStack

concatenate, append, stack classes (including hsatck, vstack, dstack, row_stack, column_stack), r_, c_ and other classes and functions are provided in Numpy for array splicing operations.

The characteristics and differences of various functions are as follows:

concatenate 提供了axis参数,用于指定拼接方向
append 默认先ravel再拼接成1维数组,也可指定axis
stack 提供了axis参数,用于生成新的维度
hstack 水平拼接,沿着行的方向,对列进行拼接
vstack 垂直拼接,沿着列的方向,对行进行拼接
dstack 沿着第3个轴(深度方向)进行拼接
column_stack 水平拼接,沿着行的方向,对列进行拼接
row_stack 垂直拼接,沿着列的方向,对行进行拼接
r_ 垂直拼接,沿着列的方向,对行进行拼接
c_ 水平拼接,沿着行的方向,对列进行拼接

Direct merger

Combine two 1-dimensional arrays into one 2-dimensional array:


import torch
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(0,15,0.1)
b = 1.088 * a + 0.638 + np.random.rand() * 10

print(a.shape,b.shape)
points = np.array([a,b])
print(points.shape)


(150,) (150,)
(2, 150)

append splicing


append(arr, values, axis=None)

arr 待合并的数组的复制(特别主页是复制,所以要多耗费很多内存)
values 用来合并到上述数组复制的值。如果指定了下面的参数axis的话,则这些值必须和arr的shape1致(shape[axis]之外都相等),否则的话,则没有要求。
axis 要合并的轴.


>>> import numpy as np
>>> ar1 = np.array([[1,2,3], [4,5,6]])
>>> ar2 = np.array([[7,8,9], [11,12,13]])

>>> np.append(ar1, ar2) #  First ravel Flattening and splicing, so the return value is 1 A 1 Dimensional array 
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13])

>>> np.append(ar1, ar2, axis=0)  #  Along the first 1 Axis splicing, here is the direction of the row  
array([[ 1, 2, 3],
  [ 4, 5, 6],
  [ 7, 8, 9],
  [11, 12, 13]])

>>> np.append(ar1, ar2, axis=1)  #  Along the first 2 Axis splicing, here is the direction of the column  
array([[ 1, 2, 3, 7, 8, 9],
  [ 4, 5, 6, 11, 12, 13]])

concatenate splicing


concatenate(a_tuple, axis=0, out=None)

a_tuple: 对需要合并的数组用元组的形式给出
axis 待合并的轴,默认为0


 >>> import numpy as np
>>> ar1 = np.array([[1,2,3], [4,5,6]])
>>> ar2 = np.array([[7,8,9], [11,12,13]])
>>> ar1
array([[1, 2, 3],
  [4, 5, 6]])
>>> ar2
array([[ 7, 8, 9],
  [11, 12, 13]])

>>> np.concatenate((ar1, ar2)) #  The number here 1 Shaft (axis 0) Is the line direction 
array([[ 1, 2, 3],
  [ 4, 5, 6],
  [ 7, 8, 9],
  [11, 12, 13]])

>>> np.concatenate((ar1, ar2),axis=1) #  Here along the first 2 Three axes, that is, the column direction, are spliced 
array([[ 1, 2, 3, 7, 8, 9],
  [ 4, 5, 6, 11, 12, 13]])

>>> ar3 = np.array([[14,15,16]]) # shape For ( 1 , 3 ) 2 Dimensional array 
>>> np.concatenate((ar1, ar3)) # 1 Go on like concatenate Operational array Adj. shape Need 1 To, of course, if array In splicing axis Directional size No 1 Sample, can also be completed 
>>> np.concatenate((ar1, ar3)) # ar3 Although in axis0 The length of the direction is not 1 To, but axis1 In the direction 1 To, so along axis0 Can be spliced 
array([[ 1, 2, 3],
  [ 4, 5, 6],
  [14, 15, 16]])
>>> np.concatenate((ar1, ar3), axis=1) # ar3 And ar1 In axis0 The length of the direction is not 1 To, so report an error 

hstack


>>> np.hstack((ar1,ar2)) #  Horizontal splicing, splicing columns along the direction of rows 
array([[ 1, 2, 3, 7, 8, 9],
  [ 4, 5, 6, 11, 12, 13]])

vstack


>>> np.vstack((ar1,ar2)) #  Vertical splicing, splicing rows in the direction of columns 
array([[ 1, 2, 3],
  [ 4, 5, 6],
  [ 7, 8, 9],
  [11, 12, 13]])

vstack


>>> np.dstack((ar1,ar2)) #  For 2 Dimension array, along the first 3 Shaft (depth direction) for splicing ,  The effect 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 columns along the direction of rows 
array([[ 1, 2, 3, 7, 8, 9],
  [ 4, 5, 6, 11, 12, 13]])

>>> np.row_stack((ar1,ar2)) #  Vertical splicing, splicing rows in the direction of 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 in the direction of columns 
array([[ 1, 2, 3],
  [ 4, 5, 6],
  [ 7, 8, 9],
  [11, 12, 13]])
 
>>> np.c_[ar1,ar2] #  Horizontal splicing, splicing columns along the direction of rows 
array([[ 1, 2, 3, 7, 8, 9],
  [ 4, 5, 6, 11, 12, 13]])

Related articles: