LR逻辑回归是一种监督学习分类算法,其实现了给定数据集到0,1的一种映射。
给定数据集 D = { ( x 1 , y 1 ) , ( x 2 , y 2 ) … ( x m , y m ) } \mathrm{D}=\{(x 1, y 1),(x 2, y 2) \ldots(xm, ym)\} D={(x1,y1),(x2,y2)…(xm,ym)}其中 ( x i , y i ) (xi,yi) (xi,yi)表示第 i i i个样本,其中 x i = ( x i 1 , x i 2 , . . x i 1 n ) xi=\left(x i_{1}, x i_{2}, . . x i_{1 n}\right) xi=(xi1,xi2,..xi1n)。即每个数据有 n n n个特征,类别 y = { 0 , 1 } y=\{0,1\} y={0,1},要求训练数据,将数据分成两类0或1。
假定 x i xi xi的 n n n个特征为线性关系,即: z = θ x + b = θ 1 x 1 + θ 1 x 1 + ⋯ + θ n x n + b \mathrm{z}=\theta \mathrm{x}+\mathrm{b}=\theta_{1} x_{1}+\theta_{1} x_{1}+\cdots+\theta_{n} x_{n}+\mathrm{b} z=θx+b=θ1x1+θ1x1+⋯+θnxn+b这里为了表示简洁,在数据样本 x i xi xi添加一个特征 x 0 = 1 x0=1 x0=1 ,将 b b b作为 θ 0 \theta_0 θ0。则有: z = θ x + b = b ∗ 1 + θ 1 x 1 + θ 1 x 1 + ⋯ + θ n x n = θ 0 x 0 + θ 1 x 1 + θ 1 x 1 + ⋯ + θ n x n = θ T X \begin{array}{l}{\mathrm{z}=\theta \mathrm{x}+\mathrm{b}} \\ {=b * 1+\theta_{1} x_{1}+\theta_{1} x_{1}+\cdots+\theta_{n} x_{n}} \\ {=\theta_{0} x_{0}+\theta_{1} x_{1}+\theta_{1} x_{1}+\cdots+\theta_{n} x_{n}=\theta^{T} X}\end{array} z=θx+b=b∗1+θ1x1+θ1x1+⋯+θnxn=θ0x0+θ1x1+θ1x1+⋯+θnxn=θTX以上实现了用样本 x i xi xi的 n n n个特征来表示样本的表达式,现在需要寻找一个映射使得 z \mathrm{z} z可以转换为0或者1。
可以使用阶跃函数,但是阶跃函数性质不好,不可导求解过于复杂,这里选用Sigmoid函数: y ( z ) = 1 1 + e − z \quad \mathrm{y}(\mathrm{z})=\frac{1}{1+e^{-z}} y(z)=1+e−z1
当输入一个Z时,y输出一个0–1之间的数,假定y>0.5则最终结果判为1, y<0.5最终结果为0。当y=0.8时,最终结果为1,y=0.8也表征了此时输出为1的概率,令: h θ ( x ) = 1 1 + e − θ T X h_{\theta}(x)=\frac{1}{1+e^{-\theta^{T} X}} hθ(x)=1+e−θTX1将样本特征线性表示,然后输入到Sigmoid函数,输出结果在0–1之间,并且输出结果表征了分类结果为1的概率,即有: P ( y = 1 ∣ x ; θ ) = h θ ( x ) P ( y = 0 ∣ x ; θ ) = 1 − h θ ( x ) \begin{array}{l}{\mathrm{P}(\mathrm{y}=1 | \mathrm{x} ; \theta)=h_{\theta}(x)} \\ {\mathrm{P}(\mathrm{y}=0 | \mathrm{x} ; \theta)=1-h_{\theta}(x)}\end{array} P(y=1∣x;θ)=hθ(x)P(y=0∣x;θ)=1−hθ(x)即 h θ ( x ) h_{\theta}(x) hθ(x)输出刚好代表了结果为1的概率,因此p(y|x)表达式: P ( y ∣ x ; θ ) = h θ ( x ) y ∗ ( 1 − h θ ( x ) ) 1 − y \mathrm{P}(\mathrm{y} | \mathrm{x} ; \theta)=h_{\theta}(x)^{y} *\left(1-h_{\theta}(x)\right)^{1-y} P(y∣x;θ)=hθ(x)y∗(1−hθ(x))1−y假设样本独立且同分布,最大似然估计: L ( θ ) = ∏ i = 1 i = m P ( y i ∣ x i ; θ ) = ∏ i = 1 i = m h θ ( x i ) y i ∗ ( 1 − h θ ( x i ) ) 1 − y i \mathrm{L}(\theta)=\prod_{i=1}^{i=m} \mathrm{P}(\mathrm{yi} | \mathrm{xi} ; \theta)=\prod_{i=1}^{i=m} h_{\theta}(x i)^{y i} *\left(1-h_{\theta}(x i)\right)^{1-y i} L(θ)=i=1∏i=mP(yi∣xi;θ)=i=1∏i=mhθ(xi)yi∗(1−hθ(xi))1−yi进而求最大对数似然估计: l ( θ ) = log L ( θ ) = ∑ i = 1 m ( y i ∗ log h θ ( x i ) + ( 1 − y i ) ∗ log ( 1 − h θ ( x i ) ) ) \mathcal{l}(\theta)=\log L(\theta)=\sum_{i=1}^{m}(y_i*\log h_{\theta}(xi)+(1-yi)*\log(1-h_{\theta}(xi))) l(θ)=logL(θ)=i=1∑m(yi∗loghθ(xi)+(1−yi)∗log(1−hθ(xi)))
1. 第一个问题,为什么要求最大对数似然估计而不是最大似然估计:
2. 第二个问题,LR的损失函数是什么: J ( θ ) = − 1 m l ( θ ) = − 1 m ∗ ∑ i = 1 m ( y i ∗ log h θ ( x i ) + ( 1 − y i ) ∗ log ( 1 − h θ ( x i ) ) ) \begin{array}{l}{J(\theta)=-\frac{1}{m} l(\theta)} {=-\frac{1}{m} * \sum_{i=1}^{m}\left(y i * \log h_{\theta}(x i)+(1-y i) * \log \left(1-h_{\theta}(x i)\right)\right)}\end{array} J(θ)=−m1l(θ)=−m1∗∑i=1m(yi∗loghθ(xi)+(1−yi)∗log(1−hθ(xi)))损失函数表征预测值与真实值之间的差异程度,如果预测值与真实值越接近则损失函数应该越小。在此损失函数可以取为最大似然估计函数的相反数,其次除以m这一因子并不改变最终求导极值结果,通过除以m可以得到平均损失值,避免样本数量对于损失值的影响。
这里采用随机梯度下降,损失函数对于 θ j \theta_j θj偏导:
θ j \theta_j θj的迭代式: θ j : = θ j − α ∗ ∂ J ( θ ) ∂ θ j = θ j − α ∑ i = 1 i = m [ ( h θ ( x i ) − y i ) ∗ x i j ] j = 0 , 1 , 2 … n \theta_{j} :=\theta_{j}-\alpha * \frac{\partial J(\theta)}{\partial \theta_{j}}=\theta_{j}-\alpha \sum_{i=1}^{i=m}\left[\left(h_{\theta}(x i)-y i\right) * x i_{j}\right] j=0,1,2 \ldots n θj:=θj−α∗∂θj∂J(θ)=θj−αi=1∑i=m[(hθ(xi)−yi)∗xij]j=0,1,2…n
损失函数:表征模型预测值与真实值的不一致程度。记为函数 L ( Y , f ( X ) ) L(Y,f(X)) L(Y,f(X))
结构风险函数 = 经验风险项 + 正则项,其中损失函数为经验风险项的重要组成部分。 Ω ( θ ) = ∑ i = 1 i = m L ( y i , f ( x i ; θ ) ) + λ ψ ( θ ) \Omega(\theta)=\sum_{i=1}^{i=m} L(y i, f(x i ; \theta))+\lambda \psi(\theta) Ω(θ)=i=1∑i=mL(yi,f(xi;θ))+λψ(θ)前半部分为经验风险项,后半部分为正则项。
L ( Y , P ( Y ∣ X ) ) = − log P ( Y ∣ X ) \mathrm{L}(\mathrm{Y}, \mathrm{P}(\mathrm{Y} | \mathrm{X}))=-\log P(Y | X) L(Y,P(Y∣X))=−logP(Y∣X) P ( Y ∣ X ) P(Y|X) P(Y∣X)为样本为 Y Y Y的概率,数值越大说明预测值与真实值越接近即损失函数应该越小,当 P ( Y ∣ X ) P(Y|X) P(Y∣X)越大的, − log P ( Y ∣ X ) -\log P(Y|X) −logP(Y∣X)越小,刚好符合损失函数的定义。
其中LR逻辑回归损失函数即为对数损失函数:
逻辑回归假设样本服从伯努利分布(0-1分布),然后求得满足该分布的似然函数,接着取对数求极值最小化负的似然函数(即 m a x F ( y , f ( x ) ) — − > m i n − [ F ( y , f ( x ) ) ] max F(y, f(x)) —-> min -[F(y, f(x))] maxF(y,f(x))—−>min−[F(y,f(x))])
即LR的损失函数为:负的对数损失函数: J ( θ ) = − 1 m l ( θ ) = − 1 m ∗ ∑ i = 1 i = m ( y i ∗ log h θ ( x i ) + ( 1 − y i ) ∗ log ( 1 − h θ ( x i ) ) \begin{array}{l}{J(\theta)=-\frac{1}{m} l(\theta)} {=-\frac{1}{m} * \sum_{i=1}^{i=m}\left(y i * \log h_{\theta}(x i)+(1-y i) * \log \left(1-h_{\theta}(x i)\right)\right.}\end{array} J(θ)=−m1l(θ)=−m1∗∑i=1i=m(yi∗loghθ(xi)+(1−yi)∗log(1−hθ(xi))
L ( Y , f ( X ) ) = ( Y − f ( X ) 2 \mathrm{L}(\mathrm{Y}, \mathrm{f}(\mathrm{X}))=\left(Y-f(X)^{2}\right. L(Y,f(X))=(Y−f(X)2线性回归模型使用了平方损失函数: E w ^ = ( y − X w ^ ) T ( y − X w ^ ) E_{\hat{\boldsymbol{w}}}=(\boldsymbol{y}-\mathbf{X} \hat{\boldsymbol{w}})^{\mathrm{T}}(\boldsymbol{y}-\mathbf{X} \hat{\boldsymbol{w}}) Ew^=(y−Xw^)T(y−Xw^)在线性回归中,它假设样本和噪声都服从高斯分布(中心极限定理),最后通过极大似然估计(MLE)可以推导出最小二乘式子。
L ( y , f ( x ) ) = e − y f ( x ) L ( y , f ( x ) ) = 1 n ∑ i = 1 i = m e − y i f ( x i ) \begin{aligned} \mathrm{L}(\mathrm{y}, \mathrm{f}(\mathrm{x})) &=e^{-y f(x)} \\ \mathrm{L}(\mathrm{y}, \mathrm{f}(\mathrm{x})) &=\frac{1}{n} \sum_{i=1}^{i=m} e^{-y i f(x i)} \end{aligned} L(y,f(x))L(y,f(x))=e−yf(x)=n1i=1∑i=me−yif(xi)AdaBoost中损失函数为: ℓ exp ( H ∣ D ) = E x ∼ D [ e − f ( x ) H ( x ) ] \ell_{\exp }(H | \mathcal{D})=\mathbb{E}_{\boldsymbol{x} \sim \mathcal{D}}\left[e^{-f(\boldsymbol{x}) H(\boldsymbol{x})}\right] ℓexp(H∣D)=Ex∼D[e−f(x)H(x)]
L ( y , f ( x ) ) = max ( 0 , w ( y ) ) \mathrm{L}(\mathrm{y}, \mathrm{f}(\mathrm{x}))=\max (0, w(y)) L(y,f(x))=max(0,w(y)) f ( x ) f(x) f(x)如果与 y y y一致,则损失函数为0,不一致则损失函数为 w ( y ) w(y) w(y)
SVM中损失函数即为Hinge损失函数: ℓ hinge ( z ) = max ( 0 , 1 − z ) \ell_{\text {hinge}}(z)=\max (0,1-z) ℓhinge(z)=max(0,1−z) min w , b 1 2 ∥ w ∥ 2 + C ∑ i = 1 m max ( 0 , 1 − y i ( w T x i + b ) ) \min _{w, b} \frac{1}{2}\|\boldsymbol{w}\|^{2}+C \sum_{i=1}^{m} \max \left(0,1-y_{i}\left(\boldsymbol{w}^{\mathrm{T}} \boldsymbol{x}_{i}+b\right)\right) w,bmin21∥w∥2+Ci=1∑mmax(0,1−yi(wTxi+b))进而变形为: min w , b , ξ 1 2 ∥ w ∥ 2 + C ∑ i = 1 m ξ i s . t . y i ( w T x i + b ) ≥ 1 − ξ i ξ i ≥ 0 , i = 1 , 2 , … , m \begin{aligned}\min _{w, b,\xi} \frac{1}{2}\|w\|^2+C\sum_{i=1}^m\xi_i\\s.t.\quad y_i(w^Tx_i+b)\geq1-\xi_i\\\xi_i\geq0,i=1,2,\dots,m\end{aligned} w,b,ξmin21∥w∥2+Ci=1∑mξis.t.yi(wTxi+b)≥1−ξiξi≥0,i=1,2,…,m
L ( Y , f ( X ) ) = { 1 Y ! = f ( X ) 0 Y = f ( X ) } \mathrm{L}(\mathrm{Y}, \mathrm{f}(\mathrm{X}))=\left\{\begin{array}{ll}{1} & {Y !=f(X)} \\ {0} & {Y=f(X)}\end{array}\right\} L(Y,f(X))={10Y!=f(X)Y=f(X)}
L ( Y , f ( X ) ) = ∣ Y − f ( X ) ∣ \mathrm{L}(\mathrm{Y}, \mathrm{f}(\mathrm{X}))=|Y-f(X)| L(Y,f(X))=∣Y−f(X)∣
各种损失函数:
转载自:https://blog.csdn.net/u014106644/article/details/83660226