当前位置:网站首页>线性回归模型学习笔记(1)
线性回归模型学习笔记(1)
2022-07-20 16:55:00 【羊咩咩咩咩咩】
对于线性回归模型的两种模型建立:
(1)通过公式完成模型的建立,再通过标准方程得出成本函数的参数值,最后带入方程得到预测值。
建立方程
import numpy as np
X =2*np.random.rand(100,1)
Y =4+3*X+np.random.rand(100,1)
通过标准方程得到参数值
X_b = np.c_[np.ones((100,1)),X]##对每一个例子进行添加了一个1,因为这里假设了常数项x0的值为1
theta_best =np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(Y)
theta_best
进行预测
X_new = np.array([[0],[2]])
X_new_b = np.c_[np.ones((2,1)),X_new]
y_predict = X_new_b.dot(theta_best)
y_predict
(2)通过sklearn包中的LinearRegression函数进行计算
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X,Y)
lin_reg.intercept_,lin_reg.coef_
这可以得出函数的偏差项和特征权重
y_pred = lin_reg.predict(X_new)
y_pred
进行预测
theta_best_svd,residuals,rank,s = np.linalg.lstsq(X_b,Y)
theta_best_svd
这里对linalg.lstsq()函数进行解释:这里是对函数进行了最小二乘回归,目的是使真实值与预测值的差的平方最小,而得出的四个参数的解释是:最小二乘回归的解,残差,矩阵的秩,奇异值。
梯度下降的目的:是通过迭代的方式找到成本函数的最低点。
梯度下降的三种模型:
(1)批处理梯度下降
eta = 0.1##学习率=0.1
n_iterations =1000##迭代1000次
m =100##有100个特征个数
import numpy as np
theta = np.random.rand(2,1)
for iteration in range(n_iterations):
theta = theta -eta*(2/m*X_b.T.dot(X_b.dot(theta)-Y))##进行梯度下降
这里使用的是以一个整体进行梯度下降,我们一开始假定了一个theta值,然后在不断的迭代中找到最优解,而这里每次减少增加的是成本函数的梯度向量,就是对成本函数的theta求偏导。
(2)随机梯度下降
##进行随机下降方法
n_epochs =50##迭代次数
t0,t1 = 5,50##学习进度超参数
def learning_schedule(t):
return t0 /(t+t1)
theta = np.random.randn(2,1)
for epoch in range(n_epochs):
for i in range(m):
random_index = np.random.randint(m)
xi = X_b[random_index:random_index+1]
yi =Y[random_index:random_index+1]
eta = learning_schedule(epoch*m+i)
theta = theta -eta*2*xi.T.dot(xi.dot(theta)-yi)
theta
随机梯度下降是以一个实例为参照进行计算的,为了防止其找到局部最小点,我们通过learning_schedule方法进行逐步减少学习率。
使用sklearn中的SGDRegressor进行随机梯度下降法
from sklearn.linear_model import SGDRegressor
sgd_reg = SGDRegressor(max_iter=1000,tol=1e-3,penalty=None,eta0=0.1)
sgd_reg.fit(X,Y.ravel())
sgd_reg.intercept_,sgd_reg.coef_
(3)小批量梯度下降
##小批量梯度下降:在随机取得小批量中进行m个的随机梯度下降
n_iterations = 50
minibatch_size = 20##一批的数量为20
np.random.seed(42)
theta = np.random.randn(2,1)
t0=5
t1=50
t=0
def learning_schedule(t):
return t0/(t+t1)
for iteration in range(n_iterations):
shuffle_indice = np.random.permutation(m)
x_shuffled = X_b[shuffle_indice]
y_shuffled =Y[shuffle_indice]
for i in range(0,m,minibatch_size):
t=t+1
xi = x_shuffled[i:i+minibatch_size]
yi = y_shuffled[i:i+minibatch_size]
eta = learning_schedule(t)
theta = theta -eta*(2/minibatch_size*xi.T.dot(xi.dot(theta)-yi))
theta
一开始将设置一个小批量的值,然后先对整个数据进行洗牌,然后每次选取一些数据进行计算
边栏推荐
- Samsung announced the completion of the research and development of the 5-nm EUV process: the performance is improved by 10% and the power consumption is reduced by 20%!
- 力扣每日一题-第40天-643. 字数组最大平均数Ⅰ
- 《程序设计基础》 第十章 函数与程序结构 7-2 汉诺(Hanoi)塔问题 (20 分)
- HTB-Bashed
- 【云原生】DevOps(四):集成Sonar Qube
- 专业的人做专业的事 GBASE参编数据库发展研究报告(2022年)、入选全球数据库产业图谱
- Wechat applet 05 obtains basic user information
- EL&JSTL:JSTL总结
- 线段树-区间修改树
- 山东大学、北邮、哈工大| Multimodal Dialog Systems with Dual Knowledge-enhanced Generative Pretrained Language Model(具有双重知识增强生成预训练语言模型的多模态对话系统)
猜你喜欢
MongoDB-复制(副本集)
Leetcode 展望2021
Low code that democratizes software development
2021-9-18
Wechat applet 05 obtains basic user information
Michael Bronstein 系列长文:迈向几何深度学习(之二)——感知器事件
Uniapp applet adds custom check box style
Netflix、麻省理工学院 | Uncertainty in Contrastive Learning: On the Predictability of Downstream Performance(对比学习中的不确定性:关于下游性能的可预测性)
HTB-Lame
Shandong University, Beijing post, Harbin Institute of technology | multimodal dialog systems with dual knowledge enhanced generic trained language model
随机推荐
Wechat applet 20 improve video page ①
lotus-bench 1.17.0-rc3 Benchmarks
《程序设计基础》 第十章 函数与程序结构 6-11 递归计算P函数 (15 分)
力扣每日一题-第40天-643. 字数组最大平均数Ⅰ
Face Huawei? Apple and Qualcomm quickly reconciled, but the price behind it is very high!
Django queries the queryset object data of MySQL database and converts it into JSON string
unity spritemask实现
sqlilabs less-25~25a
2021-09-16
三星宣布完成5纳米EUV工艺研发:性能提高10%,功耗降低20%!
个人学习 _numpy
Wechat applet 22 recommend and songdetail are initially set up
支持同花顺的证券公司怎么选择?手机上开户安不安全
《程序设计基础》 第十章 函数与程序结构 6-13 分治法求解金块问题 (20 分)
"Fundamentals of program design" Chapter 10 function and program structure 6-7 judge the three digits that meet the conditions (15 points)
解道--序言
2021-09-16
"Fundamentals of program design" Chapter 10 function and program structure 7-1 circular volume calculator (20 points)
线段树-区间修改树
《程序设计基础》 第十章 函数与程序结构 7-3 递归实现逆序输出整数 (15 分)