import numpy as np
a = np.arange(30).reshape(3,5,2)
print(a)
# [[[ 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]]]
b = np.sum(a, axis=0)
print(b, b.shape)
# [[30 33]
# [36 39]
# [42 45]
# [48 51]
# [54 57]] (5, 2)
c = np.sum(a, axis=1)
print(c, c.shape)
# [[ 20 25]
# [ 70 75]
# [120 125]] (3, 2)
d = np.sum(a, axis=2)
print(d, d.shape)
# [[ 1 5 9 13 17]
# [21 25 29 33 37]
# [41 45 49 53 57]] (3, 5)
.
.
print(a, a.shape)
# [[[ 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]]] (3, 5, 2)
a.shape = (3, 5, 2) 에서 ( , 5, )를 2번째 자리로 옮기면(a2),
a2.shape = (3, 2, 5)
a2 = np.moveaxis(a,2,0)
print(a2, a2.shape)
# [[[ 0 2 4 6 8]
# [ 1 3 5 7 9]]
# [[10 12 14 16 18]
# [11 13 15 17 19]]
# [[20 22 24 26 28]
# [21 23 25 27 29]]] (3, 2, 5)
.
.
차원을 추가하고 싶다면, np.newaxis 또는 np.expand_dims 를 사용하면 된다.
a10 = np.arange(10)
print(a10, a10.shape)
# [0 1 2 3 4 5 6 7 8 9] (10,) -> 1차원
a11 = a10[np.new_axis, : ]
print(a11, a11.shape)
# [[0 1 2 3 4 5 6 7 8 9]] (1, 10) -> 2차원
a12 = a10[ :, np.newaxis]
# [[0]
# [1]
# [2]
# [3]
# [4]
# [5]
# [6]
# [7]
# [8]
# [9]] (10, 1) -> 2차원
a13 = np.expand_dims(a10, 0)
print(a13, a13.shape)
# [[0 1 2 3 4 5 6 7 8 9]] (1, 10) -> 2차원