1、np.stack连接
1、代码
# 沿着新轴连接数组的序列
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
# [[1 2 3]
# [2 3 4]]
print(np.stack((a, b), axis=0))
# [[1 2]
# [2 3]
# [3 4]]
print(np.stack((a, b), axis=1))
2、axis=0/1/2之间的不同
1、代码
a = [[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]],
[[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]]]
# 块与块之间进行运算
# a.shape = 2x3x4
sum_0 = np.sum(a, axis=0)
# 3*4
# [[14 16 18 20]
# [22 24 26 28]
# [30 32 34 36]]
print(sum_0)
sum_1 = np.sum(a, axis=1)
# # 2*4
# [[15 18 21 24]
# [51 54 57 60]]
print(sum_1)
sum_2 = np.sum(a, axis=2)
# 2*3
# [[10 26 42]
# [58 74 90]]
print(sum_2)