1 00:00:00,360 --> 00:00:06,300 OK now we've seen a few different ways to manipulate a raise but most of the ways we've worked with 2 00:00:06,300 --> 00:00:08,550 have been arithmetic or aggregation. 3 00:00:08,550 --> 00:00:12,700 So kind of performing calculations on Empire right now. 4 00:00:12,810 --> 00:00:17,460 What if you need to change the shape of your name higher array. 5 00:00:17,610 --> 00:00:20,480 Now why would you need to change the shape of an umpire right. 6 00:00:20,700 --> 00:00:26,910 Well remember a lot of machine learning is lining up your inputs into a machine learning algorithm and 7 00:00:26,910 --> 00:00:31,080 making sure that they come out with the right output. 8 00:00:31,080 --> 00:00:37,950 One of those ways of lining up inputs is making sure your data or your list of numbers or your array 9 00:00:37,950 --> 00:00:40,150 of numbers is in the right shape. 10 00:00:40,170 --> 00:00:43,500 So let's have a look at how to do that in num pi. 11 00:00:43,500 --> 00:00:49,230 So we'll go reshaping and transposing this is this section. 12 00:00:49,230 --> 00:00:51,350 Nice little heading there. 13 00:00:51,380 --> 00:00:52,130 So let's have a look. 14 00:00:52,260 --> 00:00:53,290 A 2 array. 15 00:00:53,310 --> 00:00:54,250 Beautiful. 16 00:00:54,300 --> 00:00:56,330 Let's figure out what shape it is. 17 00:00:56,340 --> 00:00:57,360 Two by three. 18 00:00:57,440 --> 00:00:58,680 Yeah that makes sense. 19 00:00:58,680 --> 00:01:04,630 Now remember before in a previous video we tried to multiply A2 by A3. 20 00:01:04,680 --> 00:01:08,820 Let's visualize a three so we can see it again a three. 21 00:01:08,820 --> 00:01:09,570 There we go. 22 00:01:09,930 --> 00:01:12,910 Let's check out the shape a 3D up shape. 23 00:01:13,750 --> 00:01:21,850 Okay now what's going to happen if we try to multiply these operations could not be broadcast together 24 00:01:22,180 --> 00:01:26,010 with shapes 2 3 or 2 3 3. 25 00:01:26,500 --> 00:01:33,220 Well the beautiful thing about an umpire rains is that they can be reshaped or transposed. 26 00:01:33,220 --> 00:01:35,710 Let's have a look at reshape first. 27 00:01:35,800 --> 00:01:41,580 So what does reshaped to reshape is a method you can call on any array. 28 00:01:41,650 --> 00:01:49,540 So if we type in here dot reshape shift tab reshape dock string returns an array containing the same 29 00:01:49,540 --> 00:01:53,820 data with a new shape beautiful. 30 00:01:53,860 --> 00:01:54,280 There we go. 31 00:01:54,280 --> 00:01:56,320 Get some more information there. 32 00:01:56,380 --> 00:01:58,980 Let's see what it requires shape. 33 00:01:59,330 --> 00:02:00,400 Mm hmm. 34 00:02:00,640 --> 00:02:05,650 So one of the rules of broadcasting if we look up broadcasting one more time 35 00:02:08,880 --> 00:02:12,500 one of the rules of broadcasting is here. 36 00:02:12,630 --> 00:02:17,970 General broadcasting rules when operating on two arrays num pi compares their shapes. 37 00:02:18,000 --> 00:02:21,850 Element wise it starts with the trailing dimensions and works its way forward. 38 00:02:21,990 --> 00:02:27,810 Two dimensions are compatible when they are equal or one of them is one. 39 00:02:27,810 --> 00:02:35,010 So what this means is that if the shapes of two arrays are equal the multiplication can happen but if 40 00:02:35,010 --> 00:02:38,280 they're not equal one of them has to be one. 41 00:02:38,310 --> 00:02:40,170 So that's what we're going to do here. 42 00:02:40,170 --> 00:02:50,310 We're going to reshape the A2 array from its shape to three incompatible here. 43 00:02:50,940 --> 00:02:55,560 And we're going to change one of these dimensions we're going to add a dimension actually to reshape 44 00:02:55,590 --> 00:02:57,880 2 3 1. 45 00:02:57,960 --> 00:03:00,640 Let's see what happens. 46 00:03:00,910 --> 00:03:01,390 OK. 47 00:03:01,400 --> 00:03:03,470 So it's shuffled our array around a bean. 48 00:03:03,650 --> 00:03:07,960 Let's print the shape here so see our A2. 49 00:03:07,960 --> 00:03:11,590 We've used the reshape method passed in a different shape. 50 00:03:11,590 --> 00:03:17,530 So now we've added another dimension here and now its shape is 2 3 1. 51 00:03:17,540 --> 00:03:23,040 We'll check a three shape okay two three three. 52 00:03:23,150 --> 00:03:28,680 So if we refer back to the rules of broadcasting are the dimensions equal. 53 00:03:28,680 --> 00:03:29,670 No. 54 00:03:29,910 --> 00:03:31,210 Is one of them one. 55 00:03:31,500 --> 00:03:32,670 Yes. 56 00:03:32,760 --> 00:03:33,990 So let's try this. 57 00:03:33,990 --> 00:03:45,400 A two will make a variable a to reshape Eagles a to reshape 2 3 1 Beautiful. 58 00:03:45,720 --> 00:03:57,130 So we'll have a look at this a to reshape there's our reshaped a to a to reshape times a 3 There we 59 00:03:57,130 --> 00:03:57,540 go. 60 00:03:57,580 --> 00:03:58,630 Beautiful. 61 00:03:58,810 --> 00:04:01,390 So that is the benefit of reshape. 62 00:04:01,540 --> 00:04:03,760 Now this is only a small example here. 63 00:04:03,820 --> 00:04:11,710 Before they were incompatible but because we reshaped it we reshaped A2 to be within the broadcasting 64 00:04:11,710 --> 00:04:13,350 rules of num pie. 65 00:04:13,390 --> 00:04:18,700 We could then use the multiplier function across these two arrays. 66 00:04:18,700 --> 00:04:23,040 Now if this concept is kind of strange to begin with that's perfectly fine. 67 00:04:23,080 --> 00:04:23,980 And it will be. 68 00:04:24,100 --> 00:04:29,620 It will take a fair bit of practice to figure out how to use reshape properly but what the important 69 00:04:29,620 --> 00:04:37,120 takeaway here is is that just because your name pie array comes in a certain shape like it begins like 70 00:04:37,120 --> 00:04:45,850 this doesn't mean it has to stay in that shape so this reshaped version of A2 still contains the same 71 00:04:45,850 --> 00:04:48,610 information as the original A2. 72 00:04:48,640 --> 00:04:50,300 It's just in a different shape. 73 00:04:50,410 --> 00:04:55,540 So this is one of the problems you'll come across in many different machine learning problems is making 74 00:04:55,540 --> 00:05:01,330 sure that the data that you're trying to find patterns within is the same shape. 75 00:05:01,390 --> 00:05:08,170 So the machine learning algorithm can run over it like this broadcasting operation as quickly as possible. 76 00:05:08,290 --> 00:05:15,100 And if they aren't in the same shape you'll often get back errors like this which say the data you're 77 00:05:15,100 --> 00:05:17,760 trying to find patterns in isn't the same shape. 78 00:05:17,830 --> 00:05:24,010 So that's when you have to figure out how to use something like reshape to turn them into the same shape 79 00:05:24,370 --> 00:05:26,530 so that they're compatible with each other. 80 00:05:27,560 --> 00:05:35,370 Now there's one more thing which is transpose now what transpose is let's have a look at it first. 81 00:05:35,720 --> 00:05:38,210 Let's put a little comment here so we know what's happening. 82 00:05:38,210 --> 00:05:41,730 Transpose A2. 83 00:05:41,780 --> 00:05:43,620 Actually we might leave it here. 84 00:05:43,660 --> 00:05:44,800 A2 Doc T. 85 00:05:44,810 --> 00:05:48,260 So we can see what's changed here. 86 00:05:48,260 --> 00:05:53,300 Let's get A2 back up A2 there transpose what's changed. 87 00:05:53,300 --> 00:05:57,160 Let's go dot shape 3 2. 88 00:05:57,290 --> 00:05:57,630 Okay. 89 00:05:57,630 --> 00:06:01,070 Now if we go here Hey to shape. 90 00:06:01,410 --> 00:06:09,310 So what a transpose does it switches the accesses. 91 00:06:09,370 --> 00:06:13,410 So now A2 shape was 2 3. 92 00:06:13,510 --> 00:06:18,070 But if we call transpose and now t is 4 transpose it's going to swap. 93 00:06:18,100 --> 00:06:19,530 The axis is around. 94 00:06:19,720 --> 00:06:21,920 That's where we can see that now. 95 00:06:21,940 --> 00:06:28,220 One two three point three is down here in the column and four five six point five. 96 00:06:28,310 --> 00:06:29,950 It is on this column. 97 00:06:30,220 --> 00:06:33,490 So what it's done is essentially just swap the rows and columns. 98 00:06:33,490 --> 00:06:35,890 Now you can do this on a larger array as well. 99 00:06:35,920 --> 00:06:41,970 Let's view A3 A3 not shape 2 3 3. 100 00:06:41,980 --> 00:06:51,080 So if we call a three dot t it's going to swap these axes around so let's say a three dot teed up shape. 101 00:06:51,130 --> 00:06:55,130 So now we've essentially flipped it. 102 00:06:55,150 --> 00:07:00,590 So now this dimension so too is right at the end. 103 00:07:00,780 --> 00:07:04,530 And 3 and 3 have swapped to the front. 104 00:07:04,530 --> 00:07:10,860 The important difference here between transpose and reshape is that transpose just flips the accesses 105 00:07:10,860 --> 00:07:12,960 around and reshape. 106 00:07:12,960 --> 00:07:15,810 You can create your own custom shapes. 107 00:07:15,810 --> 00:07:21,780 So for example we reshaped A2 to be compatible with our broadcasting rules. 108 00:07:21,950 --> 00:07:24,370 So have a practice reshaping some of your arrays. 109 00:07:24,420 --> 00:07:26,070 And I'll see you in the next video.