declare

#31 Python Tutorial for Beginners | Working with Matrix in Python



Sharing buttons:

[Music]

welcome back aliens my name is Ivan

jetty and let's continue this is on

Python the till is fine we have talked

about numpy right but there's still one

question we have talked about numpy

three to four videos before and then the

reason we started with numpy is by

talking about multi dimensional array

and till this point we have not touched

it you know so that's the awesome thing

about numpy other than

multidimensionality we have so many

features right but in this video finally

we are talking about multi dimensional

array now what is multi dimensional so

we have sympathy right we have one

dimensional we have two dimensional we

can create three dimensional now with

that I want to perform certain

operations right so in this video we'll

talk about two dimensional and what what

operations you can perform here okay so

let's create our two dimensional array

okay so we got an array and then since

with the moment you say you want to

create multi dimensional array you need

to use this symbol right so this is one

array so this is one big array which you

have and inside this one big added you

want two different arrays and that's the

idea right if you have one array with

multiple elements that's one adding if

you have a big array inside that either

you have added itself that is multi

damage or two-dimensional array right so

this one this is one big added right and

in this one big added you have to mod

Ares so you will say we have two more a

days and each array will have certain

values I would say 1 comma 2 comma 3

right and the secondary will have 4

comma 5 comma 6 so what we have here is

we have two dimensional adding ok now

how do you know how do you know that

this is two dimensional so if you have

an array and if you want to check first

of all it splits print the array as it

is I would say ARR one run this code and

you can see we got an array so this is

the output so we got one big array

inside that we have two arrays so there

are certain functions you can apply here

the first function is called as D type

now using T type you can guess you can

know you will know what type of data you

are working with so at this point we are

working with paint we have another

keyword or the attribute you can say

which is ending now ending is dimension

which will give you the number of

dimensions and you can see the output it

is 2 so when you work with single

we'll give you one when you say three

dimensionally it will give you three

right so it will give you the number of

dimensions you have and plus it is also

called as ranking so it will write so it

is one Deanna is it 2d or 3d array now

we have one more which is called a shape

so I will say shape here now what shape

will give you as let's run this so shape

says 2 comma 3 drawn what it means it

will give you the number of rows and

columns so we have 2 rows and you have

three columns so it will give you tuple

with number of rows and number of

columns the other attribute we have a

size now using size it will tell you the

size of the entire block and you can you

can count it now so we have 3 and 3 to

the 6 so that's 1 and you can see we got

6 so there are other so there are

multiple meters you can use right in

fact we have one function which is

awesome example if I have an array which

is a or r2 and I want to create another

I want to create this added to with the

help of an array 1 but this should be

this should be 1d array ok so how can

you convert a 2d adding to 1 DL it's

very simple you say al L 1 dot there is

a function called as flatten so the

moment you say planner it will flag the

added right so from two dimensions you

will get one dimensional law and I went

to print of course the arr2 and if you

on this code you can see we got one

dimensional space so you can create from

multi-level single dimensional how about

from single dimension of

multi-dimensional because now this is so

era 2 is one dimensional right I want to

create three dimensional

how can we do that now first of all to

make it three dimensional I want to have

some more values of course then it will

work right that is a two again and nine

so we got six values here and let me

take six values zero as well

because 6 plus 6 is 12 and it is easy to

create a two-dimensional array when you

have this number of values so I will

create an a3 and this should be a

two-dimensional array from one damage

now I want to create a two-dimensional

array so you will say air are 2 dot the

function name is reshape so you can

convert a single dimensional array into

3d array ok but then what you to pass

you so when you say you want to be a 3d

array

you need to pass the number of so let's

let's try with two-dimensional first and

then we will go for three dimensional so

I would say three here I went to print

three now you have to pass

the number of rows and columns so i

would say i want three rows and the

number of columns will afford right so

we have three into four twelve so we

have three rows and four columns if I

run this code you can see from 1d Arabic

or 2d array so we got three toes and we

got four columns now it is also a Thule

because in an Eddie

so the outer and it has single

dimensional arrays right now what if you

have a big a day in which you have two

deities okay I know that's that's pretty

difficult to understand but let's try it

out

so I will say 2 comma 2 comma 3 so 2

comma 2 comma 3 is 12 right so 22 is 4 4

into 3 is 12 now what it will do is it

will create a 3d array ok big 3d a D and

that treaty array will have 2

dimensional array so it will be having 2

2 lemmas parity and each 2 dimensional

array will have 3 values or two-a-days

with three 3 values ok let me select

measure on this point for you so you can

see this is what I'm talking about it it

will give you a 3d array and this treaty

array will have to 2d Ally so you can

see this is one 2d array this is 2

second to the ADI and each 2d array will

have 2 and it's 2 and is added to 1d

array and then each one D Ally will have

D values that's what you're saying

we got to we got one big a D which has

two two-dimensional array which has to

do two single dimensionality and then we

charge three values okay so that's what

if you have to say and you've got the

output here so this is some function you

can apply to perform certain operations

on to the added now once we talked about

to rearrange some operations the main

thing about mathematics you know we have

a concept when you talk about arrays in

mathematics we have a concept of mitosis

I'm sure you are you have done that in

your college right now when you talk

about matrices what is my choice you can

imagine a two-dimensional array right so

we have multiple rows and multiple

columns now we can have one row and

multiple columns right way we call them

as low matrix right or we can have a

polymer matrix which has multiple roles

but single column but ultimately you can

represent that with one D added right so

even if you have one row one column you

can say that's one day what if you want

to have to do a metric matrix format you

know value for those multiple columns we

can do that here

in fact the array the other elevation

which we are working with this one this

is a 2d area right so I will just remove

all this stuff and I will put I want to

print one and I don't want all this

values here let's reduce it we can have

2 rows and 4 columns so if you have this

quote this is your matrix right but then

to actually work with metrics there are

certain operations we can perform so you

have to convert this to Deanna into a

matrix format and the way you can do

that is by saying matrix m is equal to

there is a function called as matrix and

in this matrix you have to pass a r1 so

you pass a 2d array it will give you a

matrix format let's run this code and

this is your matrix also it's not this

one let me print em so this is your

matrix I know the output looks similar

right output is almost same what is

difference is with this M you can

perform more operations the operations

which we do in matrices now this is not

the only way of putting a matrix example

if you don't have or two-dimensional if

you have a string with you maybe you're

you you are taking input from the user

in that case you will pass you will say

hey I got a value 1

I got a value - I got a value 3 and

value 6 now how can I differentiate

between two tools here you just have to

give a semicolon at the end and then I

canes have to say 4 comma 5 comma 6

comma 7 and even this works if I'll in

this code so you can see when this works

you don't need a separate a date to save

the value you can simply on this quote

an acquaintance a still works if you

want to create multiple rows I mean for

those and two columns you can just give

a semicolon after every two values so

this will convert it into for those and

two columns right so regard for those on

two columns so let me just do some

modification here because we want to

perform some operations here I would I

will say three rows and three columns so

you can see we got nine values we have

three rows and three columns if I run

this code this is what you've got right

now there's a concept of diagonal

elements what if you want to have

diagonal diagonal element here I don't

want to print all and the entire matrix

I just want to print the diagonal which

is 1 4 7 you can do that with them with

a function called as diagonal just say

diagonal and pass em and you're good to

go you can see we got all the diagonal

elements so this is fun so if you have

metrics you can perform this off

in fact not just this one but what if I

want to look let's try so I will say m

dot so there is a function called as min

so you know the answer right so when you

say min it means the minimum element so

in this code that is your one of course

and if you say max it will give you the

max element so the opposite which we do

with a DES can be done with mattresses

as well but with my Trice's you get this

amazing power now if you remember if you

have done with matrix before if you have

worked with matrix before we have this

concept of adding two matrices that is

very easy right you simply have to add

the elements the problem starts when you

want to multiply two matrices now if you

are thinking if you want to multiply two

matrices simply take the values from

here simply take the values from here

multiply that's not the case in

multiplication of matrices you have to

do some of those and column combinations

right if they remember matrices if you

have not done that before I would

suggest you to watch my video on how to

multiply two matrices so you will find

the link in description right here but

once that video if you are not done data

if you know how it works

how are you doing programming in fact if

you have done C C++ Java before you know

we have to use multiple loops to make it

work right so if you if I ask you to hey

you need to multiply two matrices go

with C programming you know it will take

entire one page that's not the case with

Python here

what Python says is if you have two

might Rice's that's well encode let's

create another matrix here let's call

this M as M one and regard this is M two

we can change the values of course I

will I will say this is 2 this is it

doesn't matter you can have the same

matrix or different one

I want to might apply this it's very

simple you curate m 3 equal to m1 into

m2 now this is not a simple

multiplication ok first let's try with

addition if this if that works and at

the end I will print m3 ok let's run

this code and you can see it works you

are adding two matrices it's it's

straightforward right you can just you

can just see the combinations there but

what if I say multiplication now this is

not a normal multiplication ok so if you

see the output it's not normal

multiplication it's not 1 into 1 it is

not 22 it is something different so this

is where you add you multiply Row with

column combinations right now this is

done automatically in matrices and

that's

that's the amazing thing about Python we

have so many stuff available right so it

suggests you do this manually also so as

assignment you can you can do matrix

multiplication but manually now how

would you know the logic of it so in the

description area there's all the video

in that I have talked about the theory

of it how to multiply two matrices just

look at that video and try to convert

that into algorithm and try to convert

that into a code in Python okay not

using this simple multiplication so

that's it that's how you might apply

matrices

let me know in the comment section if

you have any questions and if you are

enjoying this video let me know in the

comment section as well Duke I like but

I've been trying it thanks for watching

everyone bye

you