代码如下:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
e0 = 1
e1 = 1
e2 = 1
alpha = 0.01
theta0 = np.random.uniform(0, 1)
theta1 = np.random.uniform(0, 1)
theta2 = np.random.uniform(0, 1)
theta = np.array([theta1, theta2])
eps = 1e-4
x = np.array([[2, 3], [4, 6], [7, 8], [12, 15]])
t = np.array([5, 8, 14, 17])
cnt = 0 # 迭代次数
while (abs(e0) >= eps or abs(e1) >= eps or abs(e2) >= eps): # 这里用绝对值小于eps进行判定 如果不用绝对值的话如果有数据小于0的话会直接跳出循环
cnt += 1
i = np.random.randint(0,4) #随机一个整数作为本次的训练数据
e0 = (np.sum((x[i] * theta)) + theta0 * 1 - t[i])
e1 = (np.sum((x[i] * theta)) + theta0 * 1 - t[i]) * x[i][0]
e2 = (np.sum((x[i] * theta)) + theta0 * 1 - t[i]) * x[i][1]
i = np.random.randint(0,4)
e0 += (np.sum((x[i] * theta)) + theta0 * 1 - t[i])
e1 += (np.sum((x[i] * theta)) + theta0 * 1 - t[i]) * x[i][0]
e2 += (np.sum((x[i] * theta)) + theta0 * 1 - t[i]) * x[i][1]
# 上面是求导公式
e0 /= 5
e1 /= 5
e2 /= 5
theta0 = theta0 - alpha * e0 # 公式
theta1 = theta1 - alpha * e1
theta2 = theta2 - alpha * e2
theta = np.array([theta1, theta2]) # 每次都要对theta进行更新以便进行下次迭代
print(cnt)
print(theta0, theta1, theta2)
# 至此我们已经得到了三个常数的近似值
运行结果如下图:



