== 数组的转置与轴对换 ==
np1 = np.arange(0,40).reshape(10,4)
print(np1)
[[ 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]]
转置 两种方法等价 transpose T
np1.transpose()
np1.T
array([[ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36], [ 1, 5, 9, 13, 17, 21, 25, 29, 33, 37], [ 2, 6, 10, 14, 18, 22, 26, 30, 34, 38], [ 3, 7, 11, 15, 19, 23, 27, 31, 35, 39]])
=== 合并 stack ===
scores1 = np.array([[70,80,90],
[77,88,99],
[11,22,33]])
scores2 = np.array([[90,90,90],
[100,100,100],
[80,80,80]])
np.stack([scores1,scores2])
stack将两个二维数组,合并成了一个三维的数组
array([[[ 70, 80, 90], [ 77, 88, 99], [ 11, 22, 33]],
[[ 90, 90, 90], [100, 100, 100], [ 80, 80, 80]]])
注意:
np.stack([scores1,scores2] , axis=?)
axis=?
如果?=0 那么把两个数组直接合并到一起
如果?=2 那么把每一行的列放到一起合并
= 合并 vstack 垂直堆叠 =
scores1 = np.array([[70,80,90],
[77,88,99],
[11,22,33]])
scores2 = np.array([[90,90,90],
[100,100,100],
[80,80,80]])
np.vstack([scores1,scores2])
array([
[ 70, 80, 90],
[ 77, 88, 99],
[ 11, 22, 33],
[ 90, 90, 90],
[100, 100, 100],
[ 80, 80, 80]
])
= 合并 hstack 水平堆叠 =
scores1 = np.array([[70,80,90],
[77,88,99],
[11,22,33]])
scores2 = np.array([[90,90,90],
[100,100,100],
[80,80,80]])
np.hstack([scores1,scores2])
array([
[ 70, 80, 90, 90, 90, 90],
[ 77, 88, 99, 100, 100, 100],
[ 11, 22, 33, 80, 80, 80]
])
=== 拉伸 tile ===
scores = np.array([[90,90,90],
[100,100,100],
[80,80,80]])
=== 横向拉升 ===
np.tile(scores,2)
[[ 90, 90, 90, 90, 90, 90],
[100, 100, 100, 100, 100, 100],
[ 80, 80, 80, 80, 80, 80]]`
=== 纵横同时拉升 ===
np.tile(scores,[2,2])
[[ 90, 90, 90, 90, 90, 90],
[100, 100, 100, 100, 100, 100],
[ 80, 80, 80, 80, 80, 80],
[ 90, 90, 90, 90, 90, 90],
[100, 100, 100, 100, 100, 100],
[ 80, 80, 80, 80, 80, 80]])
=== 纵向拉升 ===
np.tile(scores,[2,1])
[[ 90, 90, 90],
[100, 100, 100],
[ 80, 80, 80],
[ 90, 90, 90],
[100, 100, 100],
[ 80, 80, 80]]