【问题标题】:How to plot theta vector line for classification algorithm (MATLAB)如何绘制分类算法的θ向量线(MATLAB)
【发布时间】:2014-11-25 03:37:39
【问题描述】:

在逻辑回归函数之后,我得到一个像 [-0.34, -4.5, 0.5] 的 theta 向量。如何使用它在图中绘制边界线?

【问题讨论】:

    标签: matlab machine-learning logistic-regression


    【解决方案1】:

    在二元分类的逻辑回归中,新样本x分类为0或1的概率为:

    因此,决策边界对应于P(y=1|x)=P(y=0|x)=sigmoid(theta'*x)=0.5 所在的行,它对应于theta'*x=0。 sigmoid函数是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]);
    

    它会生成以下图:

    【讨论】:

    • OP 的 theta 是从逻辑回归中获得的。您提到的文章是关于线性回归的。这里有什么遗漏吗?
    • 你完全正确!!我没有意识到我的错误。我纠正了错误。
    • 看起来不错。所以实际上在边界:slope = theta(2)/(-theta(3)) = 9intercept = theta(1)/(-theta(3)) = 0.68。因此边界是y = 9x + 0.68
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多