1 00:00:00,420 --> 00:00:06,930 Now that we've covered reshaping and transposing you might be thinking when would we ever want to use 2 00:00:06,930 --> 00:00:08,400 a transpose. 3 00:00:08,400 --> 00:00:10,320 It just flips the axis is around. 4 00:00:10,380 --> 00:00:12,300 When is that useful. 5 00:00:12,300 --> 00:00:14,980 Well that's what we're going to cover in this video. 6 00:00:15,150 --> 00:00:21,960 And one of the main use cases for transposing a matrix is the dot product. 7 00:00:22,040 --> 00:00:24,080 It might be thinking now dot product. 8 00:00:24,150 --> 00:00:25,320 What the hell is a dot product. 9 00:00:25,800 --> 00:00:27,030 Well let's do what we do. 10 00:00:27,030 --> 00:00:28,910 Dot product name pi. 11 00:00:28,920 --> 00:00:32,000 Let's have a research because what we're practicing. 12 00:00:32,010 --> 00:00:32,710 Here we go. 13 00:00:32,700 --> 00:00:37,880 Name pi dot dot product of two arrays. 14 00:00:37,990 --> 00:00:40,340 Oh that's not very helpful. 15 00:00:40,340 --> 00:00:46,290 Sometimes you get that then docs it'll give you a very brief explanation of what happens specifically 16 00:00:46,320 --> 00:00:48,300 if both A and B a one day arise. 17 00:00:48,330 --> 00:00:50,910 It is the inner product of vectors. 18 00:00:50,910 --> 00:00:51,610 Okay. 19 00:00:51,720 --> 00:00:57,820 There's a few text instructions here of what dot product actually is but let's focus on the code. 20 00:00:57,850 --> 00:01:03,990 I'll go back to our notebook we'll write some code first and then we'll have a little look into what 21 00:01:03,990 --> 00:01:06,260 the DOT PRODUCT actually does. 22 00:01:06,270 --> 00:01:11,940 The thing you need to remember the dot product it's just another way of finding patterns between two 23 00:01:11,940 --> 00:01:13,750 different sets of numbers. 24 00:01:13,750 --> 00:01:20,730 So let's start off with the random seed will create two more matrices that we can use for an example. 25 00:01:20,730 --> 00:01:29,670 I want this one to be Rand and let's make it of size 5 and 3 and we want map to beautiful. 26 00:01:29,670 --> 00:01:30,960 We'll just make it the same thing. 27 00:01:31,020 --> 00:01:34,180 Random dot Rand int 10. 28 00:01:34,410 --> 00:01:37,340 Size equals 5 3. 29 00:01:37,440 --> 00:01:39,150 And now we've seen Rand in before. 30 00:01:39,150 --> 00:01:41,630 Do you know what these two lines of code will do. 31 00:01:43,860 --> 00:01:45,170 So let's have a look. 32 00:01:45,210 --> 00:01:50,210 As a reminder we can do a shift tab return random in is from low to high. 33 00:01:50,220 --> 00:01:51,670 Beautiful. 34 00:01:51,750 --> 00:01:53,350 Let's see these in action. 35 00:01:53,400 --> 00:02:01,140 MATT one wonderful Matt to even more wonderful. 36 00:02:01,140 --> 00:02:06,320 And then we also want to say we already know the shape but let's just do it anyway. 37 00:02:06,330 --> 00:02:08,070 So we've got the shapes here. 38 00:02:08,220 --> 00:02:12,410 Matt to shape wonderful. 39 00:02:12,570 --> 00:02:19,360 So in a previous video we've seen a few different ways of performing arithmetic on matrices like this. 40 00:02:19,560 --> 00:02:22,320 One of the ways that we did was multiplication. 41 00:02:22,500 --> 00:02:22,920 So we go. 42 00:02:22,920 --> 00:02:24,080 Matt one Times Matt. 43 00:02:24,090 --> 00:02:26,620 Two. 44 00:02:26,740 --> 00:02:32,110 Let's bring these down so we can see them because I want to stay nice and zoomed in to keep the code 45 00:02:32,140 --> 00:02:33,090 big. 46 00:02:33,100 --> 00:02:33,640 There we go. 47 00:02:33,640 --> 00:02:41,410 Matt one Matt two wonderful this array down here is just going to be the combination of these two multiplied 48 00:02:41,530 --> 00:02:52,210 element wise five times six is 30 0 times 7 0 but this is I said a little tricky word before element 49 00:02:52,480 --> 00:02:56,080 y's multiplication. 50 00:02:56,710 --> 00:03:01,190 There's a few different names to this but it's mostly called element y's hard Ahmad product. 51 00:03:01,210 --> 00:03:02,830 That's that's how you spell out I believe. 52 00:03:03,220 --> 00:03:03,810 Let's have a look. 53 00:03:03,830 --> 00:03:06,390 Hard hard. 54 00:03:06,580 --> 00:03:09,240 Product Name pi. 55 00:03:09,390 --> 00:03:13,200 That's what we're after How to Get element wise. 56 00:03:13,200 --> 00:03:13,680 Yeah. 57 00:03:13,700 --> 00:03:18,410 MP dot multiply which is the same as using this little star symbol. 58 00:03:18,420 --> 00:03:20,650 So that's element y's multiplication. 59 00:03:20,670 --> 00:03:25,980 Another form of multiplication or product between two matrices is the dot product. 60 00:03:26,340 --> 00:03:28,520 Let's have a look at that dot product. 61 00:03:28,590 --> 00:03:36,610 If we go back actually to our documentation the formula for doing a dot product is MP dot. 62 00:03:36,800 --> 00:03:44,950 You can use num piled up we've imported num pies and P so we're gonna do MP dot form mat 1. 63 00:03:45,180 --> 00:03:48,680 Map 2 which is just our two matrices that we made. 64 00:03:48,750 --> 00:03:53,590 So let's run this oh that didn't work. 65 00:03:53,670 --> 00:03:54,580 Damn it. 66 00:03:54,790 --> 00:03:59,050 Our value era shapes 5 3 5 3. 67 00:03:59,080 --> 00:04:01,140 Not aligned. 68 00:04:01,430 --> 00:04:04,300 3 dim 1 does not equal 5. 69 00:04:04,630 --> 00:04:08,960 Okay we go back up to see the shapes of our matrices. 70 00:04:08,950 --> 00:04:10,540 They're both of shape. 71 00:04:10,540 --> 00:04:12,810 5 3 5 3. 72 00:04:12,830 --> 00:04:21,580 This is saying shapes 5 3 and 5 3 not aligned well the difference between DOT PRODUCT AND element wise 73 00:04:22,140 --> 00:04:30,010 so we've seen element wise before it's relatively simple we've got a times a goes into this top left 74 00:04:30,010 --> 00:04:33,420 square a Times F no sorry b times. 75 00:04:33,640 --> 00:04:39,310 I just said simple and here I am messing it up the Times F but if we kept going through that we'd eventually 76 00:04:39,310 --> 00:04:40,760 end up with this matrix. 77 00:04:41,110 --> 00:04:48,310 But the dot product is a little bit different now we've got say a three by three matrix here a three 78 00:04:48,310 --> 00:04:54,190 by two matrix here and we're calling dot on this one with this one. 79 00:04:54,230 --> 00:05:02,800 Now what is going on here the resulting matrix is gonna be three by two and we have a times J. 80 00:05:02,830 --> 00:05:03,240 Okay. 81 00:05:03,250 --> 00:05:08,650 Following along with that then we have b times L plus. 82 00:05:08,650 --> 00:05:13,350 Okay then we have C times n all right. 83 00:05:13,390 --> 00:05:18,480 So it's got the top row here multiplied by the first column and adding up the total. 84 00:05:18,500 --> 00:05:25,540 Now let's look at a little bit more of a colorful demonstration of the dot product so here we've got 85 00:05:25,540 --> 00:05:32,130 the letters matrix if we go back here that the same one is here now we've got some color added. 86 00:05:32,130 --> 00:05:40,200 This is a top row of the matrix and the left multiplied by the column of the matrix on the right equals 87 00:05:40,590 --> 00:05:43,020 this top left square. 88 00:05:43,020 --> 00:05:49,360 And then if we kept going through we would eventually get this resulting matrix here. 89 00:05:49,620 --> 00:05:56,530 But there's a couple of rules with a dot product that numbers on the inside must match. 90 00:05:56,580 --> 00:06:01,800 So for a dot product to happen between two matrices they're dimensions. 91 00:06:01,800 --> 00:06:03,520 If you put them side by side. 92 00:06:03,630 --> 00:06:06,040 So this three matches this three. 93 00:06:06,110 --> 00:06:09,920 So this matrix here has three rows and so does this one. 94 00:06:10,080 --> 00:06:15,570 And now the resulting matrix is the size of the outside numbers. 95 00:06:15,570 --> 00:06:21,960 So the dimensions here are three by two of this resulting matrix because the outside numbers of these 96 00:06:21,960 --> 00:06:24,780 two matrices are 3 and 2. 97 00:06:24,780 --> 00:06:25,020 Okay. 98 00:06:25,020 --> 00:06:27,780 So we've got that for a dot product to happen. 99 00:06:27,780 --> 00:06:30,180 The inside numbers must match. 100 00:06:30,180 --> 00:06:34,140 Now let's have a look with some numbers that have actually been filled in. 101 00:06:34,170 --> 00:06:42,760 We've got 5 0 3 4 6 8 and we're calling dot between this matrix and this one so we've got 5 it's going 102 00:06:42,760 --> 00:06:46,830 to come over here be multiplied by 4 that equals 20. 103 00:06:46,840 --> 00:06:48,960 Yep we've got that 5 4. 104 00:06:48,970 --> 00:06:50,970 This is a Times J. 105 00:06:50,980 --> 00:06:54,600 Now we've got 0 times 6 0 times 6. 106 00:06:54,670 --> 00:06:57,720 This is B times l yep we've got that. 107 00:06:57,760 --> 00:07:01,520 We're adding that up now we've got three times eight. 108 00:07:01,570 --> 00:07:05,130 This is C times n Orion and that's 24. 109 00:07:05,140 --> 00:07:07,590 And if we add it these three numbers together. 110 00:07:07,840 --> 00:07:11,420 So we're going to go eight times J plus B times l policy times n. 111 00:07:11,440 --> 00:07:12,710 Yeah beautiful. 112 00:07:12,730 --> 00:07:18,500 We get 44 and that's going to fill the top left square of our resulting array. 113 00:07:18,550 --> 00:07:24,850 Now if we went through these two matrices with numbers full and followed the same steps that we've got 114 00:07:25,240 --> 00:07:32,020 in this letters matrix we would end up with something like this but this is a static demonstration so 115 00:07:32,020 --> 00:07:38,890 let's check out this little beautiful resource here which is an interactive demo. 116 00:07:38,890 --> 00:07:41,650 So this is matrix multiplication dot x y z. 117 00:07:41,650 --> 00:07:43,500 This is called the Waterfall technique. 118 00:07:43,510 --> 00:07:47,470 What this demonstration is going to do and you can change these numbers here to be something similar 119 00:07:47,470 --> 00:07:55,000 to maybe what we've got here or just to view the process of what's happening so if we click multiply 120 00:07:55,480 --> 00:07:59,290 it's going to take the matrix that was on the right and put it on top. 121 00:07:59,290 --> 00:08:01,730 This where the waterfall process comes in. 122 00:08:02,020 --> 00:08:09,610 Now it's going to flow down so this row two six one used to be here as a column. 123 00:08:09,610 --> 00:08:14,820 And if we add these together multiply them we get 15 in the top left. 124 00:08:14,980 --> 00:08:18,300 What it's done is it's just replicated this step here. 125 00:08:18,580 --> 00:08:21,030 We've got the top left. 126 00:08:21,090 --> 00:08:28,200 Now if we follow it through okay we get the top right and we get the middle left and we get the middle 127 00:08:28,200 --> 00:08:35,440 right and the bottom left and then we finally finish up with the bottom right number. 128 00:08:35,480 --> 00:08:40,910 So have a play around with this if you check it out you can keep going through it takes a little bit 129 00:08:40,910 --> 00:08:43,390 of practice to understand the dot product. 130 00:08:43,700 --> 00:08:49,790 Let's go back to our notebook and now that we know a little bit more about the dot product. 131 00:08:50,000 --> 00:08:56,230 We know the rules the numbers on the inside much matched and the new size of the resulting matrix is 132 00:08:56,230 --> 00:09:00,790 three by two because it takes the outside numbers. 133 00:09:00,790 --> 00:09:09,840 How would we make this work with our matrix 1 and Matrix 2 if their internal shapes are 5 and 3 and 134 00:09:09,840 --> 00:09:10,680 5 and 3 135 00:09:13,860 --> 00:09:22,890 or maybe we could use a transpose let's try that out transpose map 1 because what does it transpose 136 00:09:22,890 --> 00:09:23,490 do. 137 00:09:23,520 --> 00:09:28,680 It flips the accesses around so let's go map one dot t. 138 00:09:28,680 --> 00:09:29,510 What does that look like. 139 00:09:30,550 --> 00:09:30,900 Mm hmm. 140 00:09:31,200 --> 00:09:31,660 Okay. 141 00:09:32,660 --> 00:09:38,610 Might look at that and see maybe it'll work but let's really confirm it map one dot t dot shape. 142 00:09:39,990 --> 00:09:41,230 3 5. 143 00:09:41,320 --> 00:09:42,160 Is that what we need. 144 00:09:42,160 --> 00:09:42,750 Yeah. 145 00:09:42,880 --> 00:09:44,710 We need to flip it around. 146 00:09:44,710 --> 00:09:48,040 So if we have met two dot shape 147 00:09:51,220 --> 00:09:53,680 beautiful so now the inside shapes. 148 00:09:53,680 --> 00:10:02,760 If we were to transpose mat 1 matching so let's do this actually before we did MP dot map one not two. 149 00:10:02,770 --> 00:10:11,390 Okay maybe we transpose Matt to that one dot shape Matt to t to stay consistent while we go above. 150 00:10:11,570 --> 00:10:21,790 Let's make Matt three Eagles MP dot map one and then we want Matt to dot t. 151 00:10:21,790 --> 00:10:26,100 So this should work if our shapes are aligned Matt. 152 00:10:26,150 --> 00:10:28,900 Three beautiful. 153 00:10:29,000 --> 00:10:33,990 And now if we have Matt three what is the shape of Matt three yes. 154 00:10:34,000 --> 00:10:34,540 Excellent. 155 00:10:34,540 --> 00:10:42,310 So now we can see where the transpose might come into play if we wanted to do a dot product on two matrices 156 00:10:42,580 --> 00:10:45,810 where in our first try the shapes were incorrect. 157 00:10:45,820 --> 00:10:50,830 We might want to flip the accesses of course you could do this with the reshape as well but this is 158 00:10:50,830 --> 00:10:56,250 just a handy example where transpose comes into play for performing something like a dot product. 159 00:10:56,290 --> 00:11:04,730 Now we can see the resulting shape of Matrix 3 is the same as the outside numbers. 160 00:11:04,810 --> 00:11:06,320 Wonderful. 161 00:11:06,340 --> 00:11:10,720 Now when I first came across dot product it took me a little while to understand how to actually go 162 00:11:10,720 --> 00:11:11,450 through it. 163 00:11:11,500 --> 00:11:17,470 A great exercise was to write out a matrix by hand and multiply it and figure something out like this 164 00:11:17,920 --> 00:11:22,300 or use the demo at like Matrix Multiplication done X Y Z. 165 00:11:22,300 --> 00:11:28,030 The main takeaway from the dot product is that it's just another tool in our arsenal to find patterns 166 00:11:28,030 --> 00:11:30,780 between two different arrays of numbers. 167 00:11:30,850 --> 00:11:36,520 As we've seen before now in the next section we'll have a look at where the dot product might be used 168 00:11:36,520 --> 00:11:37,820 in practice. 169 00:11:37,960 --> 00:11:43,830 So have a practice around create some matrices transpose them see if you can do a dot product and see 170 00:11:43,840 --> 00:11:44,560 in the next video.