- Coding Videos/
- Python /
- Linear Algebra – NUMPY Python Tutorial

# Linear Algebra – NUMPY Python Tutorial

### Download Video link >

Linear Algebra – NUMPY Python Tutorial

### View Comments source >

### Transcript view all >

00:00 hey guys welcome to this numpy Python

00:02 tutorial and in this video we're gonna

00:04 learn about linear algebra so by the end

00:07 of the video we're gonna learn how to do

00:08 dot products and dump I going to learn

00:10 how to solve systems equation in numpy

00:13 and then we're gonna wrap it up with how

00:15 to find the inverse of a matrix in numpy

00:17 now this is just the beginning

00:19 essentially anything you can do in

00:21 linear algebra you can easily do an um

00:23 hi but this is just sort of to get you

00:25 started and to show you that the

00:27 possibilities are endless okay so first

00:30 of all let's let's go over how to do dot

00:32 products and numpy so to start we always

00:35 have to import our dependency in this

00:38 case it's important um pi and we give it

00:40 the alias and P and that is so that

00:43 every time we want to call on this

00:44 library we don't have to type numpy

00:46 because M here is a lot shorter than

00:47 numpy and then we're just going to

00:50 create some matrix we got matrix a and

00:52 the matrix B say we got numpy dot matrix

00:56 and then let's create a matrix we'll

00:58 just give it the value 1 2 3 and then

01:00 for matrix B we're gonna give it 11 12

01:02 13 just cuz you know they're simple

01:04 let's go ahead and print it out and to

01:06 see what happens

01:08 as you can see this is matrix 1 and then

01:11 this is matrix 2 ok great

01:14 so let's go ahead and do some vector dot

01:16 product so vector dot product is sort of

01:19 like dot product but on a one

01:20 dimensional matrix and these are kind of

01:23 like lists right so they're basically

01:24 one dimensional matrix whether it's you

01:27 know going across or whether it's up and

01:29 down one row or one column basically a

01:32 vector is like a 1 roll or a 1 column

01:35 matrix and in this case we got 2 1 roll

01:37 matrix so in order to calculate the

01:40 vector dot product we're just gonna use

01:42 numpy dot v dot my v dot stands for

01:46 vector dot and then we're gonna feed it

01:48 our matrix in this case we're gonna have

01:50 matrix a comma matrix B and then let's

01:53 go ahead and print it out and see what

01:55 happens after we sum it together or

01:57 sorry well after we dot product get

01:59 together and as you can see it returns a

02:01 single number right it returns a single

02:03 number okay so let's go ahead and

02:06 comment this out

02:08 and let's see what happens when we have

02:10 a matrix that is more than one dimension

02:13 so in this case we got matrix C and

02:15 matrix D so matrix C is specified as

02:18 such it is a to roll by three column

02:22 matrix and matrix D is a two column as a

02:27 two column by three row or we can think

02:29 of a three row two column right first

02:31 one we got roll one when we got roll -

02:34 and they got column one column two

02:36 column three and the second we got Row 1

02:38 Row two Row three and then we got column

02:41 one column two but basically you want to

02:43 make it so that the roll one is equal to

02:45 the column count of the other and that

02:49 way you won't get any kind of sort of a

02:50 error when you try to do any kind of

02:52 multiplication on it so here we can see

02:54 that I'm this is the first matrix it's a

02:56 to roll by three column and the second

02:59 one is three row by two columns okay

03:02 so now let's go ahead and do some dot

03:04 products on it so in this case we're

03:06 just gonna use a normal adopt product

03:08 right because this is not a vector it's

03:09 just a normal matrix so we're just gonna

03:11 do a normal matrix dot product so in

03:14 order to do that we're going to do num

03:15 high dot dot dot right so here's the

03:19 period kind of dot and then here's the

03:21 actual word dot and I'm gonna feed our

03:24 matrix in this case it's C and D and

03:26 we're gonna set it equal just I'm

03:28 variable in this case is called matrix

03:30 dot product and then we're going to just

03:32 go ahead and print out that variable and

03:33 as you can see this is the answer okay

03:36 so let's go ahead and print out the

03:38 shape of matrix C and matrix D since

03:42 they see why we got that answer right so

03:44 matrix C has a shape of two rows and

03:47 three columns and matrix D has a shape

03:50 of three rows and two columns right this

03:53 is matrix C this is matrix D right you

03:56 got two rows and three columns and then

03:59 we got three rows and two columns now

04:01 because um this thing here so we're

04:04 basically gonna have we're gonna inherit

04:06 a row count from the first matrix and

04:08 the column count from the other one and

04:11 that's why the answer is a two by two

04:13 right because of the first matrix has 2

04:15 rows so we're gonna inherit that value

04:17 and

04:18 the second rope matrix has two columns

04:22 right just here we got the second Matrix

04:24 is column 1 column 2 and so therefore

04:26 our output matrix is gonna have two rows

04:28 and two columns okay great

04:31 so now let's talk about how to solve

04:33 systems of equation in numpy so again we

04:37 have to import our dependency so let's

04:39 say we have something like some kind of

04:42 matrix and we're solving for x so we

04:45 have a system of equations where we're

04:47 representing it and matrix form so here

04:49 we got a matrix and then we multiply it

04:52 by some X to get B but we want to figure

04:55 out what the X is right so so we got a

04:57 times X is equal to B or ax and B are

05:01 all matrices so we want to figure out

05:03 what is the matrix X so how do we do

05:06 that well first of all let's go ahead

05:08 and create our matrix as you can see

05:10 matrix a is basically you know this one

05:14 and matrix B is this one and in order to

05:18 solve it what we just have to do is we

05:20 just have to go ahead and and basically

05:23 use created new matrix let's call it X

05:27 that way it sort of outlines we got a

05:29 here we got B here and I got X here okay

05:33 and then in order to sort of solve it we

05:35 just use a dots all right so we got

05:37 numpy

05:38 line' ALG so that that means linear

05:41 algebra right that's just lying linear

05:43 algebra right whether there's like a

05:45 shorthand so num hi dot linear algebra

05:48 solve and then we're gonna feed it a to

05:50 matrix a B right so a is like the one on

05:53 this side of the equation and B is like

05:55 the one on the other side of the

05:56 equation and when we feed a and B what

05:59 the dots solve does it's gonna solve for

06:01 X and it's gonna put that answer inside

06:03 this variable that we created to catch X

06:05 so let's go ahead and print out X so

06:08 this is X but but how do we know that I

06:10 did it correctly well it would do it

06:13 correctly if we took X which is what

06:15 they solved and if we're to actually

06:18 multiply by a then we should get beat

06:20 right so that's that's how we sort of

06:22 know if we got X correctly so let's get

06:24 rid of this print statement let's get

06:26 rid of

06:27 print a and if this thing is equal to B

06:32 then we know we did it correctly so as

06:35 you can see this is nine eight two seven

06:37 and six and this is basically nine eight

06:39 seven six but in floating-point but you

06:42 essentially essentially they're the same

06:43 thing so that's how we know we did it

06:46 correctly okay now finally let's go

06:48 ahead and talk about how to find the

06:50 inverse of a matrix so again we're gonna

06:53 import our dependency we're gonna create

06:56 a matrix called sub matrix and we're

06:58 gonna set it equals to num hide my

07:00 tricks one two three four okay so in

07:03 order to find the inverse what we want

07:05 to do is we want to do num hi dot linear

07:07 algebra dot inverse right and and these

07:10 are like sort of shorthand Lin all right

07:13 and then dot n right stands for linear

07:15 algebra inverse and we're just gonna go

07:18 ahead and feed it some matrix in this

07:20 case this is a matrix that we want to

07:22 find the inverse to right so so we feed

07:24 it some inverse and now we're going to

07:26 catch the output in a variable called

07:28 some matrix inverse okay so let's go

07:31 ahead and actually comment out these two

07:32 and let's just print some matrix inverse

07:35 just to see what that looks like and as

07:37 you can see this is a inverse matrix of

07:40 this right but but how do we know that

07:43 like how do we know that this is the

07:46 inverse matrix of this well as you know

07:48 we can just multiply some matrix against

07:52 some matrix inverse to see what that

07:54 gives us and as you can see it gives us

07:56 like listing which has like way too many

08:00 you know zeros and like to the 16th

08:03 power and stuff like that I just way too

08:06 many decimal points and it just doesn't

08:08 look really great so we can do instead

08:10 is we can do this thing so basically

08:14 like this block here is the same as this

08:17 block here however the difference is

08:19 that we're gonna we're gonna take this

08:20 whole entire thing and after we're done

08:22 with it we're gonna do a dot round we're

08:25 gonna dot round and we're going to have

08:26 zero decimal places so dot around zero

08:29 decimal places and it's going to look

08:30 like that and as you can see if we take

08:33 an in matrix and we multiply it by the

08:35 inverse if we got that that we know that

08:38 in fact whatever we multiply the matrix

08:41 against is the inverse many ways so

08:44 those are just some like really quick

08:45 and useful things you can do in terms of

08:47 linear algebra with numpy thanks for

08:49 watching

## Leave a Reply