<a href="https://colab.research.google.com/github/AI-odyssey/AI-odyssey.github.io/blob/master/linear%20regression.ipynb" target="_parent"></a>

# Linear regression
선형회귀 - 종속 변수 y와 한개 이상의 독립변수 X와의 선형관계를 모델링하는 방법론


```python
import sympy
import numpy

from matplotlib import pyplot
%matplotlib inline
sympy.init_printing()
```


```python
w = sympy.Symbol('w', real=True)

f = w**2 + 3*w - 5
f
```


```python
sympy.plotting.plot(f);
```


```python
fprime = f.diff(w)
fprime
```


```python
sympy.plotting.plot(fprime);
```


```python
sympy.solve(fprime, w)
```

# Gradient Descent


```python
fpnum = sympy.lambdify(w, fprime)
type(fpnum)
```




    function




```python
w = 10.0

for _ in range(1000):
  w = w - fpnum(w) *0.01

print(w)
```

    -1.4999999806458753


# 데이터셋 만들어보기


```python
x_data = numpy.linspace(-5, 5, 100)  #(시작점, 끝점, 점의 개수)
w_true = 2
b_true = 20

#y= wx+b 선분에 noise를 추가해서 출력(radom.normal 없으면 그냥 직선)
y_data = w_true*x_data + b_true + numpy.random.normal(size=len(x_data))
pyplot.scatter(x_data, y_data);
```


```python
x_data.shape
```


```python
y_data.shape
```


```python
w, b, x, y = sympy.symbols('w b x y')

cost_function = (w*x + b - y)**2
cost_function
```


```python
grad_b = sympy.lambdify([w,b,x,y], cost_function.diff(b), 'numpy')
grad_w = sympy.lambdify([w,b,x,y], cost_function.diff(w), 'numpy')
```


```python
w = 0 
b = 0

for _ in range(1000):
  descent_b = numpy.sum(grad_b(w,b,x_data,y_data))/len(x_data)
  descent_w = numpy.sum(grad_w(w,b,x_data,y_data))/len(x_data)
  w = w - descent_w*0.01
  b = b - descent_b*0.01

print(w)
print(b)

```

    1.974233739486593
    19.949641406595283



```python
pyplot.scatter(x_data,y_data)
pyplot.plot(x_data, w*x_data + b, 'r');
```


```python
from IPython.display import YouTubeVideo
YouTubeVideo('gGOzHVUQCw0')
```











```python
from urllib.request import urlretrieve
URL = 'http://go.gwu.edu/engcomp1data5?accessType=DOWNLOAD'
urlretrieve(URL, 'land_global_temperature_anomaly-1880-2016.csv')
```




    ('land_global_temperature_anomaly-1880-2016.csv',
     <http.client.HTTPMessage at 0x7fd8346bffd0>)




```python
import numpy
```


```python
fname = '/content/land_global_temperature_anomaly-1880-2016.csv'

year, temp_anomaly = numpy.loadtxt(fname, delimiter=',', skiprows=5, unpack=True)
```


```python
from matplotlib import pyplot
%matplotlib inline
```


```python
pyplot.plot(year, temp_anomaly);
```


```python
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();
```


```python
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



```python
reg = b + w * year
```


```python
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();
```


```python

```

bfc7ad8529a470b7da90572c461175a8b4364ccd
ipynb
Jupyter Notebook
linear regression.ipynb
AI-odyssey/AI-odyssey.github.io
31aa670933b7768751250321a77c79726174f805
MIT
linear regression.ipynb
AI-odyssey/AI-odyssey.github.io
31aa670933b7768751250321a77c79726174f805
MIT
linear regression.ipynb
AI-odyssey/AI-odyssey.github.io
31aa670933b7768751250321a77c79726174f805
MIT
Qwen/Qwen-72B
1. YES
2. YES