In this second tutorial video on
Octave, I'd like to start
to tell you how to move data around in Octave.
So, if you have data for
a machine learning problem, how do you load that data in Octave?
How do you put it into matrix?
How do you manipulate these matrices?
How do you save the results?
How do you move data around and operate with data?
Here's my Octave window as
before, picking up from where we left off in the last video.
If I type A, that's
the matrix so we generate it, right,
with this command equals one, two,
three, four, five, six, and
this is a three by two matrix.
The size command in Octave
lets you, tells you what is the size of a matrix.
So size A returns three, two.
It turns out that
this size command itself is actually
returning a one by two matrix.
So you can actually set SZ equals
size of A and SZ
is now a one by two
matrix where the first element
of this is three, and the second element of this is two.
So, if you just type size of SZ. Does SZ
is a one by
two matrix whose two elements
contain the dimensions of the
matrix A. You can
also type size A one
to give you back the first
dimension of A, size
of the first dimension of A.
So that's the number
of rows and size A two
to give you back two, which
is the number of columns in
the matrix A. If you
have a vector V, so
let's say V equals one, two,
three, four, and you
type length V. What
this does is it gives you
the size of the longest dimension.
So you can also type
length A and because
A is a three by
two matrix, the longer
dimension is of size
three, so this should print out three.
But usually we apply length only to vectors.
So you know, length one, two,
three, four, five, rather
than apply length to matrices
because that's a little more confusing.
Now, let's look
at how the load data and
find data on the file system.
When we start an Octave
we're usually, we're often in
a path that
is, you know, the location of where the Octave location is.
So the PWD command shows
the current directory, or the
current path that Octave is in.
So right now we're in this maybe somewhat off scale directory.
The CD command stands
for change directory, so I
can go to C:/Users/Ang/Desktop, and
now I'm in, you know, in my Desktop
and if I type ls,
ls is, it comes from a Unix or a Linux command.
But, ls will list the
directories on my desktop and
so these are the files
that are on my Desktop right now.
In fact, on my desktop are
two files: Features X and
Price Y that's maybe come
from a machine learning problem I want to solve.
So, here's my desktop.
Here's Features X, and
Features X is this window,
excuse me, is this file with two columns of data.
This is actually my housing prices data.
So I think, you know, I
think I have forty-seven rows in this data set.
And so the first house
has size two hundred four
square feet, has three bedrooms; second
house has sixteen hundred square
feet, has three bedrooms; and so on.
And Price Y is this
file that has
the prices of the data in my training set.
So, Features X and
Price Y are just text files with my data.
How do I load this data into Octave?
Well, I just type
the command load Features X dot
dat and if I
do that, I load the Features X
and can load Price Y dot dat. And
by the way, there are multiple ways to do this.
This command if you put
Features X dot dat on that
in strings and load it like so.
This is a typo there.
This is an equivalent command.
So you can, this
way I'm just putting the file name of the string
in the founding in a
string and in an
Octave use single quotes to
represent strings, like so.
So that's a string, and we
can load the file
whose name is given by that string.
Now the WHO command now
shows me what variables I
have in my Octave workspace.
So Who shows me whether
the variables that Octave has in memory currently.
Features X and Price Y
are among them, as well as
the variables that, you know,
we created earlier in this session.
So I can type Features X
to display features X. And
there's my data.
And I can type size features
X and that's my
47 by two matrix.
And some of these size, press
Y, that gives me
my 47 by one vector.
This is a 47 dimensional vector.
This is all common vector that
has all the prices Y in my training set.
Now the who function shows
you one of the variables that, in the current workspace.
There's also the who S
variable that gives you the detailed view.
And so this also, with
an S at the end this also
lists my variables except that it
now lists the sizes as well.
So A is a three by
two matrix and features
X as a 47 by 2 matrix.
Price Y is a 47 by one matrix.
Meaning this is just a vector.
And it shows, you know, how many bytes of memory it's taking up.
As well as what type of data this is.
Double means double position floating
point so that just means that
these are real values, the floating point numbers.
Now if you want to get
rid of a variable you can use the clear command.
So clear features X and type whose again.
You notice that the features X
variable has now disappeared.
And how do we save data?
Let's see.
Let's take the variable V and
say that it's a price Y 1 colon 10.
This sets V to be
the first 10 elements of
vector Y. So let's type who or whose.
Whereas Y was a 47 by 1 vector.
V is now 10 by 1.
B equals price Y, one
column ten that sets it
to the just the first ten
elements of Y. Let's say
I wanna save this to date to disc
the command save, hello.mat
V. This will
save the variable V into
a file called hello.mat.
So let's do that.
And now a file
has appeared on my Desktop, you
know, called Hello.mat.
I happen to have MATLAB installed
in this window, which is why,
you know, this icon looks
like this because Windows is recognized
as it's a MATLAB file,but don't
worry about it if this file
looks like it has a different
icon on your machine and
let's say I clear all my variables.
So, if you type clear without
anything then this actually deletes all of the variables in your workspace.
So there's now nothing left in the workspace.
And if I load hello.mat,
I can now load back my
variable v, which is
the data that I
previously saved into the hello.mat file.
So, hello.mat, what we did just now to save hello.mat
to view, this save the
data in a binary format,
a somewhat more compressed binary format.
So if v is a lot
of data, this, you know, will be somewhat more compressing.
Will take off less the space.
If you want to save your
data in a human readable
format then you type save hello.text
the variable v and then -ascii.
So, this will save
it as a text
or as ascii format of text.
And now, once I've done
that, I have this file.
Hello.text has just
appeared on my desktop, and
if I open this up, we
see that this is a text
file with my data saved away.
So that's how you load and save data.
Now let's talk a bit about how to manipulate data.
Let's set a equals to that
matrix again so is my three by two matrix.
So as indexing.
So type A 3, 2.
This indexes into
the 3, 2 elements of
the matrix A. So, this
is what, you know,
in normally, we will write this
as a subscript 3, 2
or A subscript,
you know, 3, 2
and so that's the element and
third row and second column
of A which is the element of six.
I can also type A to
comma colon to fetch
everything in the second row.
So, the colon means every
element along that row or column.
So, a of 2 comma
colon is this second row of a. Right.
And similarly, if I do a colon comma 2
then this means get everything in
the second column of A. So, this gives me 2 4 6.
Right this means of
A. everything, second column.
So, this is my second
column A, which is 2 4 6.
Now, you can also
use somewhat most of the sophisticated index in the operations.
So So, we just click each of an example.
You do this maybe less often,
but let me do this A 1 3 comma colon.
This means get all of
the elements of A who's first indexes one or three.
This means I get everything from
the first and third rows of
A and from all
columns.
So, this was the
matrix A and so A
1 3 comma colon means get
everything from the first row
and from the second row and
from the third row and the
colon means, you know, one both
of first and the second
columns and so this
gives me this 1 2 5 6.
Although, you use the source
of more subscript index
operations maybe somewhat less often.
To show you what else we can do.
Here's the A matrix and this
source A colon, to give me the second column.
You can also use this to do assignments.
So I can take the second column of
A and assign that to
10, 11, 12, and
if I do that I'm now, you
know, taking the second column of
a and I'm assigning this column vector 10, 11, 12 to it.
So, now a is this matrix that's 1, 3, 5.
And the second column has been replaced by 10, 11, 12.
And here's another operation.
Let's set A to be equal
to A comma 100, 101,
102 like so and what
this will do is
depend another column vector
to the right.
So, now, oops.
I think I made a little mistake.
Should have put semicolons there
and now A is equals to this.
Okay?
I hope that makes sense.
So this 100, 101, 102.
This is a column vector
and what we did
was we set A, take
A and set it to the original definition.
And then we put that column
vector to the right
and so, we ended up taking
the matrix A and--which was
these six elements on the left.
So we took matrix
A and we appended another
column vector to the right;
which is now why A is
a three by three matrix that looks like that.
And finally, one neat
trick that I sometimes use
if you do just a and just a colon like so.
This is a somewhat special case syntax.
What this means is that put all elements with A into
a single column vector
and this gives me a 9 by 1 vector.
They adjust the other ones are combined together.
Just a couple more examples. Let's see. Let's
say I set A to be equal to 123456, okay?
And let's say
I set a B to B
equal to 11, 12, 13, 14, 15, 16.
I can create a new
matrix C as A B.
This just means my
Matrix A. Here's my Matrix
B and I've set C
to be equal to AB.
What I'm doing is I'm taking
these two matrices and just concatenating onto each other.
So the left, matrix A on the left.
And I have the matrix B on the right.
And that's how I formed
this matrix C by putting them together.
I can also do C equals
A semicolon B. The semi
colon notation means that
I go put the next thing at the bottom.
So, I'll do is a
equals semicolon B. It also
puts the matrices A
and B together except that it
now puts them on top of each other.
so now I have A on top and B at the bottom and C here
is now in 6 by 2 matrix.
So, just say the semicolon
thing usually means, you know, go to the next line.
So, C is comprised by a
and then go to the bottom
of that and then put b
in the bottom and by the
way, this A B is
the same as A, B and
so you know, either of these gives you the same result.
So, with that, hopefully you
now know how to construct
matrices and hopefully starts
to show you some of the
commands that you use
to quickly put together matrices and
take matrices and, you know,
slam them together to form
bigger matrices, and with
just a few lines of code, Octave
is very convenient in terms
of how quickly we can assemble
complex matrices and move data around.
So that's it for moving data around.
In the next video we'll start
to talk about how to actually
do complex computations on this, on our data.
So, hopefully that gives you
a sense of how, with
just a few commands, you can
very quickly move data around in Octave.
You know, you load and save vectors and
matrices, load and save data,
put together matrices to create
bigger matrices, index into or select
specific elements on the matrices.
I know I went through a lot
of commands, so I think
the best thing for you to do
is afterward, to look
at the transcript of the things I was typing.
You know, look at it.
Look at the coursework site and download
the transcript of the session
from there and look through
the transcript and type some
of those commands into Octave yourself
and start to play with these commands and get it to work.
And obviously, you know, there's no point at all to try to memorize all these commands.
It's just, but what you
should do is, hopefully from
this video you have gotten a
sense of the sorts of things you can do.
So that when later on when
you are trying to program a learning
algorithms yourself, if you
are trying to find a specific
command that maybe you think
Octave can do because you think
you might have seen it here, you
should refer to the transcript
of the session and look through
that in order to find the commands you wanna use.
So, that's it for
moving data around and in
the next video what I'd like
to do is start to tell
you how to actually do
complex computations on our
data, and how to
compute on the data, and
actually start to implement learning algorithms.