画decision boundary(直线)

%% ============= Part 3: 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', 400);  %设置一些选择项,GradObj,on:表示计算过程中需要的计算gradient;MaxIter,400表示最多迭代次数为400

% 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); %调用matlab的自带的函数fminunc, @(t)(costFunction(t, X, y))创建一个function,参数为t,调用前面写的                                                                                      costFunction函数

返回求得最优解后的theta和cost

% Print theta to screen
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('theta: \n');
fprintf(' %f \n', theta);

% Plot Boundary
plotDecisionBoundary(theta, X, y);   %调用plotDecisionBoundary函数

% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')

% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

plotDecisionBoundary.m

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
% 1) Mx3 matrix, where the first column is an all-ones column for the
% intercept.
% 2) MxN, N>3 matrix, where the first column is all-ones

% Plot Data
plotData(X(:,2:3), y);        %调用前面写的plotData函数,参见plotData.m
hold on

if size(X, 2) <= 3               %size(X,2)表示X的列数,包括X最前面的一列1(an all-ones column for the intercept

      % Only need 2 points to define a line, so choose two endpoints % decision boundary为一条直线,画直线只需要两点就可以
     plot_x = [min(X(:,2))-2, max(X(:,2))+2];

% Calculate the decision boundary line
     plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));  %求plot_y(x2)   矩阵和标量相加减(theta(2).*plot_x + theta(1))实质是矩阵每个元素与该标量相加减

% Plot, and adjust axes for better viewing
    plot(plot_x, plot_y)           %调用系统的plot函数,plot_x,plot_y均为1*2矩阵

% Legend, specific for the exercise
    legend('Admitted', 'Not admitted', 'Decision Boundary')  
    axis([30, 100, 30, 100])            %设置X轴与Y轴的范围

else                                       %decision boundary不是一条直线(如为一个圆)时, size(X, 2) > 3 
    % Here is the grid range
    u = linspace(-1, 1.5, 50);        %linearly spaced vector.在-1到1.5之间产生50个间距相等的点(包括-1与1.5这两个点),u为行向量.
    v = linspace(-1, 1.5, 50);

z = zeros(length(u), length(v));
    % Evaluate z = theta*x over the grid
    for i = 1:length(u)
        for j = 1:length(v)
              z(i,j) = mapFeature(u(i), v(j))*theta;
        end
     end
    z = z'; % important to transpose z before calling contour

% Plot z = 0
    % Notice you need to specify the range [0, 0]
    contour(u, v, z, [0, 0], 'LineWidth', 2)
end
hold off

end

mapFeature.m

function out = mapFeature(X1, X2)
% MAPFEATURE Feature mapping function to polynomial features
%
% MAPFEATURE(X1, X2) maps the two input features
% to quadratic features used in the regularization exercise.
%
% Returns a new feature array with more features, comprising of
% X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc..
%
% Inputs X1, X2 must be the same size
%

degree = 6;
out = ones(size(X1(:,1)));
for i = 1:degree
     for j = 0:i
         out(:, end+1) = (X1.^(i-j)).*(X2.^j);
     end
end

end

matlab(4) Logistic regression:求θ的值使用fminunc / 画decision boundary(直线)plotDecisionBoundary的更多相关文章

  1. matlab(3) Logistic Regression: 求cost 和gradient \ 求sigmoid的值

    sigmoid.m文件 function g = sigmoid(z)%SIGMOID Compute sigmoid functoon% J = SIGMOID(z) computes the si ...

  2. matlab(2) Logistic Regression: 画出样本数据点plotData

    画出data数据 data数据 34.62365962451697,78.0246928153624,030.28671076822607,43.89499752400101,035.84740876 ...

  3. 机器学习-- Logistic回归 Logistic Regression

    转载自:http://blog.csdn.net/linuxcumt/article/details/8572746 1.假设随Tumor Size变化,预测病人的肿瘤是恶性(malignant)还是 ...

  4. matlab(8) Regularized logistic regression : 不同的λ(0,1,10,100)值对regularization的影响,对应不同的decision boundary\ 预测新的值和计算模型的精度predict.m

    不同的λ(0,1,10,100)值对regularization的影响\ 预测新的值和计算模型的精度 %% ============= Part 2: Regularization and Accur ...

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

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

  6. matlab(7) Regularized logistic regression : mapFeature(将feature增多) and costFunctionReg

    Regularized logistic regression : mapFeature(将feature增多) and costFunctionReg ex2_reg.m文件中的部分内容 %% == ...

  7. matlab(6) Regularized logistic regression : plot data(画样本图)

    Regularized logistic regression :  plot data(画样本图) ex2data2.txt 0.051267,0.69956,1-0.092742,0.68494, ...

  8. logistic regression的一些问题,不平衡数据,时间序列,求解惑

    Logistic Regression 1.在有时间序列的特征数据中,怎么运用LR? 不光是LR,其他的模型也是. 有很多基本的模型变形之后,变成带时序的模型.但,个人觉得,这类模型大多不靠谱. 我觉 ...

  9. Machine Learning - 第3周(Logistic Regression、Regularization)

    Logistic regression is a method for classifying data into discrete outcomes. For example, we might u ...

随机推荐

  1. 走进热修复框架AndFix的世界

    关于阿里的开源热修复框架AndFix引起了广泛共鸣,受到了很多人的青睐.那今天就跟随我的步伐来详细了解一下AndFix的详细信息和使用方法.1.什么是AndFix? AndFix是阿里巴巴出的一个专门 ...

  2. 任务调度Quartz.Net之Windows Service

    这个应该是关于Quartz.Net使用的最后一篇文章了,之前的介绍都是基于Web的,这种实现任务调度的方式很少见,因为不管是MVC.WebApi还是WebService,它们都需要寄宿在IIS上运行, ...

  3. [C语言]给定直角三角形面积和斜边长

    [A题]   翘课的HugeGun_ 时间限制:1000ms   内存限制:65536kb 题目描述 HugeGun学姐很喜欢翘课.不幸的是,这一次她被发现了. 老师让她打扫了教室.当她把扫把靠在墙上 ...

  4. Java:session中的invalidate()的作用是什么呢?求解

    手工杀会话.会话失效有2种可能:超时和手工杀会话.手工杀方便省时间,程序员都爱用. 比如我做一个程序需要登录,中间访问的页面有会话控制,如果没有登录则跳转到登录页面,退出时清会话信息. 这是有两个选择 ...

  5. 【工具】java发送验证码邮件

    文章目录 前言 配置邮箱服务器 代码实现 发送随机验证码与验证 后记 前言 要实现 可以设置格式,附件,抄送等功能,就跟真人操控邮箱发送邮件一样的功能,或许比较难,博主没研究,博主暂时没用到那些功能, ...

  6. Python25之字典1

    一.字典的意义 字典不同于前述的序列类型,他是一种映射类型,它的引入就是为了简化定义索引值和元素值存在的特定关系的定义和访问问题 二,字典定义 字典变量名 = {key1 : value1, key2 ...

  7. Redis--list类型操作命令

    列表 list Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列 表的头部(左边)或者尾部(右边) 列表 list——基本命令 lpush 语法:lpush key valu ...

  8. MAC帧封装

    通过控制台输入一段文字,输出MAC帧的2进制和16进制的字符串,主要是求FCS.这里只考虑单帧的情况,即只考虑输入数据在1字节~1500字节之间的情况,对于更长的数据暂不考虑. 1.MAC帧基本格式 ...

  9. 数据结构-链式队列-C++

    用链表搭建的栈与队列相对简单,队列的特点是先进先出,不啰嗦了,由于代码比较简单,相信光顾的人不会太多,下面直接贴代码. 头文件 #ifndef QUEUELI_H #define QUEUELI_H ...

  10. go语言的安装及环境配置

    Go语言开发环境搭建(ubuntu) 1.清理.卸载之前的 go 语言环境: 删除go目录:sudo rm -rf /usr/local/go 删除软链接(如果建立了软链接):sudo rm -rf ...