逻辑回归

Logistic Regression

1 分类 Classification

首先我们来看看使用线性回归来解决分类会出现的问题。下图中,我们加入了一个训练集,产生的新的假设函数使得我们进行分类出现了错误;而且线性回归计算的结果往往会远小于0或者远大于1,这对于0,1分类变得很奇怪。可见线性回归并不适用与分类。下面介绍的逻辑回归的结果总是在[0,1],适用于分类,其实逻辑回归是一种分类算法。

2 假设函数Hypothesis Representation

逻辑回归假设函数为:

其中 是参数向量,特征向量。该函数叫做Logistic function(也叫做Sigmoid function)。函数图像如下,范围(0,1)

对于分类问题,它可以表示给定特征,参数属于类别1的概率,那么属于另一类的概率自然也就是

3 决策边界Decision Boundary

根据逻辑函数的特性可以看出,当,当

我们把 就称为决策边界(Decision Boundary)。

我们可以通过多项式组合来制定更加复杂的决策边界。

4 代价函数 Cost Function

代价函数 ,在线性回归中,我们的,如果在逻辑回归中也使用这个函数,那么代价函数会是一个非凸函数,无法使用梯度下降去求解参数,所以我们要寻找一些函数使得代价函数为凸函数。

我们来看一下这个代价函数:

我们可以将它合并为一个公式:

5 梯度下降 Gradient Desecent

这里我们把代价函数写成这种形式是由最大似然估计得到了,当然也还有其他的形式。

梯度下降算法:

可以看到这里推导出来的公式看起来和线性回归梯度下降中推导出来的公式是一样的,但是要注意已经是sigmod函数而不是线性公式了,所以他们是两码事。

6 多分类 Multi-class classification: one-vs-all

我们可以降逻辑回归用于多分类问题,假设有K类,我们可以训练出K个分类器,每个分类器,将其中一种类别作为正类,其余的都是负类来训练,然后再预测时,类别属于概率最大的那个类。

7 正则化regularization

7.1 过拟合 overfitting

7.2 代价函数cost function

在代价函数中加入正则项,注意,这里对计算,而不是从开始。其中是正则项参数,如果太大,那么 会趋向于0,使得 ,导致欠拟合。

7.3 正则化线性回归 Regularized linear regression

代价函数:

Gradeint descent

Repeat{

}

这里是一个比1小一点点的数。

在线性回归中,我们除了梯度下降,还有正规方程的方法,正规方法加入正则项后:

7.4 正则化逻辑回归Regularized logistic regression

代价函数:

Gradient descent

Repeat{

}

实验代码

正则化逻辑回归

  1. function [J, grad] = costFunctionReg(theta, X, y, lambda)
  2. %COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
  3. % J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
  4. % theta as the parameter for regularized logistic regression and the
  5. % gradient of the cost w.r.t. to the parameters.
  6.  
  7. % Initialize some useful values
  8. m = length(y); % number of training examples
  9.  
  10. % You need to return the following variables correctly
  11. J = ;
  12. grad = zeros(size(theta));
  13.  
  14. % ====================== YOUR CODE HERE ======================
  15. % Instructions: Compute the cost of a particular choice of theta.
  16. % You should set J to the cost.
  17. % Compute the partial derivatives and set grad to the partial
  18. % derivatives of the cost w.r.t. each parameter in theta
  19. hx=sigmoid(X*theta);
  20. Jnorm=(-/m)*(y'*log(hx)+(1-y)'*log(-hx));
  21. theta0=theta(); %注意theta0不用正则化
  22. theta1=theta(:end);
  23. Jreg=(lambda/(*m))*sum(theta1.^);
  24. J=Jnorm+Jreg;
  25.  
  26. grad0=(hx-y)'*X(:,1)./m;
  27. grad1=((hx-y)'*X(:,2:end)./m)'+(lambda/m).*theta1;
  28. grad=[grad0;grad1];
  29. % =============================================================
  30.  
  31. end

sigmoid函数

  1. function g = sigmoid(z)
  2. %SIGMOID Compute sigmoid functoon
  3. % J = SIGMOID(z) computes the sigmoid of z.
  4.  
  5. % You need to return the following variables correctly
  6. g = zeros(size(z));
  7.  
  8. % ====================== YOUR CODE HERE ======================
  9. % Instructions: Compute the sigmoid of each value of z (z can be a matrix,
  10. % vector or scalar).
  11. g=./(+exp((-).*z));
  12. % =============================================================
  13. end

特征

  1. function out = mapFeature(X1, X2)
  2. % MAPFEATURE Feature mapping function to polynomial features
  3. %
  4. % MAPFEATURE(X1, X2) maps the two input features
  5. % to quadratic features used in the regularization exercise.
  6. %
  7. % Returns a new feature array with more features, comprising of
  8. % X1, X2, X1.^, X2.^, X1*X2, X1*X2.^, etc..
  9. %
  10. % Inputs X1, X2 must be the same size
  11. %
  12.  
  13. degree = ;%6次函数
  14. out = ones(size(X1(:,)));
  15. for i = :degree
  16. for j = :i
  17. out(:, end+) = (X1.^(i-j)).*(X2.^j);
  18. end
  19. end
  20.  
  21. end

主函数

  1. %% Machine Learning Online Class - Exercise : Logistic Regression
  2. %
  3. % Instructions
  4. % ------------
  5. %
  6. % This file contains code that helps you get started on the second part
  7. % of the exercise which covers regularization with logistic regression.
  8. %
  9. % You will need to complete the following functions in this exericse:
  10. %
  11. % sigmoid.m
  12. % costFunction.m
  13. % predict.m
  14. % costFunctionReg.m
  15. %
  16. % For this exercise, you will not need to change any code in this file,
  17. % or any other files other than those mentioned above.
  18. %
  19.  
  20. %% Initialization
  21. clear ; close all; clc
  22.  
  23. %% Load Data
  24. % The first two columns contains the X values and the third column
  25. % contains the label (y).
  26.  
  27. data = load('ex2data2.txt');
  28. X = data(:, [, ]); y = data(:, );
  29.  
  30. plotData(X, y);
  31.  
  32. % Put some labels
  33. hold on;
  34.  
  35. % Labels and Legend
  36. xlabel('Microchip Test 1')
  37. ylabel('Microchip Test 2')
  38.  
  39. % Specified in plot order
  40. legend('y = 1', 'y = 0')
  41. hold off;
  42.  
  43. %% =========== Part : Regularized Logistic Regression ============
  44. % In this part, you are given a dataset with data points that are not
  45. % linearly separable. However, you would still like to use logistic
  46. % regression to classify the data points.
  47. %
  48. % To do so, you introduce more features to use -- in particular, you add
  49. % polynomial features to our data matrix (similar to polynomial
  50. % regression).
  51. %
  52.  
  53. % Add Polynomial Features
  54.  
  55. % Note that mapFeature also adds a column of ones for us, so the intercept
  56. % term is handled
  57. X = mapFeature(X(:,), X(:,));
  58.  
  59. % Initialize fitting parameters
  60. initial_theta = zeros(size(X, ), );
  61.  
  62. % Set regularization parameter lambda to
  63. lambda = ;
  64.  
  65. % Compute and display initial cost and gradient for regularized logistic
  66. % regression
  67. [cost, grad] = costFunctionReg(initial_theta, X, y, lambda);
  68.  
  69. fprintf('Cost at initial theta (zeros): %f\n', cost);
  70.  
  71. fprintf('\nProgram paused. Press enter to continue.\n');
  72. pause;
  73.  
  74. %% ============= Part : Regularization and Accuracies =============
  75. % Optional Exercise:
  76. % In this part, you will get to try different values of lambda and
  77. % see how regularization affects the decision coundart
  78. %
  79. % Try the following values of lambda (, , , ).
  80. %
  81. % How does the decision boundary change when you vary lambda? How does
  82. % the training set accuracy vary?
  83. %
  84.  
  85. % Initialize fitting parameters
  86. initial_theta = zeros(size(X, ), );
  87.  
  88. % Set regularization parameter lambda to (you should vary this)
  89. lambda = ;
  90.  
  91. % Set Options
  92. options = optimset('GradObj', 'on', 'MaxIter', );
  93.  
  94. % Optimize
  95. [theta, J, exit_flag] = ...
  96. fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);
  97.  
  98. % Plot Boundary
  99. plotDecisionBoundary(theta, X, y);
  100. hold on;
  101. title(sprintf('lambda = %g', lambda))
  102.  
  103. % Labels and Legend
  104. xlabel('Microchip Test 1')
  105. ylabel('Microchip Test 2')
  106.  
  107. legend('y = 1', 'y = 0', 'Decision boundary')
  108. hold off;
  109.  
  110. % Compute accuracy on our training set
  111. p = predict(theta, X);
  112.  
  113. fprintf('Train Accuracy: %f\n', mean(double(p == y)) * );

实验中参数lamda的大小也十分重要,不同的lamda可能会过拟合或者欠拟合。

ML 逻辑回归 Logistic Regression的更多相关文章

  1. Coursera公开课笔记: 斯坦福大学机器学习第六课“逻辑回归(Logistic Regression)” 清晰讲解logistic-good!!!!!!

    原文:http://52opencourse.com/125/coursera%E5%85%AC%E5%BC%80%E8%AF%BE%E7%AC%94%E8%AE%B0-%E6%96%AF%E5%9D ...

  2. 机器学习总结之逻辑回归Logistic Regression

    机器学习总结之逻辑回归Logistic Regression 逻辑回归logistic regression,虽然名字是回归,但是实际上它是处理分类问题的算法.简单的说回归问题和分类问题如下: 回归问 ...

  3. 机器学习(四)--------逻辑回归(Logistic Regression)

    逻辑回归(Logistic Regression) 线性回归用来预测,逻辑回归用来分类. 线性回归是拟合函数,逻辑回归是预测函数 逻辑回归就是分类. 分类问题用线性方程是不行的   线性方程拟合的是连 ...

  4. 机器学习入门11 - 逻辑回归 (Logistic Regression)

    原文链接:https://developers.google.com/machine-learning/crash-course/logistic-regression/ 逻辑回归会生成一个介于 0 ...

  5. 机器学习方法(五):逻辑回归Logistic Regression,Softmax Regression

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 前面介绍过线性回归的基本知识, ...

  6. 机器学习 (三) 逻辑回归 Logistic Regression

    文章内容均来自斯坦福大学的Andrew Ng教授讲解的Machine Learning课程,本文是针对该课程的个人学习笔记,如有疏漏,请以原课程所讲述内容为准.感谢博主Rachel Zhang 的个人 ...

  7. 逻辑回归(Logistic Regression)详解,公式推导及代码实现

    逻辑回归(Logistic Regression) 什么是逻辑回归: 逻辑回归(Logistic Regression)是一种基于概率的模式识别算法,虽然名字中带"回归",但实际上 ...

  8. 逻辑回归 Logistic Regression

    逻辑回归(Logistic Regression)是广义线性回归的一种.逻辑回归是用来做分类任务的常用算法.分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上.比如一个人有没有病,又因为噪声的 ...

  9. 【机器学习】Octave 实现逻辑回归 Logistic Regression

    ex2data1.txt ex2data2.txt 本次算法的背景是,假如你是一个大学的管理者,你需要根据学生之前的成绩(两门科目)来预测该学生是否能进入该大学. 根据题意,我们不难分辨出这是一种二分 ...

随机推荐

  1. Linq与扩展方法

    使用数据集 /// <summary> /// 库房信息类 /// </summary> public class Kfxx { /// <summary> /// ...

  2. Linux 并发服务器雏形总结

    如下介绍一个并发回射客户端/服务器的雏形,所谓回射:就是客户端输入一条数据,服务器端读取并显示,然后服务器端再把刚读取的信息发送回客户端进行显示.示意图如下: 所谓并发服务器:就是一个服务器可以同时为 ...

  3. 【BZOJ1937】[Shoi2004]Mst 最小生成树 KM算法(线性规划)

    [BZOJ1937][Shoi2004]Mst 最小生成树 Description Input 第一行为N.M,其中 表示顶点的数目, 表示边的数目.顶点的编号为1.2.3.…….N-1.N.接下来的 ...

  4. PHP 获得域控内用户的计算机登录名

    一个需求: 在域控范围获得访问用户的计算机名.方法: 1.测试软件环境: XAMPP Control Panel V3.2.1 ,  Apache version 2.4.7 2.Apache 2.2 ...

  5. 小程序 less wxss 混合 Mixins picker样式优化 箭头样式的实现原理

    lessc src/style/picker-arrow_.less src/style/picker-arrow_.wxss 快速入门 | Less.js 中文文档 https://less.boo ...

  6. VSpy之C Code Interface的使用

    Spy3 要运行 CCodeInterface 功能,需要安装运行环境,建议安装 Visual Studio2003,2005,2008,2010 或更新的版本.当然也可以安装 VC express ...

  7. Servlet 运行原理

    一:servlet定义 Servlet是一个Java应用程序,运行在服务器端,用来处理客户端请求并作出响应的程序. 二:简单servlet实例 //导入所需的包 import javax.servle ...

  8. mongodb文档支持的数据类型

    版权声明:转载请标明来源. https://blog.csdn.net/u014285882/article/details/25510377 1. 存储类型 mongodb文档相似于json,但不是 ...

  9. 剑指offer 面试62题

    面试62题: 题目:圆圈中最后剩下的数字 题:0,1,...,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字. 解题思路:约瑟夫环问题,可 ...

  10. try catch 事务不会滚

    在spring机制中,在配置事务后,如果采用try catch 捕获异常后,因为异常已经被捕获,所以事务不会滚,从而产生许多脏数据.解决办法: 1.在catch中抛出异常,(throw new Run ...