axis 이해하기
import numpy as np
x = np.arange(15)
print(x)
>>> [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
1차원 데이터에 적용하기
np.sum(x, axis=0)
>>> 105
2차원 행렬에 적용하기
y = x.reshape(3, 5)
print(y)
>>> [[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
np.sum(y, axis = 0)
>>> array([15, 18, 21, 24, 27])
y = x.reshape(3, 5)
print(y)
>>> [[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
np.sum(y, axis=1)
>>> array([10, 35, 60])
3차원 텐서에 적용하기
z = np.arange(36).reshape(3, 4, 3)
print(z)
>>> [[[ 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]]]
np.sum(z, axis=0)
>>> array([[36, 39, 42],
[45, 48, 51],
[54, 57, 60],
[63, 66, 69]])
np.sum(z, axis=1)
>>> array([[ 18, 22, 26],
[ 66, 70, 74],
[114, 118, 122]])
np.sum(z, axis=2)
>>> array([[ 3, 12, 21, 30],
[ 39, 48, 57, 66],
[ 75, 84, 93, 102]])
print(z)
>>> [[[ 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]]]
np.sum(z, axis=(0, 1))
>>> array([198, 210, 222])
브로드캐스팅
브로드캐스팅 Rule
shape이 같은 경우의 연산
import numpy as np
x = np.arange(15).reshape(3, 5)
y = np.random.rand(15).reshape(3, 5)
print(x)
>>> [[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
print(y)
>>> [[0.80814776 0.73781763 0.82966379 0.49129413 0.70161994]
[0.83106599 0.68243819 0.41285899 0.78419392 0.68406395]
[0.40871086 0.72481337 0.25397246 0.56917074 0.76998926]]
x + y
>>> array([[ 0.80814776, 1.73781763, 2.82966379, 3.49129413, 4.70161994],
[ 5.83106599, 6.68243819, 7.41285899, 8.78419392, 9.68406395],
[10.40871086, 11.72481337, 12.25397246, 13.56917074, 14.76998926]])
scalar(상수)와의 연산
x * 10
>>> array([[ 0, 10, 20, 30, 40],
[ 50, 60, 70, 80, 90],
[100, 110, 120, 130, 140]])
x % 2 == 0
>>> array([[ True, False, True, False, True],
[False, True, False, True, False],
[ True, False, True, False, True]])
shape이 다른 경우 연산
import numpy as np
a = np.arange(12).reshape(4, 3)
b = np.arange(100, 103)
c = np.arange(1000, 1004)
d = b.reshape(1, 3)
print(a.shape)
>>> (4, 3)
print(b.shape)
>>> (3,)
print(c.shape)
>>> (4,)
print(d.shape)
>>> (1, 3)
print(d)
>>> [[100 101 102]]
a + b
>>> array([[100, 102, 104],
[103, 105, 107],
[106, 108, 110],
[109, 111, 113]])
a + d
>>> array([[100, 102, 104],
[103, 105, 107],
[106, 108, 110],
[109, 111, 113]])
Boolean indexing
import numpy as np
x = np.random.randint(1, 100, size=10)
print(x)
>>> [58 75 24 71 2 9 2 54 44 67]
even_mask = x % 2 == 0
print(even_mask)
>>> [ True False True False True False True True True False]
x[even_mask]
>>> array([58, 24, 2, 2, 54, 44])
x[x % 2 == 0]
>>> array([58, 24, 2, 2, 54, 44])
x[x > 30]
>>> array([58, 75, 71, 54, 44, 67])
다중조건 사용하기
print(x % 2 == 0)
>>> [ True False True False True False True True True False]
print(x < 30)
>>> [False False True False True True True False False False]
print(x[(x % 2 == 0) & (x < 30)])
>>> [58 75 24 71 2 9 2 54 67]
머신러닝과 데이터 분석 A-Z 올인원 패키지 Online. 👉 https://bit.ly/3cB3C8y