没有合适的资源?快使用搜索试试~ 我知道了~
上一篇文章讲述了梯度下降法的数学思想,趁热打铁,这篇博客笔者将使用梯度下降法完成多元线性回归,话不多说,直接开始。 我们假设我们的目标函数是长这样的: import numpy as np import pandas as pd # 读入数据 data = pd.read_csv('D:/Advertising.csv') # 学习率alpha lr = 0.00001 # 参数 theta0 = 0 theta1 = 0 theta2 = 0 theta3 = 0 # 最大迭代次数 epochs = 1000 #假设目标函数 def h_predict(theta0, theta1, t
资源推荐
资源详情
资源评论
多元线性回归及其算法实现(梯度下降法)多元线性回归及其算法实现(梯度下降法)
上一篇文章讲述了梯度下降法的数学思想,趁热打铁,这篇博客笔者将使用梯度下降法完成多元线性回归梯度下降法完成多元线性回归,话不多说,直接开始。
我们假设我们的目标函数是长这样的:
import numpy as np
import pandas as pd
# 读入数据
data = pd.read_csv('D:/Advertising.csv')
# 学习率alpha
lr = 0.00001
# 参数
theta0 = 0
theta1 = 0
theta2 = 0
theta3 = 0
# 最大迭代次数
epochs = 1000
#假设目标函数
def h_predict(theta0, theta1, theta2, theta3, x_data, i):
h_pre=(theta0 + theta1 * x_data.iloc[i,0] + theta2*x_data.iloc[i,1] + theta3*x_data.iloc[i,2])
return h_pre
# 最小二乘法
def compute_error(theta0, theta1, theta2, theta3, x_data, y_data):
totalError = 0
for i in range(0, len(x_data)):
totalError += (h_predict(theta0, theta1, theta2, theta3, x_data, i)-y_data[i]) ** 2
return totalError / float(len(x_data))
def gradient_descent_runner(x_data, y_data, theta0, theta1, theta2, theta3, lr, epochs):
# 计算总数据量
m = float(len(x_data))
# 循环epochs次
for i in range(epochs):
theta0_grad = 0
theta1_grad = 0
theta2_grad = 0
theta3_grad = 0
# 求偏导
for j in range(0, len(x_data)):
theta0_grad += (1/m) * (h_predict(theta0, theta1, theta2, theta3, x_data, j) - y_data.iloc[j])
theta1_grad += (1/m) * x_data.iloc[j,0] * (h_predict(theta0, theta1, theta2, theta3, x_data, j) - y_data.iloc[j])
theta2_grad += (1/m) * x_data.iloc[j,1] * (h_predict(theta0, theta1, theta2, theta3, x_data, j) - y_data.iloc[j])
theta3_grad += (1/m) * x_data.iloc[j,2] * (h_predict(theta0, theta1, theta2, theta3, x_data, j) - y_data.iloc[j])
# 更新b和k
theta0 = theta0 - (lr*theta0_grad)
theta1 = theta1 - (lr*theta1_grad)
theta2 = theta2 - (lr*theta2_grad)
theta3 = theta3 - (lr*theta3_grad)
#print(compute_error(theta0, theta1, theta2, theta3, x_data, y_data))
return theta0, theta1, theta2 ,theta3
#计算初始时代价函数值
print("Starting theta0 = {0}, theta1 = {1}, theta2 = {2}, theta3 = {3}, error = {4}".
format(theta0, theta1, theta2, theta3, compute_error(theta0, theta1, theta2, theta3, x_data, y_data)))
print("Running...")
theta0, theta1, theta2, theta3 = gradient_descent_runner(x_data, y_data, theta0, theta1, theta2, theta3, lr, epochs)
#经过epochs次迭代后代价函数值
print("After {0} iterations theta0 = {1}, theta1 = {2}, theta2 = {3}, theta3 = {4}, error = {5}".
format(epochs, theta0, theta1, theta2, theta3, compute_error(theta0, theta1, theta2, theta3, x_data, y_data)))
运行结果如图:
可以看到error值从最开始的223变成了4,最后我将第一行数据带入验证:
资源评论
weixin_38609089
- 粉丝: 5
- 资源: 925
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- J185-VB一款SOT23封装P-Channel场效应MOS管
- 计算整数各位数字之和,利用java代码实现
- J185-T2B-VB一款SOT23封装P-Channel场效应MOS管
- map20231226Kalmanfilter.ipynb
- J185-T1B-VB一款SOT23封装P-Channel场效应MOS管
- ASME Y14.5-2018 尺寸与公差标注 中文版
- J185-T1B-A-VB一款SOT23封装P-Channel场效应MOS管
- J168-VB一款SOT23封装P-Channel场效应MOS管
- J166-VB一款SOT23封装P-Channel场效应MOS管
- i2c测试程序-linux-如何调试i2c.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功