After a logistic regression function, I get a theta vector like [-0.34, -4.5, 0.5]. How can I use this to draw the boundary line in the graph?
In logistic regression for binary classification, the probability of a new sample x
classified as 0 or 1 is:
Hence, the decision boundary corresponds to the line where P(y=1|x)=P(y=0|x)=sigmoid(theta'*x)=0.5
, which corresponds to theta'*x=0
. The sigmoid function is sigmoid = @(z) 1.0 ./ (1.0 + exp(-z))
.
In our case, the data has two dimensions plus the bias, hence:
For example, the decision boundary with x1
in the range [-1 1] can be represented as:
theta = [-0.34, -4.5, 0.5];
sigmoid = @(z) 1.0 ./ (1.0 + exp(-z));
% Random points
N = 100;
X1 = 2*rand(N,1)-1;
X2 = 20*rand(N,1)-10;
x = [ones(N,1), X1(:), X2(:)];
y = sigmoid(theta * x.') > 0.5;
% Boundary line
plot_x = [-1 1];
plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));
% Plot
figure;
hold on;
scatter(X1(y==1),X2(y==1),'bo');
scatter(X1(y==0),X2(y==0),'rx');
plot(plot_x, plot_y, 'k--');
hold off
xlabel('x1'); ylabel('x2');
title('x_{2} = 0.68 + 9 x_{1}');
axis([-1 1 -10 10]);
It generates the following plot:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With