aboutsummaryrefslogtreecommitdiff
path: root/R_LogR/mlclass-ex2/plotDecisionBoundary.m
diff options
context:
space:
mode:
authorleshe4ka46 <alex9102naid1@ya.ru>2025-12-13 19:41:40 +0300
committerleshe4ka46 <alex9102naid1@ya.ru>2025-12-13 19:41:40 +0300
commit175ac10904d0f31c3ffeeeed507c8914f13d0b15 (patch)
tree671c68a03354c5084470c5cfcfd4fe87aae2aff8 /R_LogR/mlclass-ex2/plotDecisionBoundary.m
parent72b4edeadeafc9c54b3db9b0961a45da3d07b77c (diff)
linr, logr
Diffstat (limited to 'R_LogR/mlclass-ex2/plotDecisionBoundary.m')
-rw-r--r--R_LogR/mlclass-ex2/plotDecisionBoundary.m49
1 files changed, 49 insertions, 0 deletions
diff --git a/R_LogR/mlclass-ex2/plotDecisionBoundary.m b/R_LogR/mlclass-ex2/plotDecisionBoundary.m
new file mode 100644
index 0000000..cfdf3e4
--- /dev/null
+++ b/R_LogR/mlclass-ex2/plotDecisionBoundary.m
@@ -0,0 +1,49 @@
+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);
+hold on
+
+if size(X, 2) <= 3
+ % Only need 2 points to define a line, so choose two endpoints
+ 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, and adjust axes for better viewing
+ plot(plot_x, plot_y)
+
+ % Legend, specific for the exercise
+ legend('Admitted', 'Not admitted', 'Decision Boundary')
+ axis([30, 100, 30, 100])
+else
+ % Here is the grid range
+ u = linspace(-1, 1.5, 50);
+ 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
+