Now that you know how to load
and save data in Octave, put
your data into matrices and so
on. In this video I'd like
to show you how to do computational
operations on data and
later on we'll be using this
sorts of computation operations to implement our learning algorithms.
Let's get started.
Here's my Octave window.
Let me just quickly initialize some
variables to use
for examples and set A
to be a 3 by 2 matrix.
and set B to a
3 by 2 matrix and let's
set C to a
2 by 2 matrix, like so.
Now, let's say I want to multiply 2 of my matrices.
So, let's say I wanna compute AxC.
I just type AxC.
So, it's a 3 by 2 matrix times a 2 by 2 matrix.
This gives me this 3 by 2 matrix.
You can also do elements wise
operations and do A.xB
and what this would do is
they'll take each elements of A
and multiply it by
the corresponding elements of B.
So, that's A, that's B, that's A.xB.
So, for example, the first element
gives 1 times 11 which gives 11.
The second element gives
2 x 12 which gives 24 and so on.
So it is the element
wise multiplication of two
matrices, and in general
the P rand tends to,
it's usually used, to denote
element wise operations in octave.
So, here's a matrix
A and I'll do A dot carry 2.
This gives me the multi,
the element wise squaring of
A, so 1 squared
is 1, 2 squared is 4 and so on.
Let's set V to a vector,
we'll set V as 123 as a column vector.
You can also do 1.
over V to do
the element wise reciprocal of
V so this gives me
one over one, one over two and one over three.
This works too for matrices so
one dot over A, gives me
that element wise inverse of
A. and once
again the P radians gives use
a clue that this is an elements wise operation.
To also do things like log
V This is an element wise
logarithm of, the
V, E to the
V, is the base E
exponentiation of these elements
of this is E, this is E
squared EQ, this is
V. And I
can also do apps V to
take the element wise absolute
value of V. So here,
V was all positive, abs, say
minus 1 to minus 3,
the element wise Absolute
value gives me back these
non-negative values and negative
V gives me the minus
of V. This is the same
as -1xV but usually
you just write negative V and
so that negative 1xV and what else can you do?
Here's another neat trick.
So Let's see.
Let's say I want to take V and increment each of these elements by 1.
Well, one way to do
it is by constructing a
3 by 1 vector
this all ones and adding that to V. So, they do that.
This increments V by for 123 to 234.
The way I did
that was length of V, is three.
So ones, length of
V by one, this is ones
of three by one.
So that's ones, three by one.
On the right and what I
did was B plus ones,
V by one, which is adding
this vector of all ones
to B. And so this increments
V by one.
And you, another simpler
way to do that is to type V+ one, right?
So that's V and
V+ one also means to
add one element wise to
each of my elements of V.
Now, let's talk about more operations.
So, here's my matrix A. If you want to write A transpose.
The way to do that is to write A prime.
That's the apostrophe symbol.
It's the left quote.
So, on your keyboard
you probably have a left
quote and a right quote.
So this is a at the
standard quotation mark is a,
what to say, a transpose
to excuse me the, you
know, a transpose of my
major and of course
a transpose if I transpose
that again then I should
get back my matrix A. Some more useful functions.
Let's say locate A is
1 15 to 0.5.
So, it's a, you know, 1 by 4 matrix.
Let's say set val equals max
of A. This returns the
maximum value of A, which
in this case is 15 and
I can do val ind max
A. And this returns
val of int which are
the maximum value of A
which is 15 as was the index.
So the elements number two of A that 15.
So, in is my index into this.
Just as a warning: if
you do max A where A is a matrix.
What this does is this actually
does the column wise maximum,
but say a little bit more about this in a second.
So, using this example of the
variable lowercase A. If I do A less than three.
This does the element wise operation.
Element wise comparison.
So, the first element
Of A is less than three equals to one.
Second elements of A is
not less than three, so
this value is zero, because it is also.
The third and fourth numbers of
A are the lesson,
I meant less than three, third and fourth elements are less than three.
So this is one, one, so
this is just the element wide
comparison of all four
element variable lower case
three and it returns true
or false depending on whether or not it's less than three.
Now, if I do find
A less than three, this would
tell me which are the
elements of A that the
variable A of less than three
and in this case the 1st, 3rd
and 4th elements are lesson three.
For my next example Oh, let
me set eight be code to
magic three. The magic
function returns. Let's type help magic. Functions called
The magic function returns.
Returns this matrices called magic squares.
They have this, you know,
mathematical property that all
of their rows and columns and
diagonals sum up to the same thing.
So, you know, it's
not actually useful for machine
learning as far as I
know, but I'm just using
this as a convenient way,
you know, to generate a 3
by 3 matrix and this magic square screen.
We have the power of 3
at each row, each column and
the diagonals all add up
to the same thing, so it's kind of a mathematical construct.
I use magic, I use this
magic function only when I'm
doing demos, or when I'm
teaching Octave like this and
I don't actually use it for
any, you know, useful machine learning application.
But, let's see, if I type RC equals find A greater than or equals 7.
This finds all the elements
of a that are greater than
and equals to 7 and
so, RC sense a row and column.
So, the 11 element is greater than 7.
The three two elements is
greater than 7 and the two 3 elements is greater than 7.
So let's see, the two, three
element for example, is A two, three.
Is seven, is this element
out here, and that is indeed greater than or equal seven.
By the way, I actually don't even
memorize myself what these
find functions do in the
all these things do myself and
whenever I use a find
function, sometimes I forget
myself exactly what does, and
you know, type help find to look up the document.
Okay, just two more things, if it's okay, to show you.
One is the sum function.
So here's my A and
I type sum A. This adds
up all the elements of A.
And if I want to multiply them
together, I type prod A.
Prod sense of product,
and it returns the products of
these four elements of A.
Floor A rounds down,
these elements of A, so zero
O point five gets rounded down to zero.
And ceil, or ceiling A,
gets rounded up, so zero
point five, rounded up to
the nearest integer, so zero point five gets rounded up to one.
You can also.
Let's see.
Let me type rand 3.
This generally sets a 3 by 3 matrix.
If I type max randd 3, rand 3.
What this does is it takes
the element wise maximum of
2 random 3 by 3 matrices.
So, you'll notice all these
numbers tend to be a bit on the
large side because each of
these is actually the max of
a randomly, of element Y's
max of two randomly generated matrices.
This is my magic number.
This was my magic square 3x3a.
Let's say I type max A and then this will be it.
Open, close, square brackets comma 1.
What this does is
this takes the column wise maximum.
So, the maximum of the
first column is eight, max
of the second column is nine,
the max of the third column is seven.
This 1 means to take the
max along the first dimension of
A. In contrast, if
I were to type max a, this
funny notation 2 then this
takes the per row maximum.
So, the maximum for the first
row is 8, max of
second row is 7, max
of the third row is 9
and so this allows you to take maxes.
You know, per row or per column.
And if you want to, and
remember it defaults to column
mark wise elements on this,
so if you want to find
the maximum element in
the entire matrix A, you
can type max of max
of A, like so, which is nine.
Or you can turn A into
a vector and type max
of A colon, like
so, this treats this as a vector
and takes the max element of vector.
Finally, let's set A
to be a nine by nine magic square.
So remember, the magic square
has this property that every
column in every row sums the same thing and also the diagonals.
So here is 9X9 magic square.
So let me just sum A one
so this does a per column sum.
And so I'm going to take each
column of A and add
them up and this, you
know, lets us verify that indeed
for 9 by 9 magic square.
Every column adds up to 369 as of the same thing.
Now, let's do the row wise sum.
So, the sum A comma 2
and this sums
up each row of A
and each row of A
also sums up to 369.
Now let's sum the
diagonal elements of A
and make sure that they, that
that also sums up to the same thing.
So what I'm going to
do is, construct a nine
by nine identity matrix, that's
I9, and let me
take A and construct, multiply
A elements wise.
So here's my matrix of A.
I'm gonna do A.xI9 and what
this will do is take the
element wise product of these
2 matrices, and so this
should wipe out everything except
for the diagonal entries and now
I'm going to sum, sum of
A of that and this
gives me the sum of
these diagonal elements, and indeed it is 369.
You can sum up the other diagonal as well.
So this top left to bottom right.
You can sum up the opposite diagonal
from bottom left to top right.
The sum, the commands for this is somewhat more cryptic.
You don't really need to know this.
I'm just showing you just in
case any of you are curious,
but let's see.
Flip UD stands for flip up/down.
If you do that, that turns out
to sum up the
elements in the opposites of,
the other diagonal that also sums up to 369.
Here, let me show you,
whereas i9 is this
matrix, flip up/down of
i9, you know, takes the identity
matrix and flips it vertically
so you end up with, excuse me,
flip UD, end up
with ones on this opposite diagonal as well.
Just one last command and then
that's it, and then that will be it for this video.
Let's say A to be the
3x3 magic square
again. If you want
to invert the matrix, you
type P inv A, this
is typically called a pseudo inference, but it doesn't matter.
Think of it as basically the inverse
of A and that's the
inverse of A and second
set, you know, 10 equals p
of A and of temp times
A. This is indeed the
identity matrix with essentially ones
on the diagonals and zeros on
the off-diagonals, up to a numerical round-off.
So, that's it for how
to do different computational operations
on the data in matrices.
And after running a
learning algorithm, often one of
the most useful things is to
be able to look at your
results, or to plot, or visualize your result.
And in the next video I'm
going to very quickly show you
how, again, with one or
two lines of code using Octave
you can quickly visualize your
data, or plot your data
and use that to better
understand, you know, what your learning algorithms are doing.