numpy.stack()、np.row

numpy.stack()、np.row,第1张

numpy.stack()、np.row numpy.stack()

•numpy.stack(arrays, axis = 0, out = None) 沿新轴连接一系列数组

array01=np.array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])

array02 = np.arange(24,48).reshape(4,6)
array02
# 结果
array([[24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35],
       [36, 37, 38, 39, 40, 41],
       [42, 43, 44, 45, 46, 47]])
# 拼接行

np.stack((array01,array02))

# 结果
array([[[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17],
        [18, 19, 20, 21, 22, 23]],

       [[24, 25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34, 35],
        [36, 37, 38, 39, 40, 41],
        [42, 43, 44, 45, 46, 47]]])
# 拼接列
np.stack((array01,array02),axis=1)

# 结果
array([[[ 0,  1,  2,  3,  4,  5],
        [24, 25, 26, 27, 28, 29]],

       [[ 6,  7,  8,  9, 10, 11],
        [30, 31, 32, 33, 34, 35]],

       [[12, 13, 14, 15, 16, 17],
        [36, 37, 38, 39, 40, 41]],

       [[18, 19, 20, 21, 22, 23],
        [42, 43, 44, 45, 46, 47]]])
总结

np.stack()和np.row_stack()、np.column_stack()是不一样的。

np.row_stack((array01,array02))
# 结果
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35],
       [36, 37, 38, 39, 40, 41],
       [42, 43, 44, 45, 46, 47]])

np.stack()形成是三维数组。
np.row_stack()形成的是二维数组,行拼接在一起

np.column_stack((array01,array02))
# 结果
array([[ 0,  1,  2,  3,  4,  5, 24, 25, 26, 27, 28, 29],
       [ 6,  7,  8,  9, 10, 11, 30, 31, 32, 33, 34, 35],
       [12, 13, 14, 15, 16, 17, 36, 37, 38, 39, 40, 41],
       [18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]])

np.stack()形成是三维数组,两个数组的行交叉在一起。
np.column_stack()形成的是二维数组,列拼接在一起。

再看 np.concatenate()

np.concatenate((array01,array02))

# 结果
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35],
       [36, 37, 38, 39, 40, 41],
       [42, 43, 44, 45, 46, 47]])
			 
			 
np.concatenate((array01,array02),axis=1)
# 结果
array([[ 0,  1,  2,  3,  4,  5, 24, 25, 26, 27, 28, 29],
       [ 6,  7,  8,  9, 10, 11, 30, 31, 32, 33, 34, 35],
       [12, 13, 14, 15, 16, 17, 36, 37, 38, 39, 40, 41],
       [18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]])

再看 np.vstack()和np.hstack()。

np.vstack((array01,array02))
# 结果
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35],
       [36, 37, 38, 39, 40, 41],
       [42, 43, 44, 45, 46, 47]])

np.hstack((array01,array02))
# 结果
array([[ 0,  1,  2,  3,  4,  5, 24, 25, 26, 27, 28, 29],
       [ 6,  7,  8,  9, 10, 11, 30, 31, 32, 33, 34, 35],
       [12, 13, 14, 15, 16, 17, 36, 37, 38, 39, 40, 41],
       [18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]])
			 

由此可见,np.concatenate()和np.row_stack()、np.vstack()的效果一样。
np.concatenate(,axis=1)和np.column_stack()、np.hstack()的效果一样。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5652060.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存