a = [] b = [] def gradient_descent(theta_0, theta_1, x, y, iterations, alpha): """주어진 theta_0, theta_1 변수들을 경사 하강를 하면서 업데이트 해주는 함수""" for _ in range(iterations): # 정해진 번만큼 경사 하강을 한다 error = prediction_difference(theta_0, theta_1, x, y) # 예측값들과 입력 변수들의 오차를 계산 # 코드를 쓰세요 theta_0 = theta_0 - alpha * error.mean() theta_1 = theta_1 - alpha * (error * x).mean() a.append(theta_0) # theta_0 을 a에 list로 생성 b.append(theta_1) return theta_0, theta_1
댓글 1개
a = []
b = []
def gradient_descent(theta_0, theta_1, x, y, iterations, alpha):
"""주어진 theta_0, theta_1 변수들을 경사 하강를 하면서 업데이트 해주는 함수"""
for _ in range(iterations): # 정해진 번만큼 경사 하강을 한다
error = prediction_difference(theta_0, theta_1, x, y) # 예측값들과 입력 변수들의 오차를 계산
# 코드를 쓰세요
theta_0 = theta_0 - alpha * error.mean()
theta_1 = theta_1 - alpha * (error * x).mean()
a.append(theta_0) # theta_0 을 a에 list로 생성
b.append(theta_1)
return theta_0, theta_1
.
.
.
# 학습률 0.1로 200번 경사 하강
theta_0, theta_1 = gradient_descent(theta_0, theta_1, house_size, house_price, 500, 0.1)
plt.plot(a)
plt.plot(b)
plt.show()