์๋
ํ์ธ์? Justkode ์
๋๋ค. ๋ง์ Machine Learning๊ณผ Deep Learning์ ๊ทผ๊ฐ์ ํต๊ณํ์ ๊ธฐ๋ฐ์ผ๋ก ํฉ๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก, ์ฐ๋ฆฌ๋ ์ํ์ ๋ฌด์ํ๊ณ ๋ ๋จธ์ ๋ฌ๋๊ณผ ๋ฅ๋ฌ๋์ ์ดํด ํ ์๋ ์์ต๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์, ์ผ๋จ ๊ณ ๋ฑํ๊ต ๋ ๋ฐฐ์ ๋ ํ๋ฅ ๊ณผ ํต๊ณ์ ๋ํ๊ต์์ ๋ฐฐ์ฐ๋ ์ ํ๋์ ๋ฐ ํ๋ฅ ๊ณผ ๋๋ค ๋ณ์ ์ด๋ก ์ ๋ณต์ตํ๋ ์๊ฐ์ ๊ฐ์ ธ ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. ๋ํ, ๋ณต์ต์๋ง ๋จธ๋ฌด๋ ๊ฒ์ด ์๋, ์ด๋ฅผ Numpy
๋ก ์ฐ์ฐ์ ๊ตฌํํ๋ ๊ณผ์ ๋ํ ์งํ ํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
Gaussian Distribution์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ๋ถํฌ ๊ฐ๋ ์ผ๋ก, ์คํ์ ์ธก์ ์ค์ฐจ๋ ์ฌํ ํ์ ๋ฑ ์์ฐ๊ณ์ ํ์์ ๋๋ถ๋ถ Gaussian Distribution์ ๋ฐ๋ฅด๋ ๊ฒฝํฅ์ด ์์ต๋๋ค.
Gaussian Distribution์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import numpy as np
import matplotlib.pyplot as plt
arr = np.array([1, 2, 3, 4, 5]) # np.array๋ก List ๊ฐ์ฒด ๋ณํ
mean = arr.mean() # ํ๊ท ๊ตฌํ๊ธฐ
std = arr.std() # ํ์คํธ์ฐจ ๊ตฌํ๊ธฐ
vfunc = np.vectorize(lambda x: (x - mean) / std) # vectorize method๋ฅผ ํตํด array๋ฅผ ๋ณํ ์์ผ์ฃผ๋ ํจ์
new_arr = vfunc(arr) # ์ ๊ทํ ๋ array, ๊ฒฐ๊ณผ ๊ฐ์ผ๋ก๋ array([-1.41421356, -0.70710678, 0, 0.70710678, 1.41421356])
mean = 0
std = 1
variance = np.square(std)
x = np.arange(-3,3,.01) # x์ถ ๋ฐ์ดํฐ: -3 ~ 3 ์ฌ์ด 0.01 ๊ฐ๊ฒฉ [-3, -2.99 ... 2.99, 3]
f = np.exp(-np.square(x - mean)/ 2 * variance)/(np.sqrt(2 * np.pi * variance)) # y์ถ ๋ฐ์ดํฐ
plt.plot(x,f) # x ์ขํ, y ์ขํ์ ํด๋นํ๋ ๋ฐ์ดํฐ๋ฅผ ํตํด ๊ทธ๋ํ ํํ
plt.show()
๊ฐ์ฐ์์ ๊ทธ๋ํ.
Multivariate Gaussian Distribution์ ์ ๊ท๋ถํฌ๋ฅผ ๋ค์ฐจ์ ๊ณต๊ฐ์ ๋ํด ํ์ฅํ ๋ถํฌ์ด๋ฉฐ, ์์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๋ค๋ณ๋ ๊ฐ์ฐ์์ ์
๋ค๋ณ๋ ๊ฐ์ฐ์์ ๊ทธ๋ํ
์ ํ ๋์์์ ์ฃผ๋ก ์ฌ์ฉํ๋ ๊ฐ๋ ์ธ Eigenvalue, Eigenvector๋ ๋ค์๊ณผ ๊ฐ์ ์์ด ์ฑ๋ฆฝ ํฉ๋๋ค.
์์์ ํ๋ ฌ ์ ๋ํ์ฌ, ์๋ฒกํฐ๊ฐ ์๋ ๋ฒกํฐ ๊ฐ ์กด์ฌ ํ๋ค๋ฉด, ์ซ์ ๋ ํ๋ ฌ ์ ๊ณ ์ณ๊ฐ์ด๋ผ๊ณ ํ ์ ์์ต๋๋ค.
์ด ๋, ๋ ๊ณ ์ณ๊ฐ ์ ๋์ํ๋ ๊ณ ์ ๋ฒกํฐ์ ๋๋ค.
์ด๋ ํ๋ ฌ์ ๋ฒ์น์ ์ํด ๋ค์๊ณผ ๊ฐ์ด ๋ํ ๋ผ ์ ์์ต๋๋ค.
Eigenvalue, Eigenvector์ ๊ด๊ณ
์ฌ๊ธฐ์ Eigenvalue๋ "์ผ๋ง ๋งํผ ํฌ๊ธฐ๊ฐ ๋ณํ๋"๋ฅผ ์๋ฏธํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ Eigenvector๋ ํ๋ ฌ์ ์ฌ์ํ์ ๋ "์๋ ๋ฒกํฐ์ ํํํ ๋ฒกํฐ๋ ๋ฌด์์ธ์ง"๋ฅผ ์๋ฏธ ํฉ๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์, ๊ฐ ํน์ง ๊ฐ์ ๊ณต๋ถ์ฐ (feature์ feature ์ฌ์ด์ ์ฐ๊ด์ฑ)์ ๋ํ๋ด๋ Covariance Matrix์ ๋ํด ๋์ Eigenvalue๋ฅผ ๊ฐ์ง Eigenvector์ ์ฃผ์ฑ๋ถ ๋ถ์(PCA) ๋ฅผ ํตํด ํ์ต ์ ์ฐจ์์ ์ค์ด๋ ๋ฐ ์ฌ์ฉ ๋ฉ๋๋ค.
์ฌ๊ธฐ์ ๋งํ๋ Covariance Matrix์ ๋ํด ์ถ๊ฐ ์ค๋ช ์ ๋ง๋ถ์ด์๋ฉด, ๋๊ฐ ์ฑ๋ถ์ feature์ ๋ถ์ฐ์ ๋ํ๋ด๊ณ , ๋๋จธ์ง๋ ๊ฐ ํ๊ณผ ์ด์ ๋์ํ๋ Covariance (๊ฐ feature์ ์ ํ ์ข ์์ฑ) ๋ฅผ ๋ํ ๋ ๋๋ค.
Covariance Matrix
array = [[1, 2], [3, 4]]
eigenvalues, eigenvectors = np.linalg.eig(array) # Eigenvalue, Eigenvector of Covarience Matrix
print("eigenvalue: ", eigenvalues)
print("eigenvector: ", eigenvectors)
print("eigenvalue * eigenvector: ", eigenvalues * eigenvectors)
print("array * eigenvector: ", array @ eigenvectors)
eigenvalue: [-0.37228132 5.37228132]
eigenvector: [[-0.82456484 -0.41597356]
[ 0.56576746 -0.90937671]]
eigenvalue * eigenvector: [[ 0.30697009 -2.23472698]
[-0.21062466 -4.88542751]]
array * eigenvector: [[ 0.30697009 -2.23472698]
[-0.21062466 -4.88542751]]
array = np.random.multivariate_normal([0, 0], [[1, 0], [0, 100]], 1000) # multivariate (mean, covarience matrix, samples)
cov = np.cov(array.T) # Covarience Matrix ((index x, index y) x์ ๋ํด, y๊ฐ ์ผ๋ง๋ ๋ณํ ํ๋๊ฐ?)
print(cov)
eigenvalues, eigenvectors = np.linalg.eig(cov) # Eigenvalue, Eigenvector of Covarience Matrix
print("eigenvalue: ", eigenvalues)
print("eigenvector: ", eigenvectors)
plt.scatter(array[:, 0], array[:, 1])
plt.quiver([0, 0], [0, 0], eigenvectors[:, 0], eigenvectors[:, 1], color=['r', 'b'], scale=3)
plt.xlim(-50, 50)
plt.ylim(-50, 50)
plt.show()
[[ 1.02530381 0.23267569]
[ 0.23267569 97.82469968]]
eigenvalue: [ 1.02474454 97.82525895]
eigenvector: [[-0.99999711 -0.00240367]
[ 0.00240367 -0.99999711]]
์ฃผ์ฑ๋ถ์ ํ๋์ ๋ฒกํฐ์ ๊ฐ๊น์ด๊ฑธ, ์์น์ ์ผ๋ก, ์๊ฐ์ ์ผ๋ก ์ ์ ์๋ค.
Machine Learning์์ ๋ฏธ๋ถ์ ์ด๋ค ๊ฒ์ ์๋ฏธ ํ ๊น์? ์ด๋ Cost Function์ ๋ํ Gradient descent (๊ฒฝ์ฌ ํ๊ฐ๋ฒ)์ ์ฌ์ฉ ํ ๋ ์ฌ์ฉํฉ๋๋ค. ์ด๋ ์ปดํจํฐ์์ ๋ฏธ๋ถ์ ์์์ ์ฌ์ฉํ๋ ํด์์ ๋ฐฉ๋ฒ์ด ์๋, ์ง์ ๊ฐ์ ๊ณ์ฐํ์ฌ ์งํํ๋ ์์น์ ๋ฏธ๋ถ์ ์งํ ํฉ๋๋ค. ์ด๋ฅผ ๊ตฌํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
def diff(f, index, *args): # index ๋ฒ์งธ์ parameter์ ๋ํด ํธ๋ฏธ๋ถ
delta = 1e-4 # h
new_args = list(args) # index ๋ฒ์งธ์ parameter์ ๋ํด delta๋ฅผ ๋ํด ์ค
new_args[index] += delta
return (f(*new_args) - f(*args)) / delta # f(x + h, y) - f(x, y) / h
diff(lambda x, y: x ** 2 + y ** 2, 0, 3, 3) # f(x, y) = x^2 + y^2
6.000099999994291
์ด๋ฅผ ๊ทธ๋ํ๋ก ํ์ธ ํด ๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
# ์๋ ๊ทธ๋ํ
x = np.arange(-3,3,.01)
y = x ** 2
# tangent, ์ ์
d = diff(lambda x: x ** 2, 0, 1)
x_tangent = np.arange(0, 2, .01)
y_tangent = (x_tangent - 1) * d + 1
# ๊ทธ๋ํ ํํ
plt.plot(x, y)
plt.plot(x_tangent, y_tangent, color='r')
plt.show()
๊ทธ๋ํ๋ก ํํ ๋ ๋ชจ์ต.
Cost Function์ ๋ํ Gradient descent (๊ฒฝ์ฌ ํ๊ฐ๋ฒ)์ ์ฌ์ฉ ํ ๋, ๋งค๊ฐ๋ณ์์ ์๋์ ์ธ ๋ฏธ๋ถ ๊ฐ์ ์ ๋ฌํ๊ธฐ ์ํ๊ธฐ ์ํด์ ์ด๋ค ๊ฒ์ด ํ์ ํ ๊น์? ์ด๋ด ๋๋ ๊ณ์ฐ ๊ทธ๋ํ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
ํน์ ๋งค๊ฐ๋ณ์๊ฐ ๋ณํ ํ์ ๋, ๊ฒฐ๊ณผ ๊ฐ์ด ์ผ๋ง๋งํผ ์์ง์ด๋ ๊ฐ?
๊ณฑ์ ๋ ธ๋์ ๊ตฌํ, ๋ง์ ๋ ธ๋์ ๊ตฌํ ๋ฐ ์์ ํ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
class MulLayer: # ๊ณฑ์
๋
ธ๋
def forward(self, x, y):
self.x = x
self.y = y
out = x * y
return out
def backward(self, dout):
dx = dout * self.y
dy = dout * self.x
return dx, dy
class AddLayer: # ๋ง์
๋
ธ๋
def forward(self, x, y):
out = x + y
return out
def backward(self, dout):
dx = dout * 1
dy = dout * 1
return dx, dy
chicken = 17000
num = 3
delivary_fee = 3000
mul_chicken = MulLayer()
add_fee = AddLayer()
chicken_price = mul_chicken.forward(chicken, num)
total_price = add_fee.forward(chicken_price, delivary_fee)
print(total_price)
54000
์ญ์ ํ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
d_price = 1
d_chicken_price, d_delivary_fee = add_fee.backward(d_price)
d_chicken, d_num = mul_chicken.backward(d_chicken_price)
print("d_chicken_price: ", d_chicken_price)
print("d_delivary_fee: ", d_delivary_fee)
print("d_chicken: ", d_chicken)
print("d_num: ", d_num)
d_chicken_price: 1
d_delivary_fee: 1
d_chicken: 3
d_num: 17000
์ค๋์ ์ด๋ ๊ฒ ๋ฅ๋ฌ๋์์ ์ฌ์ฉ ๋๋ ์ํ ๊ณต์๋ค์ numpy๋ฅผ ์ด์ฉ ํ์ฌ ๊ตฌํด ๋ณด์์ต๋๋ค. ๋ค์ ์๊ฐ์๋ Pandas์ ์ฌ์ฉ ๋ฒ์ ๋ํด ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.