사이킷런, datasets 모듈에서 load_diabetes 패키지 임포트
matpoltlib.pyplot 모듈 임포트
```
from sklearn.datasets import load_diabetes
import matplotlib.pyplot as plt
diabetes = load_diabetes()
diabetes.data[0:3] # 배열 슬라이싱 샘플 0 1 2 뽑아내기
diabetes.target[:3] # 배열 슬라이싱 샘플 0 1 2 뽑아내기
print(diabetes.data.shape, diabetes.target.shape)
(442, 10) (442,)
plt.scatter(diabetes.data[:,2], diabetes.target)
# plt.scatter(diabetes.data[0:45,2], diabetes.target[0:45])
x = diabetes.data[0:45,2]
y = diabetes.target[0:45]
plt.scatter(x,y)
<matplotlib.collections.PathCollection at 0x20993fc0bc8>
-> 모델 훈련을 위한 '핵심 최적화 알고리즘'인 '경사하강법(gradientdescent)'에 대해 학습
-> 1개의 뉴런으로 구성된 첫 번째 모델 생성까지.
x = diabetes.data[:,2]
y = diabetes.target
plt.scatter(x,y)
w = 1.0
b = 1.0
y_hat = x[0]*w + b
print(y_hat, y[0], sep=" / ")
1.0616962065186886 / 151.0
w_inc = w + 0.1
y_hat_inc = x[0]*w_inc + b
print(y_hat_inc, y[0], sep=" / ")
1.0678658271705574 / 151.0
w_rate = (y_hat_inc - y_hat)/(w_inc - w)
print(w_rate)
0.061696206518688734
w_new = w + w_rate
print(w_new)
1.0616962065186888
b_inc = b + 0.1
y_hat_inc = x[0]*w + b_inc
b_rate = (y_hat_inc - y_hat)/(b_inc - b)
print(b_rate)
1.0
err = y[0] - y_hat
w_new = w + w_rate*err
b_new = b + 1*err
print(w_new, b_new)
10.250624555904514 150.9383037934813
y_hat = x[1]*w_new + b_new
err = y[1] - y_hat
w_rate = x[1]
w_new = w_new + w_rate*err
b_new = b_new + 1*err
print(w_new, b_new)
14.132317616381767 75.52764127612664
x = diabetes.data[:,2]
y = diabetes.target
for x_i, y_i in zip(x,y):
y_hat = x_i*w + b
err = y_i - y_hat
w_rate = x_i
w = w + w_rate*err
b = b + 1*err
print(w,b)
587.8654539985689 99.40935564531424
plt.scatter(x,y)
pt1 = (-0.1, -0.1*w+b)
pt2 = (0.15, 0.15*w + b)
plt.plot( [pt1[0],pt2[0]], [pt1[1],pt2[1]] )
plt.xlabel('x')
plt.ylabel('y')
plt.show()
for i in range(1, 100):
for x_i, y_i in zip(x,y):
y_hat = x_i*w + b
err = y_i - y_hat
w_rate = x_i
w = w + w_rate*err
b = b + 1*err
print(w,b)
913.5973364345905 123.39414383177204
plt.scatter(x,y)
pt1 = (-0.1, -0.1*w+b)
pt2 = (0.15, 0.15*w + b)
plt.plot( [pt1[0],pt2[0]], [pt1[1],pt2[1]] )
plt.xlabel('x')
plt.ylabel('y')
plt.show()
`y^ = 913.6x + 123.4`
x_new = 0.18
y_pred = x_new*w+b
print(y_pred)
287.8416643899983
plt.scatter(x,y)
plt.scatter(x_new,y_pred)
plt.xlabel('x')
plt.ylabel('y')
plt.show()