简单解释:比如拿温度传感器来说,就是根据之前一段时间的温度数据计算下当前理论上应该测量到的温度,如果超出这个最优解的一定比例,就可以理解为突发状况了
代码如下:
# _*_ coding: utf-8 _*_
# 作者: 黄智
# 邮箱: 99923309@qq.com
# 训练集
# 每个样本点有3个分量 (x0,x1,x2)
#x = [(1, 0., 3), (1, 1., 3), (1, 2., 3), (1, 3., 2), (1, 4., 4)]
x = [(1), (2), (1), (2), (1.1), (1.2), (1.3), (1.4), (1.5)]
# y[i] 样本点对应的输出
y = [85.364, 77.217205, 75.195834, 90.105519, 89.342380, 70.123323, 80.12233, 88.34353, 98.34353]
# 迭代阀值,当两次迭代损失函数之差小于该阀值时停止迭代
epsilon = 0.0001
# 学习率
alpha = 0.01
diff = [0, 0]
max_itor = 1000
error1 = 0
error0 = 0
cnt = 0
m = len(x)
# 初始化参数
theta0 = 0
theta1 = 0
theta2 = 0
while True:
cnt += 1
# 参数迭代计算
for i in range(m):
# 拟合函数为 y = theta0 * x[0] + theta1 * x[1] +theta2 * x[2]
# 计算残差
#diff[0] = (theta0 + theta1 * x[i][1] + theta2