Hello, my dear friend.
So in this lesson, we're going to talk about linear algebra with numpty and why we need to know it.
So let's get started.
So today, machine learning and deep learning models are data hungry, you could say the performance
is highly dependent on the amount of data that's we tend to collect as much data as possible in order
to build a robust and accurate model.
Right.
That is collected in many different formats, from numbers to images, from tags to sound waves.
However, we need a process to convert the data to numbers in order to analyze and then model it.
It's just not enough to convert data to scalars or single numbers.
As the amount of data increases, the operation's done with scale start to be inefficient, so we need
vectorized or matrix operations in order to make computations efficiently.
That's where linear algebra comes into play.
So linear algebra is one of the most important topics, however, today in the data science domain.
So in this lesson, we're going to cover the basic concepts in linear algebra with examples using numpty.
So there are a few different types of objects or structures in linear algebra.
Skylar, single, no vector array of numbers, matrix two dimensional array of numbers and Tensas and
dimensional array of numbers where N is greater than to make sense.
So a scaler is just a no, it can be used in vectorized operations is we're going to see in the following
examples.
A vector is an array of numbers, so basically a vector is a list of numbers.
There are at least two ways to interpret what this list of numbers mean, one way is you can think of
a vector as being a point in space.
Then this list of numbers is a way of identifying that point in space.
Where each number represents the vector's component.
Of that dimension.
OK, so for better understanding, let's let's see some examples.
So first we'll import our number library import numpty as ENPI.
Now our number is ready, so let's define an array he named Vector, underscore one and give it some
elements.
Vector underscore one equals Expeditor Ray.
Open princess, open bracket one, two, three, four, five, six, seven, eight, nine, ten, close
bracket close.
Princess Voeckler underscore one.
All right, so we have a vector now, we can use scalars and vectorized operations, so the specified
operation is done on each element of the vector and scalar.
So let me show you what I mean.
Back to underscore one times two.
And here's the result, so I assume you already remember this process from our previous lessons, it
it's a simple multiplication process with a no brainer, right?
You could do it in your sleep.
Yes.
So, Victor, underscore one plus five.
And there's a reason.
This one, of course, is called vector addition seems so simple, right?
So we had a scalar value for our vector also.
We can add between two different factors.
So let's define another vector named Vector underscore to.
So Victor underscore two equals and put down a ray.
Open parenthesis, open bracket two one three four, five, seven, eight, six, 10, nine, make sure
all those commas are in place.
Close bracket, close, princess.
Vector underscore to.
So now let's add them together.
Victor, underscore one plus factor, underscore to.
All right, so here it is, is the formula for vector addition in linear algebra.
And here is the schema of this process.
And here is the formula for vector multiplication and linear algebra.
All right.
Far out.
So what about Matrixx?
Matrix, as you may know, is a two dimensional array.
Sorry, sorry, I meant two dimensional vector.
OK, so matrix score one equals and beat out random dot rande and.
Open princes 50 come a size equals open parenthesis, five come a five, close princess, close princess.
All right, so now you have a matrix here and let's see if we can do the same things.
For example, at a scalar value matrix, underscore one plus five.
There's the result every element of this matrix increased by five.
So let's try to multiply by three matrix to underscore one times three.
There's the result.
Matrix addition or subtraction is really similar to what we did with vectors earlier, the only difference
is that there are multiple columns instead of just one.
So the whole idea remains the same.
We just need to add up the corresponding components.
So in the general formula, I've used A and B for placeholders, and we can see how each component is
added up.
And the concepts are more or less the same as with Vector's.
Every number in the Matrix will be multiplied with some scalar and.
The formula is also very similar.
Excellent, excellent.
So our next term is tenser, so a Tenzer is an N dimensional array of numbers were N is greater than
two.
So tenters are mostly using deep learning models where the input data is three dimensional.
Tensas underscore one equals Nupedia random dot Randi and.
Open princess 50 comma size equals.
Open parenthesis three comma, three, comma, five, close princess closed princes tenter underscore
one.
And there's the result, it is a beautiful Tensas.
Now, if you're interested in deep learning.
You will cross paths with it in the future.
I have foreseen it.
All right, so.
It's sort of easy to represent with numbers, but.
Think of Tensas underscore one as three matrices with a shape of three times five.
All right, so let's let's have a look and check the shape of our variables.
Prend.
In Prince's vector, underscore one shape close Francis.
Prend.
Open parenthesis matrix, underscore one, don't shave close, princess.
Brent.
Open parenthesis tends to underscore one shape, close princes.
And there's your result, so why don't we check the size of them?
Print open parenthesis, Victor, underscore one size, close parentheses.
Print open parentheses matrixx, underscore one down close Prince's print open princes Tenzer underscore
one that size close princes and there's a result.
OK, so now.
Well, there's no other way to put it, but things become more and more complicated.
So just watch carefully now.
We are in some undiscovered country.
So I know you're waiting for some functions, so now I'll present to you our first function.
Its name is Wait for it, Dot.
So this function returns the product of two arrays, so for 2D vectors, it's the equivalent to matrix
multiplication.
For one to erase, it's the inner product of the vector's.
For n dimensional race, it is some product over the last acces of A and the second last axis of B.
So to calculate the DOT product of two vectors, we first need to multiply the corresponding elements.
In other words, x1 by Y one, x2 by Y two and so on and so forth, and then some the product terms.
So this concept is, I think, a whole lot easier to understand when you see the general formula, so
ba ba ba ba.
Here it is.
So let's try it with our vectors vector underscore one dot dot.
Open parenthesis vector underscore to close princess.
OK, and there's the result.
So basically the DOT product of two vectors is the sum of the products of elements with regard to their
position.
The first element of the first vector is multiplied by the first element of the second vector and so
on.
And there's some of these products is the product.
Now, there is one important point here, as if they're all not important, but since we multiply elements
at the same position, the two vectors must have the same length in order to have a product.
Now, the field of data science, we mostly deal with matrices.
A matrix is a bunch of and column vectors combined in a structured way.
The multiplication of two matrices involves many DOT product operations vectors.
Yeah.
Now, it's going to be a lot more clear when we go over some examples.
This time, we'll use more simple matrices.
Matrix underscore a equals and beat out random, doctorand and.
Open parenthesis 10 comma size equals open parenthesis two comma to close Francisco's close Princess
Matrix underscore a.
All right, so that's our first matrix and it's ready, so let's define the other one, but this time
let's define it with one just in another way.
So Matrixx underscore B equals NPR dot matrix.
Open parenthesis in random dot, rande end.
Open parenthesis 10 comma size equals.
Open parenthesis to commit to close friends is close, princess.
Matrix underscore B.
And the result is what you see, so our second matrix is ready now.
So Matrix a two x two matrix has two rows and two columns.
Index of rows and columns start with zero, for instance, the first row of Matrix underscore a.
A row with index zero is the area, so the first column of Matrix A. is the array of right.
The element at the first row and the first column is here.
And we can access individual rows, columns or elements as follows Matrix underscore a open bracket,
zero zero closed bracket.
And the result is what you see.
Matrix underscore a open bracket, zero close bracket.
The result is right here.
Matrix underscore a.
Open bracket, colon, comma, zero closed bracket, and there's that result.
So I know you're familiar with these processes, we already learn them in our previous lessons.
So let's continue and see what else we can do.
The multiplication of two matrices involves products between rows of first matrix and columns of second
matrix.
So the first step is the DOT product between the first row of Matrix A. and the first column of Matrix
B.
The result of this product is the element of the resulting matrix apposition zero zero.
So the resulting Matrix C will have this.
All right, so the next step is a product of the first row of Matrix A in the second column of Matrix
B.
Matrix C will have this and this.
And the first row and the second karma matrix C is here.
First row of Matrix A. is complete, so we can start on the second row of Matrix and follow the same
steps.
Matrix C will have this and this and the second row and first column, Matrix C is here.
Final step is the DOT product between the second row of Matrix and the second column of Matrix B.
Matrix C will have this and this.
And the second row on the second ColorMatrix Sea is here.
All right, so now we've seen how it's done step by step, all of these operations are done with an
NPR dot dot operation.
So the formula, the process is like this.
Basically, we can say matrix multiplication is performed by calculating the DOT product of the corresponding
row of Matrix A and the corresponding column of Matrix B.
All right, so we've got that out the way.
Let's go a bit deeper, so try to talk about some common matrix terms.
So a matrix is called a square.
If number of rows is equal to the number of columns.
Right.
A Matrix and Matrix.
B, yeah, they're square matrices matrix, say.
Matrix B.
Identity matrix denoted as I.
Is a square matrix that has one on the diagonal and zeros at all the other positions, so identity function
of numpty can be used to create identity, matrices of any size and identity.
Open princess for closed princess, and there's your result.
Now, what makes an identity matrix special is that it does not change a matrix when multiplied, so
in the sense it's similar to number one in real numbers.
We're going to do some examples with identity matrix and matrix multiplication to worry about that.
The inverse of a matrix is the matrix that gives the identity matrix when multiplied with the original
matrix.
And not every matrix has an inverse.
So Matrix A has an inverse, then it is called in vertical or non singular.
All right.
Yeah, why don't we look at some examples?
So let's define a matrix named Matrix, underscore the.
So Matrixx underscore Deek rules and beat out random that random and.
Open parenthesis 10 comma size equals open princess for comma forclose Princess Matrixx, Underscore
D..
All right, so there's the result, our Matrix D is also a square.
So let's continue and p dot dot open princess matrix underscore d come and dot identity.
Open princess for closed princess, there's a result.
Now, remember, we've mentioned that when a matrix is multiplied by its inverse, the result is the
identity matrix.
So let's first create a matrix and find its inverse for this, we can use Lin algae that I envy.
So it's a function of numpty that will find the inverse of a matrix.
So let's define another matrix named Matrix E.
All right.
Matrix underscore equals NP, Dot Lin, ALG Dot I and V.
Open Princess Matrixx Underscore Decodes Princess.
Matrix underscore E.
Now, let's multiply Matrix deep with its inverse matrix e.
NPR dot.
Open parenthesis matrix underscored the karma matrix underscore e close princess.
Cool.
So there's a real.
And we use random elements as well, so sometimes we'll get identity matrices and sometimes we won't.
So don't worry if you don't see that and he didn't do a mistake.
I mean, even I try to do it as many times so that we can get an identity matrix result.
So let's just move on, you know, see what I mean?
Now, as you may recall, from vector dot products, two vectors must have the same length in order
to have a DOT product, each DOT product operation and matrix multiplication must follow this rule.
All right.
So DOT products are done between the rows of the first matrix and the columns of the second matrix.
That way, the rules of the first matrix and the columns of the second Matrix have to be the same length.
So let's see what I mean by an example.
Specifying two different matrices, and their names are Matrix F, Matrix G, so matrix underscore F
equals airspeed out random doctorand int.
Open Princess 10 Comis size equals open princess three, comma, four, closed parenthesis, close parenthesis.
Matrix underscore F.
So that's the first one and let's define the second matrix, underscore G equals and put out random
Rande in.
Open parenthesis 10 comma size equals.
Open princess for comma three, close Frances, close Frances.
All right, so our images are ready, so let's continue.
NPR dot dot opened Princess Matrixx, underscore f, comma, matrixx, underscore g close princess and
there's a result.
So the shape of the resulting matrix will be three by three, because we are doing three dot product
operations for each row of Matrix F and of course has three rows.
Now, an easy way to determine the shape of the resulting matrix is to take the number of rows from
the first one and the number of columns from the second one.
All right.
So let's try this.
And P dot dot open Princess Matrixx underscore g.
Comma, Matrixx, underscore our close princes and there's a result.
This time, the shape of our matrix is four by four, because Matrix G has four rows.
So let's look at another function, and its name is Vedat.
So his main task is the same as the DOT product, but it's got a tricky part, so it works with two
vectors.
If the first argument is complex, then it's conjugate, is used for calculation.
And if the argument ideas multidimensional, let's say it's a multidimensional array, it gets flat.
All right.
So let's try.
First, let's define a vector vector, underscore three equals and put out random random end.
Open parentheses, one comma, 15, comma, four, close parenthesis, and there's the result.
So let's use our function.
NPR dot v dot.
Open parenthesis vector underscore three comma matrix underscore, a close friend says.
And there's that result, so we flatten our matrix A and it becomes a one dimensional array.
I mean vector well so let's continue.
Our next function is Matt Mulle.
All right, the no, not Matt Mulle is a function that returns the Matrix product of to erase while
it returns in normal product for 2D arrays.
If dimensions of either argument is greater than two, it is treated as a stack of matrices residing
in the last two indexes, and it's broadcast accordingly.
I suppose it's really matrices and indices, but you know what I mean.
So on the other hand, if either argument is a one Deira.
It is promoted to a matrix by appending a one to its dimension.
And that's going to be removed after multiplication.
So you want to see what I mean in an example?
So first, let's check our Matrix again, matrix to underscore A Matrix.
Underscore B.
Now, let's try so enpi not Matt Mulle.
Open parenthesis matrix underscore a common matrix.
Underscore be closed, princess.
And look at the results, so it's basically a multiplication process and also it's a lot like the DOT
function, but.
It's got some differences right from DOT.
Well, at first, Matt Moule can't multiply with scalars.
And most important difference is on a one dimensional array, so stacks of matrices are broadcast together
as if the matrices were elements.
Dot and Matt Moule methods behave differently when passed to 3D or higher dimensional arrays.
Wow, that's pretty cool.
So the last thing in this lesson is determinant.
So what is determinate?
Well, according to Wikipedia.
The determinant is a scalar value that can be computed from the elements of a square matrix, and it'll
encode certain properties of the linear transformation described by the Matrix.
The determinant of matrix is denoted in a couple of different ways you can say that.
Open princess, a cause, princess, make sure there's capitally.
Debt, capital or pipe, capital, a pipe.
Cool.
So geometrically, it can be viewed as the volume scaling factor of the linear transformation described
by The Matrix.
The calculation process is pretty simple for two by two matrix, so it gets a lot more difficult for
a three by three and it really shouldn't be computed by hand for anything larger than that.
I mean, you can if you want to, but why bother, right?
I mean, the goal here is to develop the intuition.
Yeah, but computers were made to do the calculations.
All right, so you want to see how.
First will define a new matrix matrix underscored debt equals.
NPR dot matrix.
Open parenthesis and put out random random end.
Open parenthesis 10 comma size equals.
Open parenthesis to commit to close parenthesis, close parenthesis, close parenthesis matrix, underscore
debt.
Right, so that's our matrix.
Now let's use our function.
NPR dot.
L i n a l g dot deti.
Open parenthesis matrix, underscore debt, close parenthesis.
And there's the result.
So let's have a look at the formula for calculating the determinant of a two by two matrix.
Ah, man, it is so simple with no, isn't it?
So we've covered some basics, but certainly fundamental operations of linear algebra.
So these basic operations are, well, the building blocks of complex machine learning and deep learning
models.
Lots of matrix multiplication operations are done during the optimization process of models.
It's highly important to understand the basics as well and really well.
All right, so that's it, I hope you enjoyed it and I hope to see in our next lesson until then, have
a great day.