Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 3/Programming Assignment - 2/machine-learning-ex2/ex2/plotDecisionBoundary.m
863 views
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
% 1) Mx3 matrix, where the first column is an all-ones column for the
8
% intercept.
9
% 2) MxN, N>3 matrix, where the first column is all-ones
10
11
% Plot Data
12
plotData(X(:,2:3), y);
13
hold on
14
15
if size(X, 2) <= 3
16
% Only need 2 points to define a line, so choose two endpoints
17
plot_x = [min(X(:,2))-2, max(X(:,2))+2];
18
19
% Calculate the decision boundary line
20
plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));
21
22
% Plot, and adjust axes for better viewing
23
plot(plot_x, plot_y)
24
25
% Legend, specific for the exercise
26
legend('Admitted', 'Not admitted', 'Decision Boundary')
27
axis([30, 100, 30, 100])
28
else
29
% Here is the grid range
30
u = linspace(-1, 1.5, 50);
31
v = linspace(-1, 1.5, 50);
32
33
z = zeros(length(u), length(v));
34
% Evaluate z = theta*x over the grid
35
for i = 1:length(u)
36
for j = 1:length(v)
37
z(i,j) = mapFeature(u(i), v(j))*theta;
38
end
39
end
40
z = z'; % important to transpose z before calling contour
41
42
% Plot z = 0
43
% Notice you need to specify the range [0, 0]
44
contour(u, v, z, [0, 0], 'LineWidth', 2)
45
end
46
hold off
47
48
end
49
50