![](https://img-blog.csdnimg.cn/img_convert/baeeb8c13b6c096e1ca37a8cba460fcf.jpeg)
一、数组的拼接
1. 横向拼接
hstack()
——水平拼接,沿着行的方向,对列进行拼接。
concatenate()
也可以完成相应的功能,axis=1
时,沿水平方向叠加。
1 2 3 4 5 6 7 8 9 10 | import numpy as np a = np.array([ 1 , 2 , 3 , 4 ]).reshape(( 2 , 2 ), order = 'F' ) b = np.array([ 0 , 0 , 1 , 1 ]).reshape(( 2 , 2 )) a, b harr = np.hstack((a, b)) harr harr = np.concatenate((a, b), axis = 1 ) harr |
2. 纵向拼接
vstack()
——垂直拼接,沿着列的方向,对行进行拼接。
concatenate()
也可以完成相应的功能,axis=0
时,沿垂直方向叠加
1 2 3 4 5 6 7 8 9 10 11 | import numpy as np a = np.array([ 1 , 1 , 0 , 0 ]).reshape(( 2 , 2 )) b = np.array([ 0 , 1 , 0 , 1 ]).reshape(( 2 , 2 )) a, b varr = np.vstack((a, b)) varr varr = np.concatenate((a, b), axis = 0 ) varr |
二、数组的分割
1. 横向分割
hsplit()
——水平拆分,沿着行的方向,对列进行拼接。
split()
也可以完成相应的功能,axis=1
时,沿水平方向拆分。
1 2 3 4 5 6 7 8 9 | import numpy as np a = np.array([ 1 , 1 , 0 , 0 ]).reshape(( 2 , 2 )) b = np.array([ 0 , 1 , 0 , 1 ]).reshape(( 2 , 2 )) harr = np.hstack((a, b)) np.hsplit(harr, 2 ) np.split(harr, 2 , axis = 1 ) |
2. 纵向分割
hvplit()
——垂直拆分,沿着列的方向,对行进行拼接。
split()
也可以完成相应的功能,axis=0
时,沿垂直方向拆分。
1 2 3 4 5 6 7 8 9 | import numpy as np a = np.array([ 1 , 1 , 0 , 0 ]).reshape(( 2 , 2 )) b = np.array([ 0 , 1 , 0 , 1 ]).reshape(( 2 , 2 )) varr = np.vstack((a, b)) np.vsplit(varr, 2 ) np.split(varr, 2 , axis = 0 ) |
到此这篇关于Python 数组的变形浅谈的文章就介绍到这了,希望可以对你有所帮助。
转自:微点阅读 https://www.weidianyuedu.com