numpy vstack hstack dstack 区别

先假设有如下数据(用于示范三种函数给这些数据带来的操作效果)

import numpy as np

data1 = [
[[11, 12, 13], [14, 15, 16], [17, 18, 19]],
[[21, 22, 23], [24, 25, 26], [27, 28, 29]],
[[31, 32, 33], [34, 35, 36], [37, 38, 39]]
]

data2 = [
[[41, 42, 43], [44, 45, 46], [47, 48, 49]],
[[51, 52, 53], [54, 55, 56], [57, 58, 59]],
[[61, 62, 63], [64, 65, 66], [67, 68, 69]]
]

arr1 = np.array(data1)
arr2 = np.array(data2)
# arr1.shape == [3, 3, 3]
# arr2.shape == [3, 3, 3]

vstack 解释

vstack 表示将数组在第一维进行堆叠(即最外层的方括号),可将 arr1 和 arr2 第一维内的部分视为一个整体,即:

arr1 == [块1], 
arr2 == [块2]
vstack((arr1, arr2)) == [块1 + 块2]
a = np.vstack((arr1, arr2))
print a
# 结果如下
[
[[11, 12, 13], [14, 15, 16], [17, 18, 19]],
[[21, 22, 23], [24, 25, 26], [27, 28, 29]],
[[31, 32, 33], [34, 35, 36], [37, 38, 39]],
[[41, 42, 43], [44, 45, 46], [47, 48, 49]],
[[51, 52, 53], [54, 55, 56], [57, 58, 59]],
[[61, 62, 63], [64, 65, 66], [67, 68, 69]]
]
# 此时 a.shape == [6, 3, 3]

hstack 解释

hstack 表示将数组在第二维进行堆叠(即第二层方括号),可将 arr 第二层括号里面的东西视为一个整体,即

arr1 == [
[块1],
[块2],
[块3]
]

arr2 == [
[块4],
[块5],
[块6]
]

# 然后 hstack((arr1, arr2)) 的结果如下:
[
[块1 + 块4],
[块2 + 块5],
[块3 + 块6]
]
b = np.hstack((arr1, arr2))
print b
# 结果如下:
[
[[11, 12, 13], [14, 15, 16], [17, 18, 19], [41, 42, 43], [44, 45, 46], [47, 48, 49]],
[[21, 22, 23], [24, 25, 26], [27, 28, 29], [51, 52, 53], [54, 55, 56], [57, 58, 59]],
[[31, 32, 33], [34, 35, 36], [37, 38, 39], [61, 62, 63], [64, 65, 66], [67, 68, 69]]
]
# 此时 b.shape == [3, 6, 3]

dstack 解释

dstack 表示将数组在第三维进行堆叠(即第三层方括号),可将 arr 第三层括号里面的东西视为一个整体,即:

arr1 = [
[[块1], [块2], [块3]],
[[块4], [块5], [块6]],
[[块7], [块8], [块9]]
]

arr2 = [
[[块11], [块12], [块13]],
[[块14], [块15], [块16]],
[[块17], [块18], [块19]]
]

# 然后 dstack((arr1, arr2)) 的结果如下:
[
[[块1 + 块11],[块2 + 块12],[块3 + 块13]]
[[块4 + 块14],[块5 + 块15],[块6 + 块16]]
[[块7 + 块17],[块8 + 块18],[块9 + 块19]]
]

print c
# 结果如下:
[
[[11 12 13 41 42 43],[14 15 16 44 45 46],[17 18 19 47 48 49]]
[[21 22 23 51 52 53],[24 25 26 54 55 56],[27 28 29 57 58 59]]
[[31 32 33 61 62 63],[34 35 36 64 65 66],[37 38 39 67 68 69]]
]

# 此时 c.shape == [3, 3, 6]

你可能感兴趣的:(numpy vstack hstack dstack 区别)