%% ============ Part : Compute Cost and Gradient ============
% In this part of the exercise, you will implement the cost and gradient
% for logistic regression. You neeed to complete the code in
% costFunction.m % Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X); % Add intercept term to x and X_test
X = [ones(m, ) X]; % Initialize fitting parameters
initial_theta = zeros(n + , ); % Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);

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

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

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

w.r.t 什么意思?

with regard to
with reference to 关于的意思

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

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

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

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

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

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

%% ============= Part : Optimizing using fminunc  =============
% In this exercise, you will use a built-in function (fminunc) to find the
% optimal parameters theta. % Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', ); % Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
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)的第一个参数里面进行计算。

function plotDecisionBoundary(theta, X, y)
%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with
%the decision boundary defined by theta
% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the
% positive examples and o for the negative examples. X is assumed to be
% a either
% ) Mx3 matrix, where the first column is an all-ones column for the
% intercept.
% ) MxN, N> matrix, where the first column is all-ones % Plot Data
plotData(X(:,:), y);
hold on if size(X, ) <=
% Only need points to define a line, so choose two endpoints
plot_x = [min(X(:,))-, max(X(:,))+]; % Calculate the decision boundary line//令theta装置乘以X等于0,即可 plot_y = (-./theta()).*(theta().*plot_x + theta()); % Plot, and adjust axes for better viewing
plot(plot_x, plot_y) % Legend, specific for the exercise
legend('Admitted', 'Not admitted', 'Decision Boundary')
axis([, , , ])
else
% Here is the grid range
u = linspace(-, 1.5, );
v = linspace(-, 1.5, ); z = zeros(length(u), length(v));
% Evaluate z = theta*x over the grid
for i = :length(u)
for j = :length(v)
z(i,j) = mapFeature(u(i), v(j))*theta;
end
end
z = z'; % important to transpose z before calling contour % Plot z =
% Notice you need to specify the range [, ]
contour(u, v, z, [, ], 'LineWidth', )
end
hold off 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));

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

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

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

%% ============= Part : Regularization and Accuracies =============
% Optional Exercise:
% In this part, you will get to try different values of lambda and
% see how regularization affects the decision coundart
%
% Try the following values of lambda (, , , ).
%
% How does the decision boundary change when you vary lambda? How does
% the training set accuracy vary?
% % Initialize fitting parameters
initial_theta = zeros(size(X, ), ); % Set regularization parameter lambda to (you should vary this)
lambda = ; % Set Options
options = optimset('GradObj', 'on', 'MaxIter', ); % Optimize
[theta, J, exit_flag] = ...
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. Code Signal_练习题_Array Replace

    Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem. Exam ...

  2. MUI框架 选择器的使用

    js.css引用 <script type="text/javascript" src="librarys/mui/js/mui.min.js">& ...

  3. Spring 、SpringMVC 、Struts2之间的区别

    一.Spring与SpringMVC的区别: spring是一个开源框架,是为了解决企业应用程序开发,功能如下: 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能 范围:任何Ja ...

  4. com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/NOTICE

    在将vivo eclipse sdk 迁移 android studio  时候报错 Error:Execution failed for task ':vivosdk:transformResour ...

  5. Android:Gradle sync failed: Another 'refresh project' task is currently running for the project

    android studio 克隆项目后,重新导入后显示Gradle sync failed: Another 'refresh project' task is currently running ...

  6. 回归JavaScript基础(七)

    主题:引用类型Function的介绍. 今天首先说的就是Function类型.下面就是定义函数的两种方法,第一种使用函数声明语法定义,第二种使用函数表达式定义.这两种定义函数的方式几乎没有什么区别. ...

  7. unity3d中的自定义模型的顶点法线和建模软件中的术语“软硬边”和立方体

    在unity3d中我是想用Mesh生成一个正方体,直到遇到了法线的问题. 我是想显示如下图所示的正方体,却发现法线设置上的问题. 这里我先使用了8个顶点 按照每个顶点一个法线的结果,只能是这样:(也就 ...

  8. Oracle EBS AR 更新客户配置文件

    DECLARE l_rec_profile_t hz_customer_profile_v2pub.customer_profile_rec_type; l_rec_profile hz_custom ...

  9. Jmeter入门--脚本录制

    一.Badboy脚本录制(推荐) 下载地址:http://www.badboy.com.au/download/add,下载完成后直接安装即可. Badboy是一个强大的工具,旨在帮助测试和开发复杂的 ...

  10. [SQL Server]数据库的恢复

    数据库恢复是和数据库备份相对应的操作,它是将数据库备份重新加载到系统中的过程.数据库恢复可以创建备份完成时数据库中存在的相关文件,但是备份以后的所有数据库修改都将丢失. SQL Server进行数据库 ...