代码如下:
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)) &