dd=np.vstack((d,ccc))
一、连接数组
import numpy as np
a = np.arange(3)
b = np.arange(10,13)
print(a)
print(b)
1
2
3
4
5
[0 1 2]
[10 11 12]
1
2
1.最基本的函数:concatenate
np.concatenate((a,b)) # 默认axis=0
1
array([ 0, 1, 2, 10, 11, 12])
1
2.vstack:垂直连接数组(axis=0)
np.vstack((a,b))
1
array([[ 0, 1, 2],
[10, 11, 12]])
1
2
3.hstack:水平连接数组(axis=1)
np.hstack((a,b))
1
array([ 0, 1, 2, 10, 11, 12])
1
4.c_[]对象:将向量按列合并
np.c_[a,b,a+b]
1
array([[ 0, 10, 10],
[ 1, 11, 12],
[ 2, 12, 14]])
1
2
3
二、拆分数组
1.split:严格平均划分
np.random.seed(42)
a = np.random.randint(0,10,12)
np.split(a,6) # 将数组a划分成相等的6份
1
2
3
[array([6, 3]),
array([7, 4]),
array([6, 9]),
array([2, 6]),
array([7, 4]),
array([3, 7])]
1
2
3
4
5
6
2.array_split:尽量平均划分
np.array_split(a,5)
1
[array([6, 3, 7]),
array([4, 6, 9]),
array([2, 6]),
array([7, 4]),
array([3, 7])]
1
2
3
4
5
三、调整轴顺序
1.transpose:重新指定轴顺序
a = np.random.randint(0,10,(2,3,4,5))
print("原数组形状:",a.shape)
print("transpose:",np.transpose(a,(1,2,0,3)).shape)#重新指定轴0到3的顺序
1
2
3
原数组形状: (2, 3, 4, 5)
transpose: (3, 4, 2, 5)
1
2
2.swapaxes:交换两个轴
print("swapaxes:",np.swapaxes(a,1,2).shape)#交换轴1和2
1
swapaxes: (2, 4, 3, 5)
原文链接:https://blog.csdn.net/fang156239305/article/details/106919020