Matrices using the Numpy

Python Programming Introduction to numpy
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.09
List Price:  €93
You save:  €27.90
£56.02
List Price:  £80.04
You save:  £24.01
CA$96.21
List Price:  CA$137.46
You save:  CA$41.24
A$106.50
List Price:  A$152.16
You save:  A$45.65
S$94.88
List Price:  S$135.55
You save:  S$40.67
HK$547.08
List Price:  HK$781.57
You save:  HK$234.49
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr764.52
List Price:  NOK kr1,092.22
You save:  NOK kr327.70
DKK kr485.54
List Price:  DKK kr693.66
You save:  DKK kr208.12
NZ$116.87
List Price:  NZ$166.97
You save:  NZ$50.09
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,696.46
List Price:  ৳10,995.41
You save:  ৳3,298.95
₹5,845.02
List Price:  ₹8,350.38
You save:  ₹2,505.36
RM331.92
List Price:  RM474.20
You save:  RM142.27
₦98,297.45
List Price:  ₦140,430.95
You save:  ₦42,133.50
₨19,467.44
List Price:  ₨27,811.82
You save:  ₨8,344.38
฿2,586.11
List Price:  ฿3,694.61
You save:  ฿1,108.49
₺2,257.70
List Price:  ₺3,225.42
You save:  ₺967.72
B$355.19
List Price:  B$507.43
You save:  B$152.24
R1,300.44
List Price:  R1,857.85
You save:  R557.41
Лв127.53
List Price:  Лв182.19
You save:  Лв54.66
₩95,343.18
List Price:  ₩136,210.38
You save:  ₩40,867.20
₪259.39
List Price:  ₪370.57
You save:  ₪111.18
₱4,014.38
List Price:  ₱5,735.07
You save:  ₱1,720.69
¥10,876.02
List Price:  ¥15,537.84
You save:  ¥4,661.82
MX$1,183.66
List Price:  MX$1,691.01
You save:  MX$507.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P952.42
List Price:  P1,360.66
You save:  P408.23
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,329.50
List Price:  E£4,756.64
You save:  E£1,427.13
ብር3,992.74
List Price:  ብር5,704.16
You save:  ብር1,711.42
Kz58,583.02
List Price:  Kz83,693.62
You save:  Kz25,110.60
CLP$65,554.03
List Price:  CLP$93,652.63
You save:  CLP$28,098.60
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,071.34
List Price:  RD$5,816.45
You save:  RD$1,745.10
DA9,415.49
List Price:  DA13,451.28
You save:  DA4,035.78
FJ$159.21
List Price:  FJ$227.46
You save:  FJ$68.24
Q544.84
List Price:  Q778.38
You save:  Q233.53
GY$14,671.19
List Price:  GY$20,959.74
You save:  GY$6,288.55
ISK kr9,784.60
List Price:  ISK kr13,978.60
You save:  ISK kr4,194
DH703.12
List Price:  DH1,004.51
You save:  DH301.38
L1,240.96
List Price:  L1,772.88
You save:  L531.91
ден4,012.27
List Price:  ден5,732.06
You save:  ден1,719.79
MOP$564.93
List Price:  MOP$807.08
You save:  MOP$242.14
N$1,294.05
List Price:  N$1,848.73
You save:  N$554.67
C$2,581.34
List Price:  C$3,687.79
You save:  C$1,106.44
रु9,369.68
List Price:  रु13,385.83
You save:  रु4,016.15
S/261.57
List Price:  S/373.68
You save:  S/112.11
K271.48
List Price:  K387.85
You save:  K116.36
SAR262.48
List Price:  SAR374.99
You save:  SAR112.51
ZK1,900.47
List Price:  ZK2,715.07
You save:  ZK814.60
L323.94
List Price:  L462.80
You save:  L138.85
Kč1,630.83
List Price:  Kč2,329.86
You save:  Kč699.02
Ft25,346.53
List Price:  Ft36,210.88
You save:  Ft10,864.35
SEK kr763.26
List Price:  SEK kr1,090.42
You save:  SEK kr327.16
ARS$61,644.55
List Price:  ARS$88,067.42
You save:  ARS$26,422.86
Bs483.68
List Price:  Bs691.01
You save:  Bs207.32
COP$273,058.61
List Price:  COP$390,100.45
You save:  COP$117,041.84
₡35,861.91
List Price:  ₡51,233.50
You save:  ₡15,371.58
L1,732.92
List Price:  L2,475.71
You save:  L742.78
₲523,307.77
List Price:  ₲747,614.58
You save:  ₲224,306.80
$U2,685.15
List Price:  $U3,836.09
You save:  $U1,150.94
zł280.89
List Price:  zł401.29
You save:  zł120.40
Already have an account? Log In

Transcript

In this lesson, we'll look at the matrices which are basically two dimensional arrays. So in our previous lesson, we have completed the areas using the NumPy package. Now here also we'll be creating this matrices using using NumPy package. So let's just call the NumPy package first. So import NumPy as NP, run it, and now let's create a matrix. So let me give a name of the matrix as max underscore one, and then assign new values to the matrix.

So to create a matrix and to use NP dot airing, then within this first bracket if two square brackets and indeed the inner square bracket, right, the elements of the first row, so let's make it simple. So I'm just writing 123 the numeric values give a comma come down again, he was quite bracket right the limits of the second room. Again, give a comma here. Come down give a square bracket right there in front of the third row. Now just run it and now print print match one. And now run this see it has printed matrix which has three rows basically one two and three to the three cross three matrix.

So, as we can see, it returned three rows of Aires, we can define them as matrix because as we said, the matrix is a two dimensional and so basically is a matrix with three rows and three columns and it is a two dimensional array. So let's now understand it better when use the function which we have used in our previous lessons. So first time use the shape function. So just write mat underscore one dot shape and run it see it gives three comma tree that is it gives a number of elements in horizontal and vertical. So there are three animates and there are three elements over here. So, that is three rows and three columns.

So, it is giving three comma three. The next function that will use that method will use is the size. So Matt underscore one dot size, it is giving nine. So it is giving how many elements are there in total, so there are nine elements so it's giving nine now, that sec next method that I use is Matt underscore one dot ending, it will show you the dimension see it is a two dimensional figure, so it is giving two so now I will see how we can use the index in matrix. So let's just write Matt underscore one and within this square bracket, right one, comma two, let's see what it gives it gives six now from where this six I got look at the matrix, this one I extracted this element that is one comma two, which is the index Starting from 0012, so one is indicating the row number and two that I provided here is indicating the column number that is 012.

So one and two, so the intersection point is six, so it is extracted six. Now, if you want to extract nine then what you have to write so, Mac underscore one within the square bracket, write the row number first and the column number so, row number is two column number is also two so write two comma two, two comma two and run it, see you got nine. In this way you can get a particular element from your matrix by using this index function. Now, I can assign a value to my matrix by replacing any of this one suppose I want to replace this eight this eight and I replace it by a 16 So, here I have to write Matt underscore one then he will square bracket and now I have to mention this index. So, what will be this index 012 that is two will be the row index two and which column zero and one.

So, I have to write two comma one, two comma one and here I have to like equal to what value you want to assign instead 16th So, I like 16 over here no right it's done now, print this matrix and see whether the values have changed or not see the values have changed is now eight is now changed to 16. So, in this way you can also input new values or change assign a value to a matrix. Now, what we have done till now is we have selected a particular element from our matrix now, here we can also select a particular row from a metric that is a particular line from the matrix. Now, suppose I want to get this 30 Row, the total third row I want to get some water to write is Matt underscore one, and within this second bracket, square bracket, give the row index.

So if just to see what you got, you got the last row that is the third row. Now how to select an entire column here we have selected the entire row. Now I tell inventor column to Mac underscore one within this square bracket, I have to write first a colon, then give a comma and write two. So I'll extract the third column. So let's run it see it gives you the result 369369. So what we've done here is I wrote a column which indexes all the lines and then Ah, to indicate the third column, that is here, I give a column which will take all the rows Okay, and now here I have given a comma and then mentioned the column number.

So I get all the rows and a particular column. So now basically, the program stacks, all the nines means all the rows and within the two within the two, it takes only column two. So this is a process of setting up a column. Now if I want to extract the first two elements of the second row, that is this four and five and extract this and to get back this way, so what I have to do to take the first element of the second line, let's just first write the program then I make you understand. So Matt, score one within this square bracket, write one comma, then write zero colon two. So why I wrote like this.

So first, I wrote one to indicate the second line, this one is indicating the second line that is the second row and then for force to element we have to write zero is two that is starting from zero up to here and providing the length now run this so it extracted perfectly, four and five. So let's remember that here. Let's start by the index zero to the index two excluding the indexed we all know that the name that we provide the last element that is the last line that is zero to two will not be extracted just before two, it will extract to n minus one it will extract that is up to minus one you can extract it. So to index one it will extract, we have done it previously, I think you remember this. So, we have come to the end of this lesson.

So, here in this lesson, we have learned how to create matrix and how to use this index function to generate x print a specific element of a matrix or a specific row or a specific column of a matrix. Now, this is very important, when you will use a data frame then this particular function will be very helpful to you if you understand this. So, I think this is not that tough to understand it just need a little bit of practice. So, keep practicing. See you in the next video. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.