吴恩达机器学习笔记(三)

多元线性回归

多元线性回归(Multivariate linear regression)

假设函数: h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 x 2 + ⋯ + θ n x n h_\theta(x)=\theta_0+\theta_1x_1+\theta_2x_2+\dots+\theta_nx_n hθ(x)=θ0+θ1x1+θ2x2++θnxn 。 定义 x 0 = 1 x_0=1 x0=1
从而: x = [ x 0 , x 1 , x 2 , … , x n ] T , x ∈ R n + 1 x=[x_0,x_1,x_2,\dots,x_n]^T,x\in\R^{n+1} x=[x0,x1,x2,,xn]T,xRn+1; θ = [ θ 0 , θ 1 , θ 2 , … , θ n ] T \theta=[\theta_0,\theta_1,\theta_2,\dots,\theta_n]^T θ=[θ0,θ1,θ2,,θn]T, θ ∈ R n + 1 \theta\in\R^{n+1} θRn+1
假设函数可记作: h θ ( x ) = θ T x h_\theta(x)=\theta^Tx hθ(x)=θTx

在这里插入图片描述

多元梯度下降法演练(I):特征缩放

Gradient descent in practice I:Feature Scaling
特征缩放:Feature Scaling
ldea: Make sure features are on a similar scale.
确保特征具有类似的规模。有利于加快梯度下降算法运行速度,加快收敛到全局最小值
方法:Mean normalization
x i = x i − μ σ x_i=\frac{x_i-\mu}{\sigma} xi=σxiμ
其中 μ \mu μ为平均值, σ 为标准差;
max-min: x i = x i − m i n m a x − m i n x_i=\frac{x_i-min}{max-min} xi=maxminximin

多元梯度下降法演练(II):学习率

Gradient descent in practice II:Learning rate
梯度下降更新公式: θ j : = θ j − α ∂ ∂ θ j J ( θ ) \theta_j:=\theta_j-\alpha\frac{\partial}{\partial\theta_j}J(\theta) θj:=θjαθjJ(θ),

  • “Debugging”:'How to make sure gradient descent is working correctly.
  • How to choose learning rate.

在这里插入图片描述
总结:
1)如果α太小:收敛慢。
2)如果α太大:每一次迭代过程中 J(θ)将会不断的越过最小值,无法收敛。
3)choose α: … , 0.001 , 0.003 , 0.01 , 0.03 , 0.1 , 0.3 , 1 …
寻找一个合适的较小值和较大值,保证结果和速度的同时选取较大的值,或者稍小的合理值。

特征和多项式回归

Features and polynomial regression
在这里插入图片描述假设有两个特征: x 1 x_1 x1​表示临街宽度, x 2 x_2 x2​表示纵向深度,可做假设:
h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 x 2 h_\theta(x)=\theta_0+\theta_1x_1+\theta_2x_2 hθ(x)=θ0+θ1x1+θ2x2
选择构造新特征值:房屋面积 x = x 1 ∗ x 2 x=x_1*x_2 x=x1x2 ,
此时假设函数: h θ ( x ) = θ 0 + θ 1 x h_\theta(x)=\theta_0+\theta_1x hθ(x)=θ0+θ1x
多项回归Polynomial regression

二次模型:quadratic modle
三次模型:cubic modle
选择一个合理的模型

正规方程

Normal equation:Method to solve for θ analytically.
提供一种求的θ解析方法。
代价函数: J ( θ ) = 1 2 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2 J(\theta)=\frac{1}{2m}\sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})^2 J(θ)=2m1i=1m(hθ(x(i))y(i))2
对 J(θ)求偏导并令导数为零可解得: θ = ( X T X ) − 1 X T y \theta=(X^TX)^{-1}X^Ty θ=(XTX)1XTy,推导过程如下:
在这里插入图片描述在这里插入图片描述 ∂ ∂ θ J ( θ ) = 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x ( i ) = 0 \frac{\partial}{\partial\theta}J(\theta)=\frac{1}{m}\sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})x^{(i)}=0 θJ(θ)=m1i=1m(hθ(x(i))y(i))x(i)=0 可得 θ = ( X T X ) − 1 X T y \theta = (X^TX)^{-1}X^Ty θ=(XTX)1XTy
原文链接:https://blog.csdn.net/qq_29317617/article/details/86312154
在这里插入图片描述总结:梯度下降VS正规方程
在这里插入图片描述梯度下降算法:需要选择学习速率 α; 需要许多次迭代;当特征数量n较大时也能够运转正常;
正规方程: 无需选择参数;无需迭代;需要计算 ( X T X ) − 1 (X^TX)^{-1} (XTX)1 (XTX){-1} ;当n较大时计算速度慢
————————————————
版权声明:本文为CSDN博主「阳阳yyx」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29317617/article/details/86312154

正规方程在矩阵不可逆的情况下的解决方案

θ = ( X T X ) − 1 X T y \theta = (X^TX)^{-1}X^Ty θ=(XTX)1XTy
What if ( X T X ) − 1 (X^TX)^{-1} (XTX)1 is non-invertible(不可逆)
(singular奇异矩阵/degenerate退化矩阵)?
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值