Path: blob/master/Week 3/Programming Assignment - 2/machine-learning-ex2/ex2/plotDecisionBoundary.m
863 views
function plotDecisionBoundary(theta, X, y)1%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with2%the decision boundary defined by theta3% PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the4% positive examples and o for the negative examples. X is assumed to be5% a either6% 1) Mx3 matrix, where the first column is an all-ones column for the7% intercept.8% 2) MxN, N>3 matrix, where the first column is all-ones910% Plot Data11plotData(X(:,2:3), y);12hold on1314if size(X, 2) <= 315% Only need 2 points to define a line, so choose two endpoints16plot_x = [min(X(:,2))-2, max(X(:,2))+2];1718% Calculate the decision boundary line19plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));2021% Plot, and adjust axes for better viewing22plot(plot_x, plot_y)2324% Legend, specific for the exercise25legend('Admitted', 'Not admitted', 'Decision Boundary')26axis([30, 100, 30, 100])27else28% Here is the grid range29u = linspace(-1, 1.5, 50);30v = linspace(-1, 1.5, 50);3132z = zeros(length(u), length(v));33% Evaluate z = theta*x over the grid34for i = 1:length(u)35for j = 1:length(v)36z(i,j) = mapFeature(u(i), v(j))*theta;37end38end39z = z'; % important to transpose z before calling contour4041% Plot z = 042% Notice you need to specify the range [0, 0]43contour(u, v, z, [0, 0], 'LineWidth', 2)44end45hold off4647end484950