๋”ฅ๋Ÿฌ๋‹/Today I learned :

[๋”ฅ๋Ÿฌ๋‹] ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ (Multiple linear regression)

์ฃผ์˜ ๐Ÿฑ 2021. 3. 16. 10:23
728x90
๋ฐ˜์‘ํ˜•

์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜x

๊ณต๋ถ€ํ•œ์‹œ๊ฐ„:x

๊ณผ์™ธ ์ˆ˜์—… ํšŸ์ˆ˜: xโ‚‚

์„ฑ์ :y

 

y  =  aโ‚xโ‚ + aโ‚‚xโ‚‚ + b

 

๊ธฐ์šธ๊ธฐ aโ‚,aโ‚‚๋Š” ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•์„ ํ†ตํ•ด ๊ตฌํ•จ

 


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import mpl_tookits import mplot3d    #3d๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

#x,y๋ฐ์ดํ„ฐ๊ฐ’
data=[[2,0,81],[4,4,93],[6,2,91],[8,3,97]]
x1= [i[0] for i in data]
x2= [i[1] for i in data]
y = [i[2] for i in data]

ax = plt.axes(projection='3d')
ax.set_xlabel('study_H')
ax.set_ylabel('private_class')
ax.set_zlabel('score')
ax.scatter(x1,x2,y)
plt.show()

#๋ฆฌ์ŠคํŠธ๋ฅผ numpy๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ๊ธฐ(์ธ๋ฑ์Šค๋ฅผ ์ฃผ์–ด ํ•˜๋‚˜์”ฉ ๋ถˆ๋Ÿฌ์™€ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด)
xdata=np.array(x1)
x2data = np.array(x1)
ydata=np.array(y)

#์ดˆ๊ธฐํ™”
a=0
a2=0
b=0

#ํ•™์Šต๋ฅ  ์ •ํ•˜๊ธฐ
lr=0.05

#๋ช‡๋ฒˆ ๋ฐ˜๋ณต๋ ์ง€(0๋ถ€ํ„ฐ ์„ธ๋ฏ€๋กœ +1ํ•ด์ฃผ๊ธฐ)
epochs =2001

#๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•
for i in range(epochs):
	y_pred = a* xdata+ a2 *x2data + b
    error =ydata-y_pred   #์˜ค์ฐจ
    a_diff = -(1/len(xdata))* sum(xdata *(error)) #์˜ค์ฐจํ•จ์ˆ˜ a๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’
    a2diff = -(1/len(x2data))* sum(x2data *(error)) #์˜ค์ฐจํ•จ์ˆ˜ a2๋กœ๋ฏธ๋ถ„ํ•œ ๊ฐ’
    b_diff = -(1/len(xdata))* sum(ydata - y_pred) #์˜ค์ฐจํ•จ์ˆ˜ b๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’
    
    a = a-lr *a_diff #ํ•™์Šต๋ฅ  ๊ณฑํ•ด ๊ธฐ์กด๊ฐ’ ์—…๋ฐ์ดํŠธ
    a2= a2-lr*a2diff
    b = b-lr *b_diff    
    
    if i%100 ==0:
    	print("epoch=%.f, ๊ธฐ์šธ๊ธฐ=%.04f, ๊ธฐ์šธ๊ธฐ2=%.04f,์ ˆํŽธ=%.04f" % (i,a, a2,b)) #100๋ฒˆ ๋ฐ˜๋ณต๋ ๋•Œ๋งˆ๋‹ค ํ˜„์žฌ a,b์ถœ๋ ฅ

 

 

 

 

 

์ฐจ์›์ด ํ•˜๋‚˜ ๋” ๋Š˜์–ด๋‚œ ๋ชจ์Šต

์กฐ๊ธˆ ๋” ์ •๋ฐ€ํ•œ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•ด์ง

๋ฐ˜์‘ํ˜•