Wil*_*con 4 matlab machine-learning logistic-regression
经过logistic回归函数后,我得到了一个像[-0.34,-4.5,0.5]的theta向量。如何使用它在图形中绘制边界线?
在二元分类的逻辑回归中,新样本x
分类为0或1 的概率为:
因此,决策边界对应于的线P(y=1|x)=P(y=0|x)=sigmoid(theta'*x)=0.5
,其中对应于theta'*x=0
。乙状结肠功能为sigmoid = @(z) 1.0 ./ (1.0 + exp(-z))
。
在我们的例子中,数据具有两个维度加上偏差,因此:
例如,x1
范围为[-1 1] 的决策边界可以表示为:
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]);
Run Code Online (Sandbox Code Playgroud)
它生成以下图: