user2006697
user2006697

Reputation: 1117

Octave plotting step-by-step (!) a costfunction and a gradient descent

for Andrew Ng's Cousera course "machine learning" I started to use Octave.

For the assignment on linear regression (one Variable) code is provided that creates a scatter plot of the original data with the fitted line, the bow shaped cost function J( theta_0, theta_1 ) and a contour plot.

BUT: all these plots provide only the FINAL result.

I would like to see a step-by-step plot development so as to understand better what's going on. Step-by-step means: create a figure, add first value, then second, then third, etc, until arriving at the final result.

(1) Screenshot from one lecture video: Is it possible to visualise this, adding the pink then the green, then red, etc dot?

Screenshot from one lecture video: Is it possible to visualise this, i.e. adding the pink, then the green dot, then red dot, etc dot?

(2.1) First step in searching for the correct parameters, gradient Descent Step 1

(2.2) Final step in searching for the correct parameters, gradient descent: all single steps are included

(2) First step in searching for the correct parameters, i.e. gradient Descent Step 1

enter image description here

Note 1: concerning screenshot scatterplot + contour plot: in the scatter plot always one line is visible, but it is changing from step to step. In the contour plot one dot after the other appears.

Note 2: this is NOT an assignment request! I only want to learn plotting in Octave and want to combine this with learning how gradient descent "really" works.

Thanks a million to everyone who provide some code help!

Upvotes: 2

Views: 2111

Answers (1)

ederag
ederag

Reputation: 2509

Use hold all (or just hold on, but the color incrementation will not be automatic) ColorOrder is an axis property that can be made default for subsequent figure axes, as partly described here.

# start with a blank page
clf

# the curve (full line, blue)
x = -5:0.1:10;
y = x.^2;
plot(x, y, '-b')

# successive colors for the points
N_colors = 6;
colororder_map = cool(N_colors);
set(gcf, 'defaultaxesColorOrder', colororder_map)

# this will add the following plots, instead of replacing the old one
hold all
# note: "hold on" does the same, except the colors are not incremented

# the progression
x_p = [10, 7, 3, 1, 0];
y_p = x_p .^2;

# plot the progression
for cpt = 1:numel(x_p)
    x_current = x_p(cpt);
    y_current = y_p(cpt);
    plot(x_current, y_current, 'o')
    pause
endfor

sample plot

Upvotes: 2

Related Questions