1. %% ============ Part : Compute Cost and Gradient ============
  2. % In this part of the exercise, you will implement the cost and gradient
  3. % for logistic regression. You neeed to complete the code in
  4. % costFunction.m
  5.  
  6. % Setup the data matrix appropriately, and add ones for the intercept term
  7. [m, n] = size(X);
  8.  
  9. % Add intercept term to x and X_test
  10. X = [ones(m, ) X];
  11.  
  12. % Initialize fitting parameters
  13. initial_theta = zeros(n + , );
  14.  
  15. % Compute and display initial cost and gradient
  16. [cost, grad] = costFunction(initial_theta, X, y);

难点1:X和theta的维度变化,怎么变得,为什么?

X加了一列1,θ加了一行θ0,因为最后边界是θ01X12X2,要符合矩阵运算

难点2:costFunction中grad是什么函数,有什么作用?

w.r.t 什么意思?

  1. with regard to
    with reference to
  2.  
  3. 关于的意思

难点3:linear regression的代价函数和logistic regression的代价函数,为什么不一样?

和幂次有关,如果还用原来的代价函数,那么会有很多局部最值,没有办法梯度下降到最小值。

另一个解读角度:从最大似然函数出发考虑。

下面的文章写得很好,参考链接:

    http://blog.csdn.net/lu597203933/article/details/38468303

http://blog.csdn.net/it_vitamin/article/details/45625143

  1. %% ============= Part : Optimizing using fminunc =============
  2. % In this exercise, you will use a built-in function (fminunc) to find the
  3. % optimal parameters theta.
  4.  
  5. % Set options for fminunc
  6. options = optimset('GradObj', 'on', 'MaxIter', );
  7.  
  8. % Run fminunc to obtain the optimal theta
  9. % This function will return theta and the cost
  10. [theta, cost] = ...
  11. fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

难点4:optimset是什么作用?

options = optimset('GradObj', 'on', 'MaxIter', 400); //

通过optimset函数设置或改变这些参数。其中有的参数适用于所有的优化算法,有的则只适用于大型优化问题,另外一些则只适用于中型问题。

LargeScale – 当设为'on'时使用大型算法,若设为'off'则使用中型问题的算法。
      适用于大型和中型算法的参数:
Diagnostics – 打印最小化函数的诊断信息。
Display – 显示水平。选择'off',不显示输出;选择'iter',显示每一步迭代过程的输出;选择'final',显示最终结果。打印最小化函数的诊断信息。
GradObj – 用户定义的目标函数的梯度。对于大型问题此参数是必选的,对于中型问题则是可选项。
MaxFunEvals – 函数评价的最大次数。
MaxIter – 最大允许迭代次数。
TolFun – 函数值的终止容限。
TolX – x处的终止容限。

options = optimset('param1',value1,'param2',value2,...) %设置所有参数及其值,未设置的为默认值

options = optimset('GradObj', 'on', 'MaxIter', 400);

在使用options的函数中,参数是由用户自行定义的,最大递归次数为400

难点5:[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options)是怎么实现功能?

关于句柄@,参考偏文章:http://blog.csdn.net/gzp444280620/article/details/49252491

关于fiminuc,参考这篇文章:http://blog.csdn.net/gzp444280620/article/details/49272977

fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);    %是为了当代码里面有这样的函数的时候,fminunc(1,θ,option)第一个参数,

会传递给costFunction(t, X, y)的第一个参数里面进行计算。

  1. function plotDecisionBoundary(theta, X, y)
  2. %PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with
  3. %the decision boundary defined by theta
  4. % PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the
  5. % positive examples and o for the negative examples. X is assumed to be
  6. % a either
  7. % ) Mx3 matrix, where the first column is an all-ones column for the
  8. % intercept.
  9. % ) MxN, N> matrix, where the first column is all-ones
  10.  
  11. % Plot Data
  12. plotData(X(:,:), y);
  13. hold on
  14.  
  15. if size(X, ) <=
  16. % Only need points to define a line, so choose two endpoints
  17. plot_x = [min(X(:,))-, max(X(:,))+];
  18.  
  19. % Calculate the decision boundary line//令theta装置乘以X等于0,即可
  20.  
  21. plot_y = (-./theta()).*(theta().*plot_x + theta());
  22.  
  23. % Plot, and adjust axes for better viewing
  24. plot(plot_x, plot_y)
  25.  
  26. % Legend, specific for the exercise
  27. legend('Admitted', 'Not admitted', 'Decision Boundary')
  28. axis([, , , ])
  29. else
  30. % Here is the grid range
  31. u = linspace(-, 1.5, );
  32. v = linspace(-, 1.5, );
  33.  
  34. z = zeros(length(u), length(v));
  35. % Evaluate z = theta*x over the grid
  36. for i = :length(u)
  37. for j = :length(v)
  38. z(i,j) = mapFeature(u(i), v(j))*theta;
  39. end
  40. end
  41. z = z'; % important to transpose z before calling contour
  42.  
  43. % Plot z =
  44. % Notice you need to specify the range [, ]
  45. contour(u, v, z, [, ], 'LineWidth', )
  46. end
  47. hold off
  48.  
  49. end

难点6:这个plotDecisionBoundary函数是怎么画出边界的?

plotDecisionBoundary中的下面的两行看不懂:

plot_x = [min(X(:,2))-2,  max(X(:,2))+2];  //直线的参数其实已经得到,选划线的范围,把直线画出来即可。为了美观,扩大了两个单位

% Calculate the decision boundary line//令theta装置乘以X等于0,即可 plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));

plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));

  1. function plotData(X, y)  //会按照真类和假类分类 画出输入的点
  2. %PLOTDATA Plots the data points X and y into a new figure
  3. % PLOTDATA(x,y) plots the data points with + for the positive examples
  4. % and o for the negative examples. X is assumed to be a Mx2 matrix.
  5.  
  6. % Create New Figure
  7. figure; hold on;
  8.  
  9. % ====================== YOUR CODE HERE ======================
  10. % Instructions: Plot the positive and negative examples on a
  11. % 2D plot, using the option 'k+' for the positive
  12. % examples and 'ko' for the negative examples.
  13. %
  14. % Find Indices of Positive and Negative Examples //返回 y=1和y=0的位置,也就是行数
  15. pos = find(y==); neg = find(y == );   //利用find的查找功能,把正类和负类分开,并把横纵坐标保存在pos里面
  16. % Plot Examples
  17. plot(X(pos, ), X(pos, ), 'k+','LineWidth', , ...
  18. 'MarkerSize', );
  19. plot(X(neg, ), X(neg, ), 'ko', 'MarkerFaceColor', 'y', ...
  20. 'MarkerSize', );
  21.  
  22. % =========================================================================
  23.  
  24. hold off;
  25.  
  26. end
  1. %% =========== Part : Regularized Logistic Regression ============
  2. % In this part, you are given a dataset with data points that are not
  3. % linearly separable. However, you would still like to use logistic
  4. % regression to classify the data points.
  5. %
  6. % To do so, you introduce more features to use -- in particular, you add
  7. % polynomial features to our data matrix (similar to polynomial
  8. % regression).
  9. %
  10.  
  11. % Add Polynomial Features
  12.  
  13. % Note that mapFeature also adds a column of ones for us, so the intercept
  14. % term is handled
  15. X = mapFeature(X(:,), X(:,));
  16.  
  17. % Initialize fitting parameters
  18. initial_theta = zeros(size(X, ), );
  19.  
  20. % Set regularization parameter lambda to
  21. lambda = ;
  22.  
  23. % Compute and display initial cost and gradient for regularized logistic
  24. % regression
  25. [cost, grad] = costFunctionReg(initial_theta, X, y, lambda);

难点7:mapFeature是怎么工作的,原理?

难点8:[cost, grad] = costFunctionReg(initial_theta, X, y, lambda);怎么工作?

  1. %% ============= Part : Regularization and Accuracies =============
  2. % Optional Exercise:
  3. % In this part, you will get to try different values of lambda and
  4. % see how regularization affects the decision coundart
  5. %
  6. % Try the following values of lambda (, , , ).
  7. %
  8. % How does the decision boundary change when you vary lambda? How does
  9. % the training set accuracy vary?
  10. %
  11.  
  12. % Initialize fitting parameters
  13. initial_theta = zeros(size(X, ), );
  14.  
  15. % Set regularization parameter lambda to (you should vary this)
  16. lambda = ;
  17.  
  18. % Set Options
  19. options = optimset('GradObj', 'on', 'MaxIter', );
  20.  
  21. % Optimize
  22. [theta, J, exit_flag] = ...
  23. fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);

难点9:为什么fminunc可以返回三个变量?

ps:先把问题记录一下,稍后会一个个解决。

week3编程作业: Logistic Regression中一些难点的解读的更多相关文章

  1. Andrew Ng机器学习编程作业:Logistic Regression

    编程作业文件: machine-learning-ex2 1. Logistic Regression (逻辑回归) 有之前学生的数据,建立逻辑回归模型预测,根据两次考试结果预测一个学生是否有资格被大 ...

  2. logistic regression中的cost function选择

    一般的线性回归使用的cost function为: 但由于logistic function: 本身非凸函数(convex function), 如果直接使用线性回归的cost function的话, ...

  3. Andrew Ng机器学习编程作业: Linear Regression

    编程作业有两个文件 1.machine-learning-live-scripts(此为脚本文件方便作业) 2.machine-learning-ex1(此为作业文件) 将这两个文件解压拖入matla ...

  4. Logistic regression中regularization失败的解决方法探索(文末附解决后code)

    在matlab中做Regularized logistic regression 原理: 我的代码: function [J, grad] = costFunctionReg(theta, X, y, ...

  5. Coursera machine learning 第二周 编程作业 Linear Regression

    必做: [*] warmUpExercise.m - Simple example function in Octave/MATLAB[*] plotData.m - Function to disp ...

  6. Andrew Ng 的 Machine Learning 课程学习 (week3) Logistic Regression

    这学期一直在跟进 Coursera上的 Machina Learning 公开课, 老师Andrew Ng是coursera的创始人之一,Machine Learning方面的大牛.这门课程对想要了解 ...

  7. 逻辑回归 logistic regression(1)逻辑回归的求解和概率解释

    本系列内容大部分来自Standford公开课machine learning中Andrew老师的讲解,附加自己的一些理解,编程实现和学习笔记. 第一章 Logistic regression 1.逻辑 ...

  8. Matlab实现线性回归和逻辑回归: Linear Regression & Logistic Regression

    原文:http://blog.csdn.net/abcjennifer/article/details/7732417 本文为Maching Learning 栏目补充内容,为上几章中所提到单参数线性 ...

  9. Probabilistic SVM 与 Kernel Logistic Regression(KLR)

    本篇讲的是SVM与logistic regression的关系. (一) SVM算法概论 首先我们从头梳理一下SVM(一般情况下,SVM指的是soft-margin SVM)这个算法. 这个算法要实现 ...

随机推荐

  1. html-使用表单标签实现注册页面

    案例说明: - 使用表格实现页面效果 - 超链接不想要有效果,使用href="#" - 如果表格里面的单元格没有内容,使用空格作为占位符   - 使用图片标签提交表单 <in ...

  2. thinkPHP的几个系统常量

    echo "当前请求:" . __SELF__ . '<br>'; echo "当前分组:" . __MODULE__ . '<br>' ...

  3. 用Struts2实现列表显示和分页功能

    引用自http://www.2cto.com/kf/201309/243730.html BlogDAO.java文件 /** 根据条件(默认一张表所有数据)返回多条记录 */ public List ...

  4. hadoop HA集群搭建步骤

      NameNode DataNode Zookeeper ZKFC JournalNode ResourceManager NodeManager node1 √   √ √   √   node2 ...

  5. 线程间的通信方式3--Handler

    Android的消息处理有三个核心类:Looper,Handler和Message.其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道,因 ...

  6. sql 内连接 子查询 合并查询

    -- 内连接:-- 显示员工姓名.工资和公司所在地 select e.ename, e.sal, d.dname from emp e,dept d; -- 笛卡尔积 select e.ename, ...

  7. Hive常用配置

    1.配置hive在HDFS上的根目录位置 <property> <name>hive.metastore.warehouse.dir</name> <valu ...

  8. java基础(二) 自增自减与贪心规则

    引言   JDK中提供了自增运算符++,自减运算符--.这两个操作符各有两种使用方式:前缀式(++ a,--a),后缀式(a++,a--).可能说到这里,说不得有读者就会吐槽说,前后缀式都挺简单的,前 ...

  9. 使用 Azure CLI 创建 Linux 虚拟机

    Azure CLI 用于从命令行或脚本创建和管理 Azure 资源. 本指南详细介绍了如何使用 Azure CLI 部署运行 Ubuntu 服务器的虚拟机. 服务器部署以后,将创建 SSH 连接,并且 ...

  10. REST Framework 的用户认证组件

    用户认证流程: 我们要知道这个流程是怎么走的? 认证之后做的什么? 怎么认证?这三个条件 认证流程:就是使用BaseAuthentication这个模块来做认证,判断你登陆成功传递过来的随机字符串是否 ...