How to plot a decision boundary for binary logistic regression in matlab

345 Views Asked by At

let me preface by saying this is from a homework question, but the question is not to plot the decision boundary, just to train the model and do some predictions. I have already done that and my predictions seem to be correct.

But I would like to verify my results by plotting the decision boundary. This is not part of the homework.

The question was to take a simple dataset $$ X = \begin{bmatrix} -1&0&2&0&1&2\\ -1&1&0&-2&0&-1 \end{bmatrix} $$

$$ y = \begin{bmatrix} 1&1&1&-1&-1&-1 \end{bmatrix} $$

Given this, convert the input to non-linear functions: $$ z = \begin{bmatrix} x_1\\x_2\\x_1^2\\x_1x_2\\x_2^2 \end{bmatrix} $$

Then train the binary logistic regression model to determine parameters $\hat{w} = \begin{bmatrix} w\\b \end{bmatrix}$ using $\hat{z} = \begin{bmatrix} z\\1 \end{bmatrix}$

So, now assume that the model is trained and I have $\hat{w}^*$ and would like to plot my decision boundary $\hat{w}^{*T}\hat{z} = 0$

Currently to scatter the matrix I have

scatter(X(1,:), X(2,:))
axis([-1.5 2.5 -2.5 1.5])
hold on
% what do I do to plot the decision boundary?

Not sure where to go from here. I have tried using symbolic functions, but fplot doesn't like using 2 variables.

2

There are 2 best solutions below

1
On

I could plot it using the following code:

syms X Y y

X = [-1 -1; 0 1; 2 0; 0 -2; 1 0; 2 -1]
Y = [1 1 1 -1 -1 -1]
y = categorical(Y)
B = mnrfit(X,y)

scatter(X(:,1),X(:,2))
hold on
plot(X(:,1),-((B(1)*1+B(2)*X(:,1))/B(3)));
axis([-1.5 2.5 -2.5 1.5])
hold off

Note that I needed to change the $y$ values to categorial values (internally, these are positive integers), since mnrfit required this. But it has no influence on our decision problem.

The result is:

enter image description here

0
On

I was able to plot the curve using ezplot:

X = [-1 0 2 0 1 2; -1 1 0 -2 0 -1];
P = length(X);
z = zeros(6, P);
    
for i = 1:P
    z(:,i) = z_calc(X(:,i));
end

syms x1 x2
z_fn = [x1; x2; x1^2; x1*x2; x2^2; 1];

% xs was previously computed by the logistic regression model
eqn = (xs'*z_fn == 0);

ezplot(eqn)
hold on
scatter(X(1,1:3), X(2,1:3), 'red')
axis([-1.5 2.5 -2.5 1.5])
hold on
scatter(X(1,4:6), X(2,4:6), 'blue')
title("Decision Boundary")
hold off

enter image description here