๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
AI ๊ณต๋ถ€ ํ•ญ์ƒํ•˜์ž/๊ด€๋ จ ์ด๋ก 

[Math] ํ–‰๋ ฌ์„ ์•Œ์•„๋ณด์ž

by ์ž„๋ฆฌ๋‘ฅ์ ˆ 2024. 12. 2.
๋ฐ˜์‘ํ˜•
๋”๋ณด๊ธฐ

๊ธฐ์กด ๋ถ€์บ  ๋•Œ ๋…ธ์…˜์— ๊ฐœ์ธ์ ์œผ๋กœ ์ •๋ฆฌํ•œ ๊ฒƒ์„ ๊ณต๋ถ€ํ•  ๊ฒธ ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.
๊ฐœ์ธ์ ์œผ๋กœ ํ•ด์„ํ•ด์„œ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. (ํ‹€๋ฆด ์ˆ˜ ์žˆ์Œ. ์ •์ •์š”์ฒญ ์š”๋งใ…‹)
** ๊ฐ•์˜์ž๋ฃŒ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค **
** ์ƒ์—…์  ์ด์šฉ์„ ๊ธˆ์ง€ํ•ฉ๋‹ˆ๋‹ค **

Today's Keyword
ํ–‰๋ ฌ ๊ณฑ์…ˆ, ํ–‰๋ ฌ ๋‚ด์ , ํ–‰x์—ดy, 

ํ–‰๋ ฌ

  • ๋ฒกํ„ฐ๋ฅผ ์›์†Œ๋กœ ๊ฐ€์ง€๋Š” 2์ฐจ์› ๋ฐฐ์—ด
  • ํ–‰๋ฒกํ„ฐ(row), ์—ด๋ฒกํ„ฐ(column)
  • ๋ฒกํ„ฐ๊ฐ€ ๊ณต๊ฐ„์—์„œ ํ•œ ์ ์„ ์˜๋ฏธํ•œ๋‹ค๋ฉด, ํ–‰๋ ฌ์€ ์—ฌ๋Ÿฌ ์ ๋“ค์„ ๋‚˜ํƒ€๋ƒ„. 
  • ๊ฐ™์€ ๋ชจ์–‘์ด๋ฉด
    • ๋ง์…ˆ, ๋บ„์…ˆ
    • ์„ฑ๋ถ„๊ณฑ (๊ฐ ์ธ๋ฑ์Šค ์œ„์น˜๋ผ๋ฆฌ ๊ณฑํ•˜๊ธฐ) X * Y = (Xij Yij)
    • ์Šค์นผ๋ผ๊ณฑ aX = aXij
  • vector ๊ณต๊ฐ„์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์—ฐ์‚ฐ์ž operator๋กœ ์ดํ•ด. 
  • ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ๋ฒกํ„ฐ๋ฅผ ๋‹ค๋ฅธ ์ฐจ์› ๋ณด๋‚ด๋ฒ„๋ฆฌ๊ธฐ.
  • ํŒจํ„ด์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ์••์ถ•ํ•  ์ˆ˜๋„ ์žˆ์Œ. 
  • ๋ชจ๋“  ์„ ํ˜•๋ณ€ํ™˜ linear transform์€ ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ „์น˜ํ–‰๋ ฌ X^T

  • ํ–‰๊ณผ ์—ด์˜ ์ธ๋ฑ์Šค๊ฐ€ ๋ฐ”๋€œ
  • ๋Œ€๊ฐ์œผ๋กœ 
  • xn1 <-> x1m

ํ–‰๋ ฌ ๊ณฑ์…ˆ ; Matrix multiplication

  • i๋ฒˆ์งธ ํ–‰ ๋ฒกํ„ฐ์™€ j๋ฒˆ์งธ ์—ด๋ฒกํ„ฐ ์‚ฌ์ด์˜ ๋‚ด์ ์„ ์„ฑ๋ถ„์œผ๋กœ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ ๊ณ„์‚ฐ
  • ํฌ๋กœ์Šค๋กœ ๊ณ„์‚ฐํ•œ๋‹ค 
import numpy as np
X = np.array([[1, -2, 3],
			[7, 5, 0],
            [-2, -1, 2]])
Y = np.array([[0, 1],
			[1, -1],
            [-2, 1]])
            
X @ Y
# n11 = 1*0 + -2*1 + 3*-2 = -8
# n12 = 1*1 + -2*-1 + 3*1 = 6
# n21 = 7*0 + 5*1 + 0*-2 = 5
# n22 = 7*1 + 5*-1 + 0*1 = 2
# n31 = -2*0 + -1*1 + 2*-2 = -5
# n32 = -2*1 + -1*-1 + 2*1 = 1

ํ–‰๋ ฌ ๋‚ด์  np.inner

  • i ๋ฒˆ์งธ ํ–‰๋ฒกํ„ฐ์™€ j ๋ฒˆ์งธ ํ–‰๋ฐฑํ„ฐ ์‚ฌ์ด์˜ ๋‚ด์ ์„ ์„ฑ๋ถ„์œผ๋กœ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ 
  • ์ˆ˜ํ•™์˜ ๋‚ด์ ๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค !!! (์–ด์šฐ ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ)
import numpy as np
X = np.array([[1, -2, 3],
			[7, 5, 0],
            [-2, -1, 2]])
Y = np.array([[0, 1, -1],
			[1, -1, 0]])
            
np.inner(X, Y)

# n11 = 1*0 + -2*1 + 3*-1 = -5
# n12 = 1*1 + -2*-1 + 3*0 = 3
# n21 = 7*0 + 5*1 + 0*-1 = 5
# n22 = 7*1 + 5*-1 + 0*0 = 2
# n31 = -2*0 + -1*1 + 2*-1 = -3
# n32 = -2*1 + -1*-1 + 2*0 = -1

 

์—ญํ–‰๋ ฌ ; inverse matrix A^-1

  • ํ–‰ = ์—ด
  • ํ–‰๋ ฌ์‹ determinant ๊ฐ€ -์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ ๊ณ„์‚ฐ. 
  • AA^-1 = A^-1A = I (ํ•ญ๋“ฑํ–‰๋ ฌ)
  • ์—ญํ–‰๋ ฌ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†๋‹ค๋ฉด
    • ์œ ์‚ฌ์—ญํ–‰๋ ฌ (pseudo-inverse)
    • ๋ฌด์–ด-ํŽœ๋กœ์ฆˆ (Moore-Penrose) 

 

์ด๋ฅผ ์‘์šฉํ•ด์„œ ์„ ํ˜•ํšŒ๊ท€๋ถ„์„์„ ํ•ด๋ณด์ž (linear model)

Expression of point to linear
Moore-Penrose ์—ญํ–‰๋ ฌ ์ด์šฉํ•˜๋ฉด y ๊ทผ์ ‘ํ•˜๋Š” y hat์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ.

  • X : input array
  • y : output array
  • b : ํšŒ๊ท€ ๊ณ„์ˆ˜ ๋ฒกํ„ฐ

L2 norm์„ ์ตœ์†Œํ™” ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ

# ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape(-1, 1)
y = np.array([-1, 2, 5, 7, 10, 12, 15, 16, 18, 21])

# ์„ ํ˜•ํšŒ๊ท€ ๋ชจ๋ธ ํ•™์Šต
model = LinearRegression()
model.fit(x, y)
y_test = model.predict(x_test)

# Moore-Penrose ์—ญํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•
X_ = np.array([np.append(x, [1]) for x in X])  # intercept ํ•ญ ์ถ”๊ฐ€ (y์ ˆํŽธ)
beta = np.linalg.pinv(X_) @ y
y_test = np.append(x, [1]) @ beta

 

  • y์ ˆํŽธ ํ•ญ(intercept)๋ฅผ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ์œ„์˜ linearregressionํ•˜๋Š” ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์ด ๋‚˜์˜จ๋‹ค. 

ipynb๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์ง€๋จผ ์—ฌ๊ธฐ์„œ ์ˆ˜์ •ํ•˜์—ฌ ์‹œ๊ฐํ™” ํ•ด๋ด์„œ ๋น„๊ตํ•˜๋Š” ๊ฒƒ๋„ ์ข‹๊ฒ ๋‹ค. !

 

๋ฐ˜์‘ํ˜•

์ตœ๊ทผ๋Œ“๊ธ€

์ตœ๊ทผ๊ธ€

skin by ยฉ 2024 ttutta