0:00

In the 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 from machine

Â learning problem, how you load that data into 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 that we generate, right,

Â with this command equals 1, 2, 3, 4, 5, 6 and the this is a 3 by 2

Â matrix. The size command in Octave lets you tells

Â you what is the size of the matrix, so size A returns 3, 2, it turns out the

Â size command itself is actually returning a 1 by 2 matrix. So you can actually set

Â SZ equals size of A and SZ is now a 1 by 2 matrix where the first element of this

Â is 3 and the second element of this is 2. So you can just type size of SZ SZ as a 1

Â by 2 matrix whose two elements contain the dimensions of the matrix A.

Â You can also type size(A,1) to give you back the first dimension of A, of,

Â size of the first dimension of A, so that's the number of rows and size(A,2)

Â to give you back 2 which is the number of columns in the matrix A.

Â If you have a vector V, so let's say V equals 1, 2, 3, 4, and you type length V,

Â what this does is it gives you the sides of the longest dimension.

Â So you can also type length A and because A is a 3 by 2 matrix, the longer

Â dimension is of size 3, so this should printout 3.

Â But usually we apply length only to vectors, so your length 1, 2, 3, 4, 5

Â rather than apply length to matrices, because that's, that's a little more

Â confusing. Now, let's look at how to load data and, find

Â data on a file system. when we start up Octave, we're usually,

Â we're often in a path that is near the location of where the Octave program is.

Â So the pwd command shows the current directory or the current path that Octave

Â is in. So right now, we're in this somewhat, maybe somewhat obscure

Â directory, the cd command that stands for change directory, so I can go to

Â c:\User\ang\Desktop and now in my desktop.

Â And if I type ls ls is a type that comes from a Unix for a Linux command, but ls

Â will list the directories on my desktop. And so, you know, these are the files

Â that are on my desktop right now. In fact, on my desktop are two files,

Â featuresX and priceY, that's maybe come from a machine learning problem were

Â going to solve. So, here's my desktop, here's featuresX.

Â And featuresX 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 47

Â rows in this data sets and so the first house had size 2104 square feet, has

Â three bedrooms, second house has 1600 square feet, has three bedrooms and so on

Â and priceY is this file that has the prices of the data in my training set. So

Â [SOUND] featuresX and priceY are just text files with my data.

Â How do I load this data into Octave? Well, I just, type load featuresX.dat and

Â if I do that and load the featuresX X, I can load priceY.dat.

Â And, by the way, there are multiple ways to do this,

Â this command, if you put featuresX.dat in strings and load it like so.

Â This is oops, typo there. This is a equivalent command so you can

Â you know, but this way, I'm, I'm just putting the

Â file name of the string in the file name in, in a string and in Octave use single

Â quotes to you know, to represent strings like so.

Â So that's a string, and I 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 what are the variables that Octaves has in memory currently,

Â featuresX and priceY are among them, as well as the variables that,

Â you know, we created earlier in this session.

Â So I can type featuresX to display featuresX and there's my data.

Â And I can type size featuresX and that's my 47 by 2 matrix. And sum of the size

Â priceY, that gives me my 47 by 1 vector for, this

Â is a 47-dimensional vector, this tall column vector that has all the

Â pricesY in my training set. Now, the who function shows you what are

Â the variables that in the current workspace.

Â There's also the whos variable that gives you the detail 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 3 by 2 matrix and featuresX is

Â a 47 by 2 matrix price, priceY is a 47 by 1 matrix, meaning this is just a vector

Â and it shows, you know, how many bites of memory it's taking up as well as what

Â type of data this is. Double means double precision, 48 points,

Â so that just means these are real values of floating point numbers.

Â Now, if you want to get rid of a variable, you can use the clear command.

Â So clear featuresX and type whos again, you notice that the featuresX variable

Â has now disappeared. And how do we save data? Let's see, let's

Â take the variable V and set it to priceY(1:10), this sets V to be the first

Â ten elements of of the vector Y. So let's type who or whose,

Â whereas Y was a 47 by 1 vector. V is now 10 by 1,

Â because Vprice equals priceY(1:10), this sets into the, just the first ten

Â elements of Y. Let's say I want to save this to data, to

Â disc. The command, save hello.matv,

Â 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, hello.mat I happened to have MATLAB

Â installed in this Windows, which is why, you know, this this icon looks like this,

Â because Windows is recognizing this is 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, if I, if you type clear without anything,

Â then this actually deletes all the variables on your workspace. so I type

Â whos, there's now nothing left in my workspace.

Â And if I load hello.mat, I can now load back my variable V, which

Â is my the, the data that I previously saved

Â into the hello.mat file. So, hello.mat or what, what we did just

Â now, the save hello.mat to V, this saves the data in the binary format

Â in a somewhat more compressed binary format.

Â So that if V is a lot of data, this, you know, will be somewhat more compressed

Â 8:05

and will take up less of the space. And 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 text, or as ASCII formatted text. And now, once I've done that, I have this

Â file, hello.txt is just appeared on my desktop.

Â And if I open this up, you see that this is a text file with my, 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 equal to that matrix again. So there's my 3 by 2 matrix.

Â Let's start by indexing, so if I type A(3,2), this indexes into

Â the three comma two element of the matrix A.

Â So this is what a, this is, you know, normally we would write this as A

Â subscript 3, 2 or A subscript, you know, 3,

Â 2. And so, that's the element in the third

Â row and second column of A, which is the element 6.

Â And I can also type A(2, ): to fetch everything in the second row. So the

Â colon means every element along that row or column.

Â So, A(2, ): is this second row of A. Right?

Â And similarly, if I do A(:, 2), then this means get everything in the second column

Â of A. So, this gives me 2, 4, 6 by this means

Â of A, everything comma second column,

Â so this is my second column of A which is 2, 4, 6.

Â Now, you can also use somewhat most sophisticated indexing operations. So,

Â so, I'll just quickly show you an example.

Â You, you do this maybe less often, but let me do A([1 3], ),: this means, get

Â all the elements of A, whose first index is 1 or 3.

Â This means 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], ): means get everything from the

Â first row and from the second row and, and from the third row.

Â And colon means, you know, I want both the first and the second columns and so

Â this gives me this 1, 2, 5, 6. Although you, you, use these sorts of

Â more sophisticated indexing operations, maybe somewhat less often.

Â To show you what else we can do. Here's the A matrix,

Â and let's, this was a colon comma to give me the second column.

Â And you can also use this to do assignments.

Â So I can take the second column of A and assign that to ten, eleven, twelve.

Â And if I do that, I'm now, you know, taking the second column with 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, it's A

Â let's set A, that's A to be equal to A comma, 100, 101, 102, like so, and, what

Â this will do, is it appends another column n vector to the right.

Â So now oops, I think I made a mistake. Should I put semicolons there?

Â And now, A is equal to this. Okay? I hope that makes sense.

Â So this 100, 101, 102, this is a column vector.

Â And what we did was, we said AA 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 this, these six elements on the left.

Â So we took the matrix A and we appended another column vector to the right, which

Â is now Y, now A is a 3 by 3 matrix that looks like that.

Â And finally, when we click that I sometimes use do A and then just a colon

Â like so, this is a somewhat special case syntax.

Â what this means is that, puts all elements of A into a single column vector

Â and this gives me a 9 by 1 vector that just has all the algorithms of A

Â [INAUDIBLE] together. just, a couple more examples.

Â I can also, let's see.

Â Let's say I set A to be equal to 1, 2, 3, 4, 5, 6, again. And, let's see I

Â said B to be equal to 11, 12, 13, 14, 15, 16.

Â I can create a new matrix C as [A B] and this just means, so here's my matrix A,

Â here's my matrix B, and off a set C to be equal to AB.

Â What I'm doing is I'm taking these two matrices and just concatenating them onto

Â each other. And so the left, [INAUDIBLE] matrix A on

Â the left. And I have the matrix B on the right.

Â And that's how I form this, you know? this matrix C, by putting them together.

Â I can also do C = [A; B]. The semi colon notation means that means,

Â it, means go put the next thing at the bottom. So we do sequence A sem colons B

Â it also puts the matrices A and B together, except that it now puts them on

Â top of each other, so now you have A on top and B at the bottom and C here, is

Â now a 6 by 2 matrix. So, so just saying, the semi colon thing

Â usually means you go to the next line. So C is comprised by A and then go to the

Â bottom of that and then put B, the bottom and by the way, this AB 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 this

Â starts to show you some of the commands that you can use to quickly put together

Â matrices and take matrices and, you know, slam them together to form big, 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 talk about how to actually do complex computations on

Â this, on, on, 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, load and save vectors and matrices or load and save data put

Â together matrices create bigger matrices indexed into or select specific elements

Â of the matrices. I know I went through a lot of commands,

Â so I think the best thing for you to do is afterwards to look at the transcript

Â of the things I was typing, you know, look at the, look at the

Â coursework sign and download the transcript of the session from there.

Â And look for the transcript and type some of those commands into Octave yourself,

Â so that you can 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're trying to program a learning algorithms system yourself.

Â If you are trying to find a specific amount that maybe you think Octave can do

Â because you think you might see it here you should refer to the, to the

Â transcript of the session and look through that in order to find the

Â commands you want to use. So, that's it for moving data around and

Â in the next video, what I'd l like to do is start to tell you how to actually do

Â complex computations on our data and how to compute some data and how to actually

Â start to implement learning algorithms.

Â