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

[๋”ฅ๋Ÿฌ๋‹] ์˜ค์ฐจ ํ‰๊ฐ€๋ฒ• : ํ‰๊ท ์ œ๊ณฑ์˜ค์ฐจ Mean Squared Error

์ฃผ์˜ ๐Ÿฑ 2021. 3. 15. 17:51
728x90
๋ฐ˜์‘ํ˜•

 

์˜ค์ฐจ = ์˜ˆ์ธก ๊ฐ’- ์‹ค์ œ ๊ฐ’

์˜ค์ฐจ๋“ค์˜ ํ•ฉ์€ ๋ถ€ํ˜ธ๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์–ด ๊ฐ ์˜ค์ฐจ์˜ ๊ฐ’์„ ์ œ๊ณฑํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

n = x์›์†Œ์˜ ์ด ๊ฐœ์ˆ˜, i = x๊ฐ€ ๋‚˜์˜ค๋Š” ์ˆœ์„œ , (์‹ค์ œ๊ฐ’-์˜ˆ์ธก๊ฐ’)

 

ํ‰๊ท ์ œ๊ณฑ์˜ค์ฐจ(MSE) = ์˜ค์ฐจ๋ฅผ ์ œ๊ณฑํ•˜์—ฌ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•œ ๊ฒƒ .  ๊ฐ’์ด ์ž‘์„์ˆ˜๋ก ์˜ˆ์ธก๋ ฅ์ด ์ข‹๋‹ค

 

 

MSE

์˜ค์ฐจ์˜ ํ•ฉ์„ n์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค.

 

 

์„ ํ˜• ํšŒ๊ท€ : ์ž„์˜์˜ ์ง์„ ์„ ๊ทธ์–ด ์ด์— ๋Œ€ํ•œ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•˜๊ณ , ์ด ๊ฐ’์„ ๊ฐ€์žฅ ์ž‘๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” A์™€ B์˜ ๊ฐ’์„ ์ฐพ์•„๊ฐ€๋Š” ๊ณผ์ •


   ํŒŒ์ด์ฌ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ   

 

import numpy as np


#์ž„์˜๋กœ ์ •ํ•œ ๊ธฐ์šธ๊ธฐa์™€ y์ ˆํŽธ b๊ฐ’์„ fake_ab ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ
fake_ab = [3,76]

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


#y=ax+b๊ตฌํ˜„
def predict(x):
	return fake_ab[0]*x + fake_ab[1]


#mean():ํ‰๊ท ๊ฐ’๊ตฌํ•˜๋Š”ํ•จ์ˆ˜///ํ‰๊ท ์ œ๊ณฑ๊ตฌํ•˜๊ธฐ
def mse(yi,y):
   return ((yi-y)**2),mean())
   
def mse_val(predict_result,y):
	return mse(np,array(predict_result),np.array(y))


#์˜ˆ์ธก๊ฐ’๋ฆฌ์ŠคํŠธ
pedict_result=[]


#๋ชจ๋“  x๊ฐ’ ํ•œ๋ฒˆ์”ฉ ๋Œ€์ž…
for i in range(len(x)):
	predict_result.append(predict(x[i]))
    print("๊ณต๋ถ€์‹œ๊ฐ„=%.f,์‹ค์ œ์ ์ˆ˜=%.f,์˜ˆ์ธก์ ์ˆ˜=%.f"% (x[i],y[i],
    predict(x[i])))
    
print("MSE ์ตœ์ข…๊ฐ’:"+ str(mse_val(predict_result,y))
๋ฐ˜์‘ํ˜•