【Machine Learning】逻辑回归 Logistic Regression

来源:互联网 发布:jquery.uploadify.js 编辑:程序博客网 时间:2024/05/01 04:39
【公式】

s形函数:


【Logistic regression cost function】
重申:cost function表示预测值和实际值的平方差
hθ(x)之前已经定义,由于y只能等于1或者0,则J(θ) 可变型如下

那What should do Next is find out minθJ(θ)的 θ 值了
如前几章所讲,由求导求出最小的值

【求导步骤】
先贴一张链式法则求三层以上的复合函数的导数:

然后J(θ)可以变形成

带入公式,再求导可以得出:

设 则有:

将g1带入公式

将k g(θ)带入
【Useful Function】
matlab中已经帮我们做好了优化 θ 的辅助函数,我们只要写好cost function并告诉系统就可以了

jVal 是 cost function 的表示,比如设有两个点(1,0,5)和(0,1,5)进行回归,那么就设方程为hθ(x)=θ1x1+θ2x2;
则有costfunction J(θ): jVal=(theta(1)-5)^2+(theta(2)-5)^2;

在每次迭代中,按照gradient descent的方法更新参数θ:θ(i)-=gradient(i),其中gradient(i)是J(θ)对θi求导的函数式,在此例中就有gradient(1)=2*(theta(1)-5), gradient(2)=2*(theta(2)-5)。如下面代码所示:

 【Run】

  1.  options = optimset('GradObj','on','MaxIter',100);  
  2.  initialTheta = zeros(2,1)  
  3.  [optTheta,functionVal,exitFlag] = fminunc(@costFunction,initialTheta,options);  
‘GradObj’, Use the GradObj option to specify that FUN also returns a second output argument G that is the partial derivatives of the function df/dX, at the point X.
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initialtheta, options);
@(t) ( costFunction(t, X, y) ) . This creates a function, with argument t, which calls your costFunction.
 
来源: <http://blog.csdn.net/abcjennifer/article/details/7716281>
来源: <http://feature-space.com/en/post24.html>
原创粉丝点击