๊ณผ๊ฑฐ ๋ฐ์ดํฐ ๊ธฐ๋ฐ์ผ๋ก, ์ ๋ ฅ ์ฃผ์ด์ก์ ๋ ์ถ๋ ฅ(์์ธก๊ฐ)์ด ๋์์ผ ํ๋ค.
์์ธกํ๊ณ ์ ํ๋ ๋ณ์ = target variable(ํ๊ฒ ๋ณ์)
ํ๊ฒ ๋ณ์๊ฐ ์ค์์ด๋ฉด = regression problem,
ํ๊ฒ ๋ณ์๊ฐ ์นดํ ๊ณ ๋ฆฌ ๋ณ์์ด๋ฉด = classification (๋ํ์ ์ธ ๋ฐฉ๋ฒ๋ก : ๋ก์ง์คํฑ ํ๊ท)
์ด ๋์ supervised learning(์ง๋ ํ์ต)์ด๋ค.
unsupervised learning(๋น์ง๋ ํ์ต)์๋ clustring(k-means) ๋ฑ์ด ์๋ค.
์ ํํ๊ท Linear Regression
- ์ข ์ ๋ณ์ ๐ฆ์ ํ๊ฐ ์ด์์ ๋ ๋ฆฝ ๋ณ์ ๐์์ ์ ํ ๊ด๊ณ๋ฅผ ๋ชจ๋ธ๋ง(=1์ฐจ๋ก ์ด๋ฃจ์ด์ง ์ง์ ์ ๊ตฌํ๋ค)ํ๋ ๋ฐฉ๋ฒ๋ก
- ์ต์ ์ ์ง์ ์ ์ฐพ์ ๋ ๋ฆฝ ๋ณ์์ ์ข ์ ๋ณ์ ์ฌ์ด์ ๊ด๊ณ๋ฅผ ๋์ถํ๋ ๊ณผ์
๋ ๋ฆฝ ๋ณ์= ์ ๋ ฅ ๊ฐ์ด๋ ์์ธ(input)
์ข ์ ๋ณ์ = ๋ ๋ฆฝ ๋ณ์์ ์ํด ์ํฅ์ ๋ฐ๋ ๋ณ์(output)
<simple linear regression>(๋ ๋ฆฝ๋ณ์x๊ฐ 1๊ฐ์ผ ๋)
๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฅ ์ ์ค๋ช ํ๋ ์ง์ ์ ์์ธกํ ๊ฐ์ด ์ค์ ๋ฐ์ดํฐ์ ๊ฐ๊ณผ ๊ฐ์ฅ ๋น์ทํด์ผ ํฉ๋๋ค.
์ฐ๋ฆฌ์ ๋ชจ๋ธ์ด ์์ธกํ ๊ฐ์ ์์์ ์ ์ ์๋ฏ ๐(๐ฅ๐)์ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ค์ ๋ฐ์ดํฐ๋ ๐ฆ ์ ๋๋ค.
์ค์ ๋ฐ์ดํฐ(์ ๊ทธ๋ฆผ์์ ๋นจ๊ฐ ์ ) ๊ณผ ์ง์ ์ฌ์ด์ ์ฐจ์ด๋ฅผ ์ค์ด๋ ๊ฒ์ด ์ฐ๋ฆฌ์ ๋ชฉ์ ์ ๋๋ค.
๊ทธ๊ฒ์ ๋ฐํ์ผ๋ก cost function์ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํด๋ณด๊ฒ ์ต๋๋ค.
(n: ์ํ ์, i: i๋ฒ์งธ ๋ฐ์ดํฐ)
์ฐ๋ฆฌ๋ cost function์ ์ต์๋ก ํ๋ ๐ค์ ๐๋ฅผ ์ฐพ์์ผ ํฉ๋๋ค.
์ด์ฐจํจ์์ด๋ฏ๋ก ์ด์ฐจํจ์์ ์ต์๊ฐ์ ๊ตฌํ๋ ๋ฐฉ๋ฒ์
1. ๋ฏธ๋ถํ ๊ฐ์ด 0์ด ๋๋ ์ง์ ์ฐพ๊ธฐ
[−3/2โ]
2. gradient descent
gradient descent
ํ๋ฒ์ ์ ๋ต์ ์ ๊ทผํ๋ ๊ฒ์ด ์๋ ๋ฐ๋ณต์ ์ผ๋ก ์ ๋ต์ ๊ฐ๊น์์ง๋ ๋ฐฉ๋ฒ
fpnum = sympy.lambdify(w, fprime)
2. ์ฒ์ ๐ค ๊ฐ์ ์ค์ ํ ๋ค, ๋ฐ๋ณต์ ์ผ๋ก ์ต์๊ฐ์ ํฅํด์ ์ ๊ทผ
w = 10.0 # starting guess for the min
for i in range(1000):
w = w - fpnum(w)*0.01 # with 0.01 the step size
๊ฒฐ๊ณผ๋ ๋ฏธ๋ถํ ๊ฐ๊ณผ ๊ฐ๋ค
์ค์ ๋ก ์ ์ฉํด๋ณด๊ธฐ :
linear regression ๋ฐฉ๋ฒ์ ์ฌ์ฉํด์ ์๊ฐ ํ๋ฆ์ ๋ฐ๋ฅธ ์ง๊ตฌ์ ์จ๋ ๋ณํ ๋ถ์
Global temperature anomaly๋ผ๋ ์งํ๋ฅผ ํตํด์ ๋ถ์์ ํด๋ณผ ๊ฒ์ ๋๋ค.
์ฌ๊ธฐ์ temperature anomaly๋ ์ด๋ ํ ๊ธฐ์ค ์จ๋ ๊ฐ์ ์ ํด๋๊ณ ๊ทธ๊ฒ๊ณผ์ ์ฐจ์ด๋ฅผ ๋ํ๋ธ ๊ฒ์ ๋๋ค. ์๋ฅผ ๋ค์ด์ temperature anomaly๊ฐ ์์์ ๋์ ๊ฐ์ ๊ฐ์ง๋ค๋ฉด ๊ทธ๊ฒ์ ํ์๋ณด๋ค ๋ฐ๋ฏํ ๊ธฐ์จ์ ๊ฐ์ก๋ค๋ ๋ง์ด๊ณ , ์์์ ์์ ๊ฐ์ ๊ฐ์ง๋ค๋ฉด ๊ทธ๊ฒ์ ํ์๋ณด๋ค ์ฐจ๊ฐ์ด ๊ธฐ์จ์ ๊ฐ์ก๋ค๋ ๋ง์ ๋๋ค.
์ธ๊ณ ์ฌ๋ฌ ์ง์ญ์ ์จ๋๊ฐ ๊ฐ๊ฐ ๋ค ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ global temperature anomaly๋ฅผ ์ฌ์ฉํด์ ๋ถ์์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค. ์์ธํ ๋ด์ฉ์ ์๋ ๋งํฌ์์ ํ์ธํ์ค ์ ์์ต๋๋ค.
https://www.ncdc.noaa.gov/monitoring-references/faq/anomalies.php
Global Surface Temperature Anomalies | National Centers for Environmental Information (NCEI)
www.ncei.noaa.gov
Step 1 : Read a data file
NOAA(National Oceanic and Atmospheric Administration) ํํ์ด์ง์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ฒ ์ต๋๋ค.
์๋ ๋ช ๋ น์ด๋ก ๋ฐ์ดํฐ๋ฅผ ๋ค์ด๋ฐ๊ณ , numpy ํจํค์ง๋ฅผ ์ด์ฉํด ๋ถ๋ฌ์ค๊ฒ ์ต๋๋ค.
from urllib.request import urlretrieve
import numpy
URL = 'http://go.gwu.edu/engcomp1data5?accessType=DOWNLOAD'
urlretrieve(URL, 'land_global_temperature_anomaly-1880-2016.csv')
fname = '/content/land_global_temperature_anomaly-1880-2016.csv'
year, temp_anomaly = numpy.loadtxt(fname, delimiter=',', skiprows=5, unpack=True)
Step 2 : Plot the data
Matplotlib ํจํค์ง์ pyplot์ ์ด์ฉํด์ 2D plot์ ์ฐ์ด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
from matplotlib import pyplot
%matplotlib inline
pyplot.rc('font', family='serif', size='18')
#You can set the size of the figure by doing:
pyplot.figure(figsize=(10,5))
#Plotting
pyplot.plot(year, temp_anomaly, color='#2929a3', linestyle='-', linewidth=1)
pyplot.title('Land global temperature anomalies. \n')
pyplot.xlabel('Year')
pyplot.ylabel('Land temperature anomaly [°C]')
pyplot.grid();
Step 3 : Analytically
Linear regression์ ํ๊ธฐ ์ํด์ ๋จผ์ ์ง์ ์ ์ ์ํ๊ฒ ์ต๋๋ค.

๊ทธ ๋ค์ ์ต์ํ ํด์ผ ํ cost function์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ด์ cost function ์ ๊ตฌํ๊ณ ์ ํ๋ ๋ณ์๋ก ๋ฏธ๋ถํ ๋ค 0์ด ๋๋๋ก ํ๋ ๊ฐ์ ์ฐพ์ผ๋ฉด ๋ฉ๋๋ค.
w = numpy.sum(temp_anomaly*(year - year.mean())) / numpy.sum(year*(year - year.mean()))
b = a_0 = temp_anomaly.mean() - w*year.mean()
print(w)
print(b)
#0.01037028394347266
#-20.148685384658464
์ด์ ๊ทธ๋ํ๋ก ๊ทธ๋ ค์ ํ์ธํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค
reg = b + w * year
pyplot.figure(figsize=(10, 5))
pyplot.plot(year, temp_anomaly, color='#2929a3', linestyle='-', linewidth=1, alpha=0.5)
pyplot.plot(year, reg, 'k--', linewidth=2, label='Linear regression')
pyplot.xlabel('Year')
pyplot.ylabel('Land temperature anomaly [°C]')
pyplot.legend(loc='best', fontsize=15)
pyplot.grid();
'๋จธ์ ๋ฌ๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ค์ค ๋ก์ง์คํฑ ํ๊ท (์ํํธ๋งฅ์ค ํ๊ท) (0) | 2022.12.05 |
---|---|
๋ก์ง์คํฑ ํ๊ท sklearn logistic regression iris python (0) | 2022.12.04 |
[๋จธ์ ๋ฌ๋4] Logistic Regression ๋ก์ง์คํฑ ํ๊ท pyhton (0) | 2022.11.30 |
[๋จธ์ ๋ฌ๋3] Multiple Linear Regression ๋ค์ค์ ํํ๊ท python (0) | 2022.11.30 |
[๋จธ์ ๋ฌ๋2] Polynomial Regression python (0) | 2022.11.30 |