How to plot decision boundary for logistic regression in MATLAB?

30 views (last 30 days)
I am trying to run logistic regression on a small data set. I present the full code below:
%% Plotting data
x1 = linspace(0,3,50);
mqtrue = 5;
cqtrue = 30;
dat1 = mqtrue*x1+5*randn(1,50);
x2 = linspace(7,10,50);
dat2 = mqtrue*x2 + (cqtrue + 5*randn(1,50));
x = [x1 x2]'; % X
subplot(2,2,1);
dat = [dat1 dat2]'; % Y
scatter(x1, dat1); hold on;
scatter(x2, dat2, '*'); hold on;
classdata = (dat>40);
%% Compute Cost and Gradient
% Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(x);
% Add intercept term to x and X_test
x = [ones(m, 1) x];
% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);
% Compute and display initial cost and gradient
[cost, grad] = logistic_costFunction(initial_theta, x, dat);
% Plot Boundary
plotDecisionBoundary(initial_theta, x, dat);
%% Predict and Accuracies
% Compute accuracy on our training set
p = predict(initial_theta, x);
fprintf('Train Accuracy: %f\n', mean(double(p == dat)) * 100);
000.png
I then proceed to compute gradient and cost:
logistic_costFunction.m
-----------------------
function [J, grad] = logistic_costFunction(theta, X, y)
% Initialize some useful values
m = length(y); % number of training examples
grad = zeros(size(theta));
H = sigmoid(X*theta);
T = y.*log(H) + (1 - y).*log(1 - H);
J = -1/m*sum(T);
% ====================Compute grad==================
for i = 1 : m
grad = grad + (H(i) - y(i)) * X(i,:)';
end
grad = 1/m*grad;
end
sigmoid.m
function g = sigmoid(z)
% Computes thes sigmoid of z
g = zeros(size(z));
g = 1 ./ (1 + (1 ./ exp(z)));
end
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
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 = (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
I get no errors, even though I haven't verified if the logistic regression is actually working. I then proceed to plot and predict accuracy.
predict.m
---------
function p = predict(theta, X)
% p = PREDICT(theta, X) computes the predictions for X using a
% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)
p = (sigmoid(X*theta) >= 0.5);
end
After running the main script I get an empty plot:
00.png
Workspace:
0.png
Any suggestions on how I can plot the decision boundary line correctly?

Answers (2)

An Liu
An Liu on 16 Jan 2020
maybe your axis is not fit your data
you need to change the axis range in plotDecisionBoundery function
function plotDecisionBoundary(theta, X, y)
...
% Legend, specific for the exercise
legend('Admitted', 'Not admitted', 'Decision Boundary')
axis([30, 100, 30, 100])->change here
...
end
  2 Comments
Suresh Subbanarahari
Suresh Subbanarahari on 21 Mar 2020
An liu, thanks for your reply. I had similar issue and could adjust to see the values. Any suggestion to check on why it always shows a straight line which is not an expected decision boundary.
Samet Oran
Samet Oran on 31 Aug 2020
because your code needs to arrange "cost function" The cost value that 0.6931 the first iteration result. It needs to reduce. The link will help you;

Sign in to comment.


waleed qaisrani
waleed qaisrani on 5 Nov 2020
Chage your "plot_y" in your "Plot Decision Boundary" Function
From this:
plot_y = (theta(2).*plot_x + theta(1));
To this:
plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!