np.sum
을 활용하여 axis
에 대해 알아보겠음.(row,column)
의 형태# In[1]
x=np.arange(15)
y=x.reshape(3,5)
print(y)
# Out[1]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
# In[2]
t=np.sum(y,axis=0)
print(t)
# Out[2]
[15 18 21 24 27]
# In[3]
print(y.shape)
t.shape
# Out[3]
(3, 5)
(5,)
t.shape
은 (5,)가 됨.# In[4]
t2=np.sum(y,axis=1)
print(t2)
# Out[4]
[10 35 60]
# In[5]
z=np.arange(27).reshape(3,3,3)
print(z)
# Out[5]
[[[ 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]]]
# In[6]
np.sum(z,axis=0)
# Out[6]
array([[27, 30, 33],
[36, 39, 42],
[45, 48, 51]])
# In[7]
np.sum(z,axis=1)
# Out[7]
array([[ 9, 12, 15],
[36, 39, 42],
[63, 66, 69]])
# In[8]
np.sum(z,axis=2)
# Out[8]
array([[ 3, 12, 21],
[30, 39, 48],
[57, 66, 75]])
References:
1) https://steadiness-193.tistory.com/46
2) https://steadiness-193.tistory.com/50
3) http://taewan.kim/post/numpy_sum_axis/#case-2-axis-0
4) https://jainkku.tistory.com/27