对于给定训练集 x = [4,8,5,10,12] y = [20,50,30,70,60] 参数的定义以及设置 #初始化参数 theta0 = theta1
= 0 #学习率 alpha = 0.00001 #迭代次数 cnt = 0 error0 = error1 = 0 #指定一个阈值,用于检查两次误差
thershold= 0.0000001 迭代进行 while True: #定义梯度 diff[0]-theta0 diff[1]-theta1 diff =
[0,0] m = len(x) for i in range(m): diff[0] = y[i] - (theta0+theta1*x[i]) diff[1
] =(y[i] - (theta0+theta1*x[i]))*x[i] theta0 = theta0 + alpha * diff[0] theta1 =
theta1+ alpha * diff[1] #误差计算 for i in range(m): error1 += (y[i] - (theta0+
theta1*x[i]))**2 error1 /= m if abs(error1 - error0) < thershold: break else:
error0= error1 cnt += 1 pass print('theta0',theta0) print('theta1',theta1) print
('cnt',cnt) 结果预测 print(predict(theta0,theta1,8))

技术
©2019-2020 Toolsou All rights reserved,
Java开发2020年最新常见面试题整理【Spring源码分析】42-@Conditional详解element-ui踩坑记录神仙面试宝典你有了吗?半月看完25大专题,居然斩获阿里P8offer使用css样式设计一个简单的html登陆界面XCTF攻防世界web新手练习_ 9_command_executionJS中的解构赋值的详解与具体用途Python Web 框架elementui 穿梭框 el-transfer 展示列表内容文字过长ConcurrentHashMap实现原理及源码解析