李燕

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

画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

 

posted on 2015-09-23 21:04  李燕  阅读(1225)  评论(0编辑  收藏  举报