python:np.vstack, np.hstack

一、np.vstack(tup)使用

沿着竖直方向将矩阵堆叠起来。
Note: the arrays must have the same shape along all but the first axis. 除开第一维外,被堆叠的矩阵各维度要一致。
示例代码:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
res = np.vstack((arr1, arr2))

结果如下:

array([[1, 2, 3],
       [4, 5, 6]])


二、np.hstack(tup)

沿着水平方向将数组堆叠起来。
Note:
tup : sequence of ndarrays
All arrays must have the same shape along all but the second axis.
示例代码:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
res = np.hstack((arr1, arr2))

print res


arr1 = np.array([[1, 2], [3, 4], [5, 6]])
arr2 = np.array([[7, 8], [9, 0], [0, 1]])
res = np.hstack((arr1, arr2))

print res

结果如下:

[1 2 3 4 5 6]

[[1 2 7 8]
 [3 4 9 0]
 [5 6 0 1]]

 

原博:https://blog.csdn.net/u012609509/article/details/70319293

おすすめ

転載: blog.csdn.net/weixin_39450145/article/details/115603435