北京南站地铁线路图,wordpress添加特效,平面设计网上接单一个月能赚多少,如何查看网站的点击量正规方程与梯度下降API及案例预测 文章目录 正规方程与梯度下降API及案例预测1. 正规方程与梯度下降正规方程#xff08;Normal Equation#xff09;梯度下降#xff08;Gradient Descent#xff09; 2. API3. 波士顿房价预测 1. 正规方程与梯度下降
回归模型是机器学习中…正规方程与梯度下降API及案例预测 文章目录 正规方程与梯度下降API及案例预测1. 正规方程与梯度下降正规方程Normal Equation梯度下降Gradient Descent 2. API3. 波士顿房价预测 1. 正规方程与梯度下降
回归模型是机器学习中用于预测连续数值实数的模型通常用于解决回归问题。两种常见的回归模型求解方法是正规方程和梯度下降。
正规方程Normal Equation
正规方程是一种封闭解法用于直接计算线性回归模型的权重系数。
原理 给定一个线性回归模型的数据集我们的目标是找到最佳的权重系数w使得模型的预测值尽可能接近实际值。正规方程的原理是通过最小化损失函数来找到最佳权重。对于线性回归问题损失函数通常是均方误差Mean Squared Error J ( w ) 1 2 m ∑ i 1 m ( h w ( x ( i ) ) − y ( i ) ) 2 J(w) \frac{1}{2m} \sum_{i1}^{m} (h_w(x^{(i)}) - y^{(i)})^2 J(w)2m1i1∑m(hw(x(i))−y(i))2
其中m 是训练样本数量 h w ( x ( i ) ) h_w(x^{(i)}) hw(x(i)) 是模型的预测值 y ( i ) y^{(i)} y(i) 是实际值。
正规方程的目标是找到权重w使损失函数J(w)最小化。通过求解损失函数的梯度等于零的方程可以得到权重w的解析解 ∇ J ( w ) 0 \nabla J(w) 0 ∇J(w)0
这个方程的解即为最佳权重w从而得到线性回归模型。
优点
正规方程提供了封闭解不需要手动选择学习率或迭代次数。适用于小型数据集通常在特征数量较少时表现良好。
缺点
对于大型数据集计算复杂度高需要计算特征矩阵的逆时间复杂度较高。不适用于非线性模型。
梯度下降Gradient Descent
梯度下降是一种迭代优化算法用于调整模型的参数使损失函数最小化。
原理 梯度下降的核心思想是通过迭代来更新模型参数使损失函数逐渐减小。对于线性回归梯度下降的损失函数是均方误差Mean Squared Error目标是最小化这个损失函数。
梯度下降的迭代过程如下
初始化权重w。计算损失函数J(w)关于权重 w w w的梯度 ∇ J ( w ) \nabla J(w) ∇J(w)更新权重w通常按照以下规则更新 w w − α ∇ J ( w ) w w - \alpha \nabla J(w) ww−α∇J(w) 其中α是学习率控制每次更新的步长。重复步骤2和3直到满足停止条件例如达到最大迭代次数或损失函数收敛。
梯度下降的关键是学习率α的选择过大的学习率可能导致算法不收敛过小的学习率可能导致收敛速度慢。
优点
适用于大型数据集和高维特征计算复杂度较低。可以用于各种不同类型的模型和损失函数包括非线性模型。
缺点
需要手动选择学习率和迭代次数选择不当可能导致收敛问题或性能下降。对特征缩放和初始化敏感。
2. API
sklearn.linear_model.LinearRegression(fit_interceptTrue)
通过正规方程优化fit_intercept是否计算偏置LinearRegression.coef_回归系数LinearRegression.intercept_偏执
sklearn.linear_model.SGDRegressor(loss“squared_loss”,fit_interceptTrue,learning_rate“invscaling”,eta00.01)
SGDRegressor类实现了随机梯度下降学习它支持不同的loss函数和正则化惩罚项来拟合线性回归模型loss损失类型 loss“squared_loss”:普通最小二乘法 fit_intercept是否计算偏置learning_rate:string,optional 学习率填充“constant”:etaeta0“optimal”:eta1.0/(alpha*(tt0))[default]“invscaling”:etaeta0/pow(t,power_t),power_t存在父类之中对于一个常数值的学习率来说可以使用learning_rate“constant”,并使用eta0来指定学习率 SGDRegressor.coef_:回归系数SGDRegressor.intercept_:偏置
3. 波士顿房价预测
实例数量506属性数量13数值型或类别墅帮助预测的属性属性信息 CRIM城镇人均犯罪率ZN占地面积超过2.5万平方英尺的住宅用地比例INDUS城镇非零售业务地区的比例CHAS查尔斯河虚拟变量1如果土地在河边否则是0NOX一氧化氮浓度每1000万份RM平均每居民房数AGE在1940年之前建成的所有者占用单位的比例DIS与五个波士顿就业中心的加权距离RAD辐射状公路的可达性指数TAX每10000美元的全额物业税率PTRATIO城镇师生比例B 1000Bk-0.63^2其中Bk是城镇中的黑人比例LSTAT人口中地位较低人群的百分数MEDV以1000美元计算的自由住房的中位数 缺失属性值无
流程
获取数据集划分数据集特征工程无量纲化处理–标准化预估器流程fit()–模型coef_,intercept_模型评估
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegressiondef linear_demo():正规方程的方法对波士顿房价进行预测:return:# 1. 获取数据boston load_boston()# 2. 划分数据集x_train, x_test, y_train,y_test train_test_split(boston.data, boston.target, random_state 22)# 3. 标准化transfer StandardScaler()x_train transfer.fit_transform(x_train)x_test transfer.transform(x_test)# 4. 预估器estimator LinearRegression()estimator.fit(x_train, y_train)# 5. 得出模型print(权重系数为\n, estimator.coef_)print(偏置为\n, estimator.intercept_)# 6. 模型评估y_predict estimator.predict(x_test)print(y_predict:\n, y_predict)print(直接对比真实值和预测值\n, y_test y_predict)score estimator.score(x_test, y_test)print(准确率为\n, score)from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import SGDRegressordef linear_demo():梯度下降的方法对波士顿房价进行预测:return:# 1. 获取数据boston load_boston()# 2. 划分数据集x_train, x_test, y_train,y_test train_test_split(boston.data, boston.target, random_state 22)# 3. 标准化transfer StandardScaler()x_train transfer.fit_transform(x_train)x_test transfer.transform(x_test)# 4. 预估器estimator SGDRegressor()estimator.fit(x_train, y_train)# 5. 得出模型print(权重系数为\n, estimator.coef_)print(偏置为\n, estimator.intercept_)# 6. 模型评估y_predict estimator.predict(x_test)print(y_predict:\n, y_predict)print(直接对比真实值和预测值\n, y_test y_predict)score estimator.score(x_test, y_test)print(准确率为\n, score)
文章转载自: http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.mmtjk.cn.gov.cn.mmtjk.cn http://www.morning.wkxsy.cn.gov.cn.wkxsy.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.dwrbn.cn.gov.cn.dwrbn.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.pdwzr.cn.gov.cn.pdwzr.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.qlkjh.cn.gov.cn.qlkjh.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.xbbrh.cn.gov.cn.xbbrh.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.sblgt.cn.gov.cn.sblgt.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.mprtj.cn.gov.cn.mprtj.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.ldynr.cn.gov.cn.ldynr.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.tgtrk.cn.gov.cn.tgtrk.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.fdfdz.cn.gov.cn.fdfdz.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.drgmr.cn.gov.cn.drgmr.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn