简体   繁体   English

在Octave中绘制决策边界线

[英]Plotting decision boundary line in Octave

I have been working on a machine learning course and currently on Classification. 我一直在学习机器学习课程,目前正在学习分类。 I implemented the classification algorithm and obtained the parameters as well as the cost. 我实现了分类算法,并获得了参数以及成本。 The assignment already has a function for plotting the decision boundary and it worked but I was trying to read their code and cannot understand these lines. 该分配已经具有绘制决策边界的功能,并且可以正常工作,但是我试图读取其代码并且无法理解这些行。

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

Anyone explain? 有人解释吗?

I'm also taking the same course as you. 我也和你上同一门课。 I guess what the code does is to generate two points on the decision line. 我猜代码的作用是在决策线上生成两个点。

As you know you have the function: 如您所知,您具有以下功能:

theta0 + theta1 * x1 + theta2 * x2 = 0

Which it can be rewritten as: 可以将其重写为:

c + mx + ky = 0

where x and y are the axis corresponding to x1 and x2 , c is theta(0) or the y-intercept, m is the slope or theta(1) , and k is theta(2) . 其中xy是对应于x1x2的轴, ctheta(0)或y轴截距, m是斜率或theta(1)ktheta(2)

This equation ( c + mx + ky = 0 ) corresponds to the decision boundary, so the code is finding two values for x (or x1 ) which cover the whole dataset (-2 and +2 in plot_x min and max functions) and then uses the equation to find the corresponding y (or x2 ) values. 该方程式( c + mx + ky = 0 )对应于决策边界,因此代码正在寻找x (或x1 )的两个值,它们覆盖了整个数据集( plot_x minmax函数中的-2和+2),然后使用方程式找到对应的y (或x2 )值。 Finally, a decision boundary can be plotted -- plot(plot_x, plot_y) . 最后,可以绘制决策边界plot(plot_x, plot_y)

In other words, what it does is to use the the equation to generate two points to plot the line on graph, the reason of doing this is that Octave cannot plot the line given an equation to it. 换句话说,它的作用是使用方程式生成两个点以在图形上绘制直线,这样做的原因是Octave无法在给定方程式的情况下绘制直线。

Hope this can help you, sorry for any mistake in grammar or unclear explanation ^.^ 希望这对您有所帮助,对于语法上的任何错误或不清楚的解释^。^


Rearranging equations helped me, so adding those here: 重新排列方程式对我有帮助,因此在这里添加:

plot_y = -1/theta2 (theta1*plot_x + theta0)

note that index in Octave starts at 1, not at 0, so theta(3) = theta2 , theta(2) = theta1 and theta(1) = theta0 . 请注意,Octave中的索引从1开始,而不是从0开始,因此theta(3) = theta2theta(2) = theta1theta(1) = theta0

This plot_y equation is equivalent to: plot_y公式等效于:

c + mx + ky = 0             <=>
        -ky = mx + c        <=>
          y = -1/k (mx + c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM