import numpy as np
t = np.array([400,336,369,232,540])
X = np.array([[1,2104,3],[1,1600,3],[1,2400,3],[1,1416,2],[1,3000,4]])
theta0 = 1
theta1 = 1
theta2 = 1
alpha = 0.000001
epsilon = 1e-5
e0 = 1
e1 = 1
e2 = 1
m = len(X)
for i in range(5):
while e0 > epsilon or e1 > epsilon or e2 > epsilon:
e0 = (1/m)*(theta0 * X[i][0] + theta1 * X[i][1] + theta2 * X[i][2] - t[i])
e1 = e0 * X[i, 1]
e2 = e0 * X[i, 2]
theta0 = theta0 - alpha * e0
theta1 = theta1 - alpha * e1
theta2 = theta2 - alpha * e2
i += 1
print(theta0,theta1,theta2)
梯度下降算法学习记录
最新推荐文章于 2024-08-23 15:15:47 发布