简体   繁体   中英

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

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. Finally, a decision boundary can be plotted -- 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.

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 .

This plot_y equation is equivalent to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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