numpy 提供了丰富的数组拼接和合并操作
对行进行拼接
np.concatenate((ar1, ar2),axis=0)
np.append(ar1, ar2, axis=0)
np.vstack((ar1,ar2))
np.row_stack((ar1,ar2))
np.r_[ar1,ar2]
对列进行拼接
np.concatenate((ar1, ar2),axis=1)
np.append(ar1, ar2, axis=1)
np.hstack((ar1,ar2))
np.column_stack((ar1,ar2))
np.c_[ar1,ar2]
attention
The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to hstack only for 2D arrays
a=np.array([1,2])
b=np.array([3,4])
res=np.column_stack((a,b))
print(res)
res=np.hstack((a,b))
print(res)
[[1 3]
[2 4]]
[1 2 3 4]
注意区别
column_stack 只有在二维数组时才与hstack相等
row_stack 等同于vstack