When developing learning algorithms, very
often a few simple plots
can give you a better
sense of what the algorithm
is doing and just sanity check
that everything is going okay
and the algorithms doing what is supposed to.
For example, in an earlier
video, I talked about how
plotting the cost function J
of theta can help you
make sure that gradient descent is converging.
Often, plots of the data
or of all the learning algorithm outputs
will also give you ideas
for how to improve your learning algorithm.
Fortunately, Octave has very
simple tools to generate lots
of different plots and when
I use learning algorithms, I find
that plotting the data, plotting
the learning algorithm and so
on are often an important
part of how I get
ideas for improving the
algorithms and in this video,
I'd like to show you some
of these Octave tools for plotting and visualizing your data.
Here's my Octave window.
Let's quickly generate some data
for us to plot.
So I'm going to set T
to be equal to, you know, this array of numbers.
Here's T, set of
numbers going from 0 up to .98.
Let's set y1 equals sine
of 2 pie 40 and
if I want to plot the sine function, it's very easy.
I just type plot T comma Y
1 and hit enter.
And up comes this plot
where the horizontal axis is
the T variable and the vertical
axis is y1, which
is the sine you saw in the function that we just computed.
Let's set y2 to be
equal to the cosine
of two pi, four T, like so.
And if I plot
T comma y2, what octave
will I do is I'll take my
sine plot and it
will replace with this cosine
function and now, you know, cosine of xi of 1.
Now, what if I
want to have both
the sine and the cosine plots on top of each other?
What I'm going to do is I'm
going to type plot t,y1.
So here's my sine function, and then
I'm going to use the function hold on.
And what hold does it closes
octaves to now
figures on top of the
old one and let
me now plot t y2.
I'm going to plot the cosine function in a different color.
So, let me put there
r in quotation marks there
and instead of replacing
the current figure, I'll plot the
cosine function on top and
the r indicates the what is an event color.
And here additional commands - x
label times, to label the X axis, or the horizontal axis.
And Y label values A,
to label the vertical axis value,
and I can also
label my two lines
with this command: legend sine cosine
and this puts this
legend up on the upper
right showing what the 2
lines are, and finally title
my plot is the title at the top of this figure.
Lastly, if you want to save
this figure, you type print -dpng
myplot
.png.
So PNG is a graphics
file format, and if you
do this it will let you save this as a file.
If I do that,
let me actually change directory to,
let's see, like
that, and then I will print that out.
So this will take a
while depending on how
your Octave configuration is setup,
may take a few seconds, but change
directory to my desktop and Octave
is now taking a few seconds to save this.
If I now go to my desktop, Let's hide these windows.
Here's myplot.png
which Octave has saved, and you
know, there's the figure saved as the PNG file.
Octave can save thousand other formats as well.
So, you can type help plot,
if you want to see the
other file formats, rather than
PNG, that you can save
figures in.
And lastly, if you want
to get rid of the plot, the
close command causes the figure to go away.
As I figure if I type
close, that figure just
disappeared from my desktop.
Octave also lets you specify a figure and numbers.
You type figure 1 plots t, y1.
That starts up
first figure, and that plots t, y1.
And then if you want a second figure, you specify a different figure number.
So figure two, plot t,
y2 like so, and
now on my desktop, I actually have 2 figures.
So, figure 1 and figure
2 thus 1 plotting the sine
function, 1 plotting the cosine function.
Here's one other neat command that
I often use, which is the subplot command.
So, we're going to use subplot 1 2 1.
What it does it sub-divides
the plot into a
one-by-two grid with the
first 2 parameters are, and
it starts to access the
first element. That's
what the final parameter 1 is, right?
So, divide my figure into a
one by two grid, and I
want to access the first
element right now.
And so, if I type that
in, this product, this figure, is on the left.
And if I plot t,
y1, it now fills
up this first element.
And if I I'll do subplot 122.
I'm going to start to
access the second element and plot t, y2.
Well, throw in y2 in
the right hand side, or in the second element.
And last command, you can
also change the axis scales
and change axis these to 1.51
minus 1 1 and this
sets the x range
and y range for the
figure on the right,
and concretely, it assess the horizontal
major values in the figure
on the right to make sure 0.5
to 1, and the vertical
axis values use the range from minus one to one.
And, you know, you don't need to memorize all these commands.
If you ever need to
change the access or you
need to know is that, you know, there's an
access command and you can
already get the details
from the usual octave help command.
Finally, just a couple last
commands CLF clear is
a figure and here's one unique trait.
Let's set a to be equal
to a 5 by 5
magic squares a. So, a
is now this 5 by 5
matrix does a neat
trick that I sometimes use to
visualize the matrix, which is
I can use image sc
of a what this will
do is plot a five
by five matrix, a five by five grid of color.
where the different colors correspond to
the different values in the A matrix.
So concretely, I can also do color bar.
Let me use a
more sophisticated command, and image sc
A color bar
color map gray.
This is actually running three commands at a time.
I'm running image sc then running
color bar, then running color map gray.
And what this does, is it sets
a color map, so a
gray color map, and on the
right it also puts in this color bar.
And so this color bar
shows what the different shades of color correspond to.
Concretely, the upper left
element of the A matrix
is 17, and so that corresponds
to kind of a mint shade of gray.
Whereas in contrast the second
element of A--sort of the
1 2 element of A--is 24.
Right, so it's A 1 2 is 24.
So that corresponds to
this square out here, which is
nearly a shade of white.
And the small value, say
A--what is that? A
4 5, you know, is a value
3 over here that corresponds--
you can see on my color bar
that it corresponds to a
much darker shade in this image.
So here's another example,
I can plot a larger, you
know, here's a magic 15 that
gives you a 15 by 15
magic square and this
gives me a plot of what
my 15 by 15 magic squares values looks like.
And finally to wrap
up this video, what you've
seen me do here is
use comma chaining of function calls.
Here's how you actually do this.
If I type A equals
1, B equals 2, C equals
3, and hit Enter, then
this is actually carrying out
three commands at the same time.
Or really carrying out three
commands, one after another,
and it prints out all three results.
And this is a lot like
A equals 1, B equals
2, C equals 3, except
that if I use semicolons instead
of a comma, it doesn't print out anything.
So, this, you know,
this thing here we call comma
chaining of commands, or comma chaining of function calls.
And, it's just another
convenient way in Octave to
put multiple commands like image sc
color bar, colon map
to put multi-commands on the same line.
So, that's it.
You now know how to plot
different figures and octave, and
in next video the
next main piece that I want
to tell you about is how to
write control statements like if,
while, for statements and
octave as well as hard to define and use functions