이 노트북을 주피터 노트북 뷰어(nbviewer.jupyter.org)로 보거나 구글 코랩(colab.research.google.com)에서 실행할 수 있습니다.
주피터 노트북 뷰어로 보기 | 구글 코랩(Colab)에서 실행하기 |
from sklearn.datasets import load_diabetes
diabetes = load_diabetes()
print(diabetes.data.shape, diabetes.target.shape)
(442, 10) (442,)
diabetes.data[0:3]
array([[ 0.03807591, 0.05068012, 0.06169621, 0.02187235, -0.0442235 , -0.03482076, -0.04340085, -0.00259226, 0.01990842, -0.01764613], [-0.00188202, -0.04464164, -0.05147406, -0.02632783, -0.00844872, -0.01916334, 0.07441156, -0.03949338, -0.06832974, -0.09220405], [ 0.08529891, 0.05068012, 0.04445121, -0.00567061, -0.04559945, -0.03419447, -0.03235593, -0.00259226, 0.00286377, -0.02593034]])
diabetes.target[:3]
array([151., 75., 141.])
import matplotlib.pyplot as plt
plt.scatter(diabetes.data[:, 2], diabetes.target)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
x = diabetes.data[:, 2]
y = diabetes.target
<퀴즈> 슬라이싱을 사용해 훈련 데이터 x에서 100번째 부터 109번째 까지 샘플을 출력해 보세요. 슬라이싱한 배열의 크기는 얼마인가요?
x_sample = x[99:109]
print(x_sample, x_sample.shape)
[-0.06440781 0.01750591 -0.04500719 0.02828403 0.04121778 0.06492964 -0.03207344 -0.07626374 0.04984027 0.04552903] (10,)
w = 1.0
b = 1.0
y_hat = x[0] * w + b
print(y_hat)
1.0616962065186886
print(y[0])
151.0
w_inc = w + 0.1
y_hat_inc = w_inc * x[0] + b
print(y_hat_inc)
1.0678658271705574
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
print(y_hat_inc)
1.1616962065186887
b_rate = (y_hat_inc - y_hat) / (b_inc - b)
print(b_rate)
1.0
b_new = b + 1
print(b_new)
2.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
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()
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()
class Neuron:
def __init__(self):
self.w = 1.0 # 가중치를 초기화합니다
self.b = 1.0 # 절편을 초기화합니다
def forpass(self, x):
y_hat = x * self.w + self.b # 직선 방정식을 계산합니다
return y_hat
def backprop(self, x, err):
w_grad = x * err # 가중치에 대한 그래디언트를 계산합니다
b_grad = 1 * err # 절편에 대한 그래디언트를 계산합니다
return w_grad, b_grad
def fit(self, x, y, epochs=100):
for i in range(epochs): # 에포크만큼 반복합니다
for x_i, y_i in zip(x, y): # 모든 샘플에 대해 반복합니다
y_hat = self.forpass(x_i) # 정방향 계산
err = -(y_i - y_hat) # 오차 계산
w_grad, b_grad = self.backprop(x_i, err) # 역방향 계산
self.w -= w_grad # 가중치 업데이트
self.b -= b_grad # 절편 업데이트
neuron = Neuron()
neuron.fit(x, y)
plt.scatter(x, y)
pt1 = (-0.1, -0.1 * neuron.w + neuron.b)
pt2 = (0.15, 0.15 * neuron.w + neuron.b)
plt.plot([pt1[0], pt2[0]], [pt1[1], pt2[1]])
plt.xlabel('x')
plt.ylabel('y')
plt.show()