1 00:00:00,270 --> 00:00:01,900 Alright alright alright. 2 00:00:02,100 --> 00:00:06,500 So in the previous few videos we've had a look at different ways of creating an empire raise. 3 00:00:06,600 --> 00:00:08,410 We've seen how we can view them. 4 00:00:08,550 --> 00:00:10,220 Now let's figure out the fun part. 5 00:00:10,590 --> 00:00:14,840 Let's figure out how we can manipulate and compare our different arrays. 6 00:00:14,850 --> 00:00:17,310 This is where the crux of machine learning happens right. 7 00:00:17,370 --> 00:00:23,250 It's manipulating performing calculations finding patterns in arrays of different numbers. 8 00:00:23,280 --> 00:00:24,600 Let's start it off. 9 00:00:24,600 --> 00:00:30,380 We'll put in another heading because we're keeping our notebooks nice and neat manipulating and comparing 10 00:00:30,390 --> 00:00:31,560 have I spelt it right. 11 00:00:31,560 --> 00:00:36,100 I usually get typos in these headings manipulating and comparing arrays. 12 00:00:36,120 --> 00:00:37,150 Beautiful. 13 00:00:37,200 --> 00:00:42,570 We're gonna put a little subheading here because like pandas with NUM pi if you can think of a way of 14 00:00:42,570 --> 00:00:45,180 manipulating numbers you can probably do it. 15 00:00:45,510 --> 00:00:49,780 So the first one we're going to look at is our riff Medtech. 16 00:00:49,780 --> 00:00:55,020 Here we go lucky encoding is a little bit better than my spelling ability but arithmetic is a fancy 17 00:00:55,020 --> 00:00:58,110 word for basic math equations. 18 00:00:58,170 --> 00:00:59,240 So let's have a look. 19 00:00:59,310 --> 00:01:01,500 Start off by checking out our A1 array. 20 00:01:01,500 --> 00:01:03,920 We created this earlier A1. 21 00:01:03,960 --> 00:01:05,070 Wonderful. 22 00:01:05,100 --> 00:01:07,330 We'll create another one called ones actually. 23 00:01:07,430 --> 00:01:10,430 We've already got ones yeah beautiful. 24 00:01:10,440 --> 00:01:16,560 But if you wanted to recreate it we'll go NDP dot one's three just to make sure we know what it looks 25 00:01:16,560 --> 00:01:16,980 like. 26 00:01:16,990 --> 00:01:22,760 One's wonderful Okay so we're going to have a look at is nice and simple plus. 27 00:01:23,160 --> 00:01:29,340 So what do you think this will do if we go A1 which is our right here plus ones. 28 00:01:29,350 --> 00:01:31,480 Well let's not talk about it Daniel. 29 00:01:31,480 --> 00:01:40,750 Let's run the code beautiful A1 plus once so we've got one plus one equals two two plus one equals three 30 00:01:41,020 --> 00:01:43,580 three plus one equals four. 31 00:01:43,630 --> 00:01:44,510 Excellent. 32 00:01:44,530 --> 00:01:51,240 So all this is done has just added together the different elements in the same position of both arrays. 33 00:01:51,310 --> 00:01:56,530 So the zero position one plus one it goes to the first position two plus one equals three. 34 00:01:56,710 --> 00:02:00,730 And the second position is three plus one equals four. 35 00:02:00,730 --> 00:02:01,980 All right well let's keep going. 36 00:02:01,980 --> 00:02:03,550 Let's see a few more options. 37 00:02:03,580 --> 00:02:06,550 Hey one minus one's Can you guess what'll happen here. 38 00:02:08,910 --> 00:02:09,900 Zero one two three. 39 00:02:09,900 --> 00:02:19,740 So all it's done is it subtracted ones from a one let's push forward let's try multiply i1 times ones 40 00:02:20,780 --> 00:02:25,880 Yeah well that makes sense because our one's array is just all ones and it's just times a one by one 41 00:02:26,150 --> 00:02:30,530 one times one is one one times two is two and so on. 42 00:02:30,530 --> 00:02:33,720 Let's bring another array a two. 43 00:02:33,720 --> 00:02:35,700 Here we go. 44 00:02:35,700 --> 00:02:44,370 Now this will be interesting if we multiply A1 by A2 what's going to happen huh let's see. 45 00:02:44,370 --> 00:02:45,660 Let's work through this. 46 00:02:45,720 --> 00:02:47,700 So we've got one. 47 00:02:47,700 --> 00:02:49,020 This is let's get up. 48 00:02:49,020 --> 00:02:52,560 I won again so we can see everything in one place. 49 00:02:52,560 --> 00:03:01,380 I won one times one is one two times two is four three times three point three is nine point nine. 50 00:03:01,380 --> 00:03:02,670 That makes sense. 51 00:03:02,690 --> 00:03:06,680 What about this second row here. 52 00:03:06,700 --> 00:03:12,570 Let's see if we can figure this out by looking at it 1 4 4 2 2 5 10. 53 00:03:12,570 --> 00:03:16,140 Yep three times six point five is nineteen point five. 54 00:03:16,150 --> 00:03:19,780 Let's use python to help us out for that six point five. 55 00:03:20,250 --> 00:03:22,400 It's nineteen point five huh. 56 00:03:22,600 --> 00:03:24,820 We can get rid of this cell we don't want that actually. 57 00:03:24,940 --> 00:03:29,590 Never use a toolbar escape X to get rid of that. 58 00:03:29,620 --> 00:03:36,720 So what this is done is it's taken a one these three numbers here multiplied it by this row. 59 00:03:36,970 --> 00:03:43,400 Put it in here and then done the same thing again with a one and multiply it by this row. 60 00:03:43,640 --> 00:03:45,500 Huh that's interesting. 61 00:03:46,030 --> 00:03:52,720 Let's have a look at our other array first A3 let's combine these two a two and a three a two times 62 00:03:52,750 --> 00:04:03,670 a three operations could not be broadcast together with shapes two three two three three operations 63 00:04:03,670 --> 00:04:04,950 could not be broadcast together. 64 00:04:04,950 --> 00:04:06,310 That's interesting. 65 00:04:06,400 --> 00:04:11,350 But remember right at the start the concept video where we talked about where num pi is fast because 66 00:04:11,350 --> 00:04:15,640 it vector rises code through a technique called broadcasting. 67 00:04:15,640 --> 00:04:17,730 Well this is what's happening here. 68 00:04:17,890 --> 00:04:22,950 But to be that fast broadcasting has a few rules so let's look that up. 69 00:04:22,950 --> 00:04:26,230 Num pi broadcasting. 70 00:04:26,230 --> 00:04:27,250 There we go. 71 00:04:27,250 --> 00:04:34,590 We're gonna go to the documentation broadcasting num pi click on that the term broadcasting describes 72 00:04:34,590 --> 00:04:39,180 how num pi treats arrays with different shapes during arithmetic operations which is exactly what we're 73 00:04:39,180 --> 00:04:39,740 doing. 74 00:04:39,870 --> 00:04:46,320 We're trying to use some arithmetic on arrays with different shapes so there's a three and there's a 75 00:04:46,320 --> 00:04:49,650 two they've got different shapes and that's what the errors coming up. 76 00:04:49,650 --> 00:04:51,390 So let's have a look here. 77 00:04:51,510 --> 00:04:57,290 Subject to certain constraints the smaller array is broadcast across the larger array. 78 00:04:57,390 --> 00:04:59,420 So they have compatible shapes. 79 00:04:59,430 --> 00:05:06,930 Well that explains what's happened up here with a one times eight to the smaller array is broadcast 80 00:05:07,020 --> 00:05:09,840 across the larger array. 81 00:05:09,960 --> 00:05:16,780 So they have compatible shapes that makes sense because we've got here this is a smaller array. 82 00:05:16,860 --> 00:05:21,590 It's been broadcast across this row and then across this row. 83 00:05:22,200 --> 00:05:26,660 So that the output has a compatible shape of the larger array. 84 00:05:26,720 --> 00:05:27,600 Okay. 85 00:05:28,080 --> 00:05:30,630 We'll keep going there's some examples here. 86 00:05:30,630 --> 00:05:32,600 So these are two arrays multiplied. 87 00:05:32,610 --> 00:05:34,200 They've got the same shape. 88 00:05:34,470 --> 00:05:39,500 1 2 3 2 2 2 8 times P is one times two is two. 89 00:05:39,500 --> 00:05:42,750 Two times two is four three times two is six. 90 00:05:42,750 --> 00:05:43,840 Aha. 91 00:05:43,860 --> 00:05:45,100 That makes sense. 92 00:05:45,360 --> 00:05:50,830 And then broadcasting rule relaxes this constraint when the array shape meet certain constraints. 93 00:05:50,840 --> 00:05:51,120 Okay. 94 00:05:51,150 --> 00:05:51,780 So this is good. 95 00:05:51,780 --> 00:05:56,310 This is the kind of workflow you want to start getting into right is if you come across an area like 96 00:05:56,310 --> 00:06:03,330 this and you will all the time one of these mismatch shapes errors like different shapes that are incompatible 97 00:06:03,330 --> 00:06:05,220 with each other a value area. 98 00:06:05,280 --> 00:06:10,440 These are gonna come up all the time in machine learning and part of being a data scientist or a machine 99 00:06:10,440 --> 00:06:16,110 learning engineer is reading documentation like we're doing now and trying to figure out how to fix 100 00:06:16,110 --> 00:06:17,190 our problem. 101 00:06:17,280 --> 00:06:20,360 So General broadcasting rules okay. 102 00:06:20,450 --> 00:06:23,080 When operating on two arrays none pi compares their shapes. 103 00:06:23,100 --> 00:06:28,530 Element wise it starts with the trailing dimensions and works that way forward two dimensions are compatible 104 00:06:28,530 --> 00:06:31,860 when they are equal or one of them is one. 105 00:06:31,950 --> 00:06:38,190 Now to save a little bit of time instead of going through the entire documentation I'll leave that for 106 00:06:38,190 --> 00:06:38,940 you. 107 00:06:39,030 --> 00:06:47,130 I want you to have a think about try and figure out a way how would we get these two to be of compatibility. 108 00:06:47,130 --> 00:06:50,640 How would we fulfill this broadcasting need. 109 00:06:50,670 --> 00:07:02,670 So what you may want to look into is how can you reshape A2 to be compatible with A3. 110 00:07:03,000 --> 00:07:12,120 So that might be looking at search put in another comment here how to reshape num pie array. 111 00:07:12,720 --> 00:07:19,320 So check out the broadcasting documentation along with searching something like this and then figure 112 00:07:19,320 --> 00:07:21,240 out how to make these two shapes align. 113 00:07:21,240 --> 00:07:21,810 If you can't. 114 00:07:21,840 --> 00:07:23,240 That's entirely fine. 115 00:07:23,250 --> 00:07:27,230 This takes a bit of practice but we're going to see how to do it in a future video. 116 00:07:27,240 --> 00:07:31,270 Right now we're going to continue with our arithmetic just to finish things off. 117 00:07:31,350 --> 00:07:34,460 So where were we we've done multiplication. 118 00:07:34,560 --> 00:07:38,180 Let's try A1 divided by ones. 119 00:07:38,280 --> 00:07:43,770 So this little slashes four divided by you can probably predict what's going to happen here. 120 00:07:43,770 --> 00:07:47,480 Beautiful it works fine because our rays are similar shapes. 121 00:07:47,510 --> 00:07:49,180 Okay let's try a two. 122 00:07:49,290 --> 00:07:53,610 Let's try some floor division think two divided by a one. 123 00:07:53,610 --> 00:07:54,660 Wonderful. 124 00:07:54,870 --> 00:07:58,190 But we'll see what non floor division works here. 125 00:07:58,200 --> 00:07:58,890 There we go. 126 00:07:59,040 --> 00:08:04,100 So floor division what this means if you do two slashes you might have seen this in Python. 127 00:08:04,110 --> 00:08:05,400 If not that's fine. 128 00:08:05,460 --> 00:08:16,230 Floor division removes the decimals so you can see here it rounds it down rounds down so one point zero 129 00:08:16,410 --> 00:08:24,300 point zero point zero one point one but that's been rounded down to one two point 1 6 6 6 6 7 has been 130 00:08:24,300 --> 00:08:25,380 rounded down to 2. 131 00:08:25,920 --> 00:08:26,730 Let's keep going. 132 00:08:26,730 --> 00:08:27,780 What have we missed out on. 133 00:08:27,810 --> 00:08:32,900 Let's go square a two square two. 134 00:08:33,030 --> 00:08:38,540 This is a power so to the power of so everything in here is going to be to the power of two. 135 00:08:38,580 --> 00:08:47,520 So if we look at a two on its own it's gonna be one times one two times two is four one times one is 136 00:08:47,520 --> 00:08:53,700 one three point three times three point three is ten point eight nine will confirm that with Python 137 00:08:55,050 --> 00:09:01,500 wonderful ten point eight nine that's just filled in then rounded it off actually num Pi has a square 138 00:09:01,500 --> 00:09:08,700 you can use with this so all of these functions that you see here that we've gone through arithmetic 139 00:09:08,700 --> 00:09:15,270 floor division etc num Pi has the equivalent here now which one should you use. 140 00:09:15,270 --> 00:09:17,620 Well it's going to depend on what your workflow is best at. 141 00:09:18,210 --> 00:09:24,030 So if you want to use the maths symbol operations you can use that or if you want to use Square or you 142 00:09:24,030 --> 00:09:30,930 can even go and he'd add gonna go shift tab to figure out what it does it's figure out here doc string 143 00:09:31,290 --> 00:09:43,000 adds arguments element wise let's try that a1 ones same as just doing a one plus ones wonderful. 144 00:09:43,030 --> 00:09:44,190 I got a couple more. 145 00:09:44,370 --> 00:09:45,730 I won modulo. 146 00:09:46,270 --> 00:09:47,830 This is gonna show us the remainders. 147 00:09:48,610 --> 00:09:52,110 So A1 divided by two let's see where that goes. 148 00:09:52,120 --> 00:09:57,360 A1 divided by two let's go now in PI modulo 149 00:10:00,100 --> 00:10:00,720 model. 150 00:10:00,740 --> 00:10:01,310 There we go. 151 00:10:03,260 --> 00:10:05,960 Return element y's remainder of division. 152 00:10:05,990 --> 00:10:06,890 Wonderful. 153 00:10:06,920 --> 00:10:12,260 So this is again I'm trying to exemplify the kind of workflow you go towards if you're not sure what 154 00:10:12,260 --> 00:10:13,540 something does. 155 00:10:13,550 --> 00:10:16,340 You can always look up the documentation and have a look. 156 00:10:16,340 --> 00:10:17,960 That's what we did with broadcasting. 157 00:10:17,960 --> 00:10:24,470 We've just done that with modulo if we divided every element in a one modulo two it's gonna show us 158 00:10:24,470 --> 00:10:26,320 the remainder that's left over. 159 00:10:26,450 --> 00:10:29,870 So let's do a two modulo two. 160 00:10:29,960 --> 00:10:36,970 So this is just showing us the remainder of what's left after we've divided everything by two and if 161 00:10:36,970 --> 00:10:40,960 you wanted the exponential you're not sure what this is. 162 00:10:40,960 --> 00:10:46,450 Don't worry for now it's just to highlight the kind of arithmetic that you can do and num pi is gonna 163 00:10:46,450 --> 00:10:51,730 take the exponential of everything in an array and if you want the log you might see this come up in 164 00:10:51,730 --> 00:10:59,760 future projects of an array you can use MP dot log so that was a bit of a whirlwind tour of some of 165 00:10:59,760 --> 00:11:04,620 the arithmetic functions that you can do a num pi remember if you can't imagine it. 166 00:11:04,620 --> 00:11:05,870 You can probably do it. 167 00:11:05,910 --> 00:11:08,090 This is only just scratching the surface. 168 00:11:08,130 --> 00:11:13,320 So before next video have a practice around creating some of your own arrays and then using some of 169 00:11:13,320 --> 00:11:15,110 these arithmetic functions on them. 170 00:11:15,240 --> 00:11:15,560 Remember. 171 00:11:15,780 --> 00:11:18,910 If in doubt run the code if you're not sure what something does. 172 00:11:19,050 --> 00:11:23,300 Have a look at the NUM pi documentation see if you can figure it out. 173 00:11:23,640 --> 00:11:29,760 I'll see in the next video we're going to cover a little topic called aggregation but we'll save that 174 00:11:29,760 --> 00:11:31,000 for the next video. 175 00:11:31,020 --> 00:11:31,490 See you soon.