机器学习(4)——logistic regression
2015-07-24 16:49
459 查看
【一、逻辑回归模型】
逻辑回归不同于线性回归,它实际上一种分类方法,用于二分类问题(y=0或者1)。逻辑回归模型如下:即当
>=0.5时,预测输出值y=1;否则预测输出值y=0;且有:
【二、决策边界】
所谓Decision Boundary就是能够将所有数据点进行很好地分类的h(x)边界。【例1】
由图可知:
对应的线性回归模型为:
决策边界为粉色直线:
【例2】
由图可知:
对应的线性回归模型为:
决策边界为粉色曲线:
【三、代价函数】
代价函数 J(θ)的推导如下:
对于每个样本:
于是似然函数为:
则对数似然函数为:
那么代价函数J(θ)取为
,即
因为最大似然估计是求使l(θ)最大的θ,那么这个θ也是使代价函数J(θ)最小的θ。
【四、梯度下降法求最佳theta】
其中α为学习率。
偏导的推导过程如下:
将偏导代入:
【五、利用matlab自带优化函数求最佳theta】
除了gradient descent 方法之外,我们还有很多方法可以使用,如下图所示,左边是另外三种方法,右边是这三种方法共同的优缺点,无需选择学习率α,更快,但是更复杂。matlab中已经帮我们实现好了一些优化参数θ的方法,那么这里我们需要完成的事情只是写好cost function,并告诉系统,要用哪个方法进行最优化参数。比如我们用‘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
伪代码:
cost function具体实现如下:
function [J, grad] = costFunction(theta, X, y) m = length(y); % number of training examples J = 0; grad = zeros(size(theta)); J=-sum(y.*(log(sigmoid(X*theta)))+(1.0-y).*(log(1.0-sigmoid(X*theta))))/m; for j=1:size(theta) grad(j)=sum((sigmoid(X*theta)-y).*X(:,j))/m; end ============================================================= end
% Set options for fminunc options = optimset('GradObj', 'on', 'MaxIter', 400); % Run fminunc to obtain the optimal theta [theta, cost]=fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
【六、使用逻辑回归解多类问题】
即对于一个输入样本x,获得最大hθ(x)的类就是x所分到的类。
对于每一个类,训练得到相对应的一组theta
function [all_theta] = oneVsAll(X, y, num_labels, lambda) %ONEVSALL trains multiple logistic regression classifiers and returns all %the classifiers in a matrix all_theta, where the i-th row of all_theta %corresponds to the classifier for label i m = size(X, 1); n = size(X, 2); all_theta = zeros(num_labels, n + 1); % Add ones to the X data matrix X = [ones(m, 1) X]; % ====================== YOUR CODE HERE ====================== initial_theta = zeros(n + 1, 1); options = optimset('GradObj', 'on', 'MaxIter', 50); for i=1:num_labels theta=fmincg (@(t)(lrCostFunction(t, X, (y == i), lambda)), initial_theta, options); all_theta (i,:)=theta'; end % ========================================================================= end
那么对于未知样本x,分别计算每一类的相应输出,最大输出的哪一类即为x所属的类
function p = predictOneVsAll(all_theta, X) %PREDICT Predict the label for a trained one-vs-all classifier. The labels %are in the range 1..K, where K = size(all_theta, 1). m = size(X, 1); num_labels = size(all_theta, 1); % You need to return the following variables correctly p = zeros(size(X, 1), 1); % Add ones to the X data matrix X = [ones(m, 1) X]; % ====================== YOUR CODE HERE ====================== % Instructions: Complete the following code to make predictions using % your learned logistic regression parameters (one-vs-all). % You should set p to a vector of predictions (from 1 to % num_labels). % all_predict=sigmoid(all_theta*X'); [C,I]=max(all_predict); p=I'; % ========================================================================= end
相关文章推荐
- localhost与127.0.0.1之间的区别
- could not perform addBatch
- mysql主从配置
- windows上Apache+tomcat实现主备机服务器
- HOW TO INSTALL PHANTOMJS ON A CENTOS SERVER
- Android常用的设计模式
- 多线程问题
- 判断是否开启推送
- T-code:SE01,SE93,SE43 经常使用的几个事务码
- ZigZag Conversion
- 关于 - headerViewForSection returns nil问题
- 性能优化(一个)Hibernate 使用缓存(一个、两、查询)提高系统性能
- iOS Human Interface Guidelines(原创翻译) 目录
- mac 上面安装mysql-python
- 关于 - headerViewForSection returns nil问题
- 戏说javascript原型(prototype)实现面向对象
- Android 打包Jar(发布SDK)常见问题及解决方案
- IEnumerable、IEnumerator
- 如何使用CodeSmith批量生成代码(原创系列教程)
- 从一个肉松包说开去