编程语言
首页 > 编程语言> > python——numpy.vstack / numpy.hstack组合矩阵

python——numpy.vstack / numpy.hstack组合矩阵

作者:互联网

numpy.vstack / numpy.hstack分别表示沿着行方向和列方向进行组合矩阵

numpy.vstack(iterable)

        输入参数为可迭代对象,如list或tuple,每个对象为矩阵
        要求输入的不同矩阵除第0维外,其余维度全部相同
        最终结果第0维为所有矩阵第0维之和

In [1]: import numpy
In [2]: dat1 = np.array([[[ 1,  2],
        				  [ 3,  4],
        				  [ 5,  6]],
       					 [[ 7,  8],
        			      [ 9, 10],
        			      [11, 12]]])
In [3]: dat2 = np.array([[[13, 14],
        				  [15, 16],
        				  [17, 18]]])
In [4]: dat1.shape, dat2.shape
Out[4]: ((2, 3, 2), (1, 3, 2))
In [5]: dat = np.vstack((dat1, dat2))
Out[5]: array([[[ 1,  2],
        		[ 3,  4],
        		[ 5,  6]],
       		   [[ 7,  8],
        		[ 9, 10],
        		[11, 12]],
       		   [[13, 14],
        		[15, 16],
        		[17, 18]]]) 
In [6]: dat.shape
Out[6]: (3, 3, 2)       
numpy.hstack(iterable)

        输入参数为可迭代对象,如list或tuple,每个对象为矩阵
        要求输入的不同矩阵除第1维外,其余维度全部相同
        最终结果第1维为所有矩阵第1维之和

In [1]: import numpy
In [2]: dat1 = np.array([[[1, 2, 3],
        				  [4, 5, 6]]])
In [3]: dat2 = np.array([[[1, 2, 3],
        				  [4, 5, 6],
        				  [7, 8, 9]]])
In [4]: dat1.shape, dat2.shape
Out[4]: ((1, 2, 3), (1, 3, 3))
In [5]: dat = np.hstack((dat1, dat2))
Out[5]: array([[[1, 2, 3],
		        [4, 5, 6],
        		[1, 2, 3],
        		[4, 5, 6],
        		[7, 8, 9]]])
In [6]: dat.shape
Out[6]: (1, 5, 3)     

标签:hstack,python,dat1,矩阵,dat2,shape,array,numpy
来源: https://blog.csdn.net/u012633319/article/details/113799775