Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot theta vector line for classification algorithm (MATLAB)

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?

like image 674
William Falcon Avatar asked Feb 12 '23 22:02

William Falcon


1 Answers

In logistic regression for binary classification, the probability of a new sample x classified as 0 or 1 is:

enter image description here

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)).

enter image description here

In our case, the data has two dimensions plus the bias, hence:

enter image description here

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:

enter image description here

like image 83
tashuhka Avatar answered Feb 15 '23 09:02

tashuhka