在 Octave 中绘制决策边界线

Lev*_*evi 2 octave

我一直在学习机器学习课程,目前正在研究分类。我实现了分类算法并获得了参数和成本。作业已经有一个绘制决策边界的函数,它起作用了,但我试图阅读他们的代码并且无法理解这些行。

plot_x = [min(X(:,2))-2,  max(X(:,2))+2]; 
% Calculate the decision boundary line
plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));
Run Code Online (Sandbox Code Playgroud)

有人解释一下吗?

小智 7

我也跟你一样的课程。我猜代码的作用是在决策线上生成两个点。

如您所知,您具有以下功能:

theta0 + theta1 * x1 + theta2 * x2 = 0
Run Code Online (Sandbox Code Playgroud)

可以改写为:

c + mx + ky = 0
Run Code Online (Sandbox Code Playgroud)

其中xy是对应于x1和的轴x2ctheta(0)或 y 轴截距,m是斜率 或theta(1),并且ktheta(2)

这个方程 ( c + mx + ky = 0) 对应于决策边界,因此代码为x(or x1)寻找两个值,它们覆盖整个数据集(-2 和 +2 in plot_x minandmax函数),然后使用该方程找到相应的y(或x2)值。最后,可以绘制决策边界 -- plot(plot_x, plot_y)

换句话说,它所做的是使用方程生成两个点来在图形上绘制线,这样做的原因是 Octave 无法绘制给定方程的线。

希望能帮到你,如有语法错误或解释不清楚^.^


重新排列方程对我有帮助,所以在这里添加:

plot_y = -1/theta2 (theta1*plot_x + theta0)
Run Code Online (Sandbox Code Playgroud)

注意到,在八度开始该指数在1,不为0,所以theta(3) = theta2theta(2) = theta1theta(1) = theta0

这个plot_y等式等价于:

c + mx + ky = 0             <=>
        -ky = mx + c        <=>
          y = -1/k (mx + c)
Run Code Online (Sandbox Code Playgroud)