1 00:00:00,000 --> 00:00:01,174 [No audio] 2 00:00:01,199 --> 00:00:03,209 All right, now that you know how to do 3 00:00:03,209 --> 00:00:05,249 slicing and iterating through NumPy 4 00:00:05,249 --> 00:00:07,799 arrays, let's do some more specific 5 00:00:07,824 --> 00:00:10,524 operations, and that'd be stocking 6 00:00:10,559 --> 00:00:13,139 NumPy arrays, so concatenating NumPy 7 00:00:13,139 --> 00:00:16,139 arrays to each other, and also splitting 8 00:00:16,169 --> 00:00:20,969 a NumPy array to smaller arrays. And yeah, I have 9 00:00:20,994 --> 00:00:24,874 this NumPy array still here to what's, 10 00:00:24,899 --> 00:00:27,839 let's start with stocking to NumPy 11 00:00:27,839 --> 00:00:30,929 arrays. To do that, you may want to 12 00:00:31,199 --> 00:00:32,999 create a new variable where you'll 13 00:00:32,999 --> 00:00:35,639 save your big array and let it be equal to 14 00:00:35,639 --> 00:00:38,999 numpy and the methods to stock two NumPy 15 00:00:38,999 --> 00:00:41,189 arrays, actually, there are two methods. 16 00:00:41,497 --> 00:00:45,217 One is horizontal stack, so hstack, 17 00:00:45,511 --> 00:00:48,719 and that expects from you to pass two or 18 00:00:48,719 --> 00:00:50,609 more NumPy arrays that you want to stack 19 00:00:50,609 --> 00:00:53,988 horizontally. So let me pass 20 00:00:54,012 --> 00:00:56,304 [No audio] 21 00:00:56,334 --> 00:00:57,394 im_g, and 22 00:00:57,419 --> 00:01:00,419 then the same array, it is not a 23 00:01:00,419 --> 00:01:02,579 problem. If you had two different 24 00:01:02,579 --> 00:01:04,289 arrays, you could do that. But 25 00:01:04,289 --> 00:01:05,699 I'm just passing the same array 26 00:01:05,699 --> 00:01:07,899 there. Now if you execute this, you'll 27 00:01:07,924 --> 00:01:09,689 get an error, and I want you to see 28 00:01:09,689 --> 00:01:12,749 this error so that you understand it. So 29 00:01:12,749 --> 00:01:15,179 it says each stack takes one positional 30 00:01:15,179 --> 00:01:17,819 argument, but 2 were given, which 31 00:01:17,819 --> 00:01:20,639 means that a stack inside the brackets, 32 00:01:20,669 --> 00:01:23,729 it gets only 1 argument, but we were 33 00:01:23,775 --> 00:01:26,295 passing 2. So how do we go about, 34 00:01:26,587 --> 00:01:29,734 I mean, concatenating, 2 NumPy arrays. We can 35 00:01:29,759 --> 00:01:32,759 just pass one NumPy array there. So the 36 00:01:32,759 --> 00:01:37,804 solution here is to have a tuple of 37 00:01:37,829 --> 00:01:41,879 NumPy arrays, just like that. So a tuple 38 00:01:41,904 --> 00:01:45,474 inside the input or the hstack method, 39 00:01:46,414 --> 00:01:47,588 you know, execute that. 40 00:01:47,612 --> 00:01:50,906 [No audio] 41 00:01:50,931 --> 00:01:52,144 And here is a stacked 42 00:01:52,169 --> 00:01:55,939 array. Yeah, so this is the first array 43 00:01:56,954 --> 00:01:59,404 and then the next array, which in this case 44 00:01:59,429 --> 00:02:02,789 happens to be the same one. But if you 45 00:02:02,789 --> 00:02:04,321 want, you can add more, 46 00:02:04,345 --> 00:02:08,987 [No audio] 47 00:02:09,012 --> 00:02:12,149 and you get a longer array, which is not very good 48 00:02:12,149 --> 00:02:15,170 looking like that. So you can print that out, 49 00:02:16,876 --> 00:02:18,888 and yeah, now you see the difference. 50 00:02:19,170 --> 00:02:22,649 So the first array, second, 51 00:02:22,674 --> 00:02:24,258 and the third one. 52 00:02:24,282 --> 00:02:26,846 [No audio] 53 00:02:26,871 --> 00:02:29,729 And if you want to stack 54 00:02:29,754 --> 00:02:32,214 vertically, you would want to do v there. 55 00:02:32,238 --> 00:02:37,781 [No audio] 56 00:02:37,826 --> 00:02:41,240 And yeah, as you expected this, we'll 57 00:02:41,760 --> 00:02:44,100 concatenate the arrays, array in the 58 00:02:44,100 --> 00:02:46,620 vertical position in the vertical axis. 59 00:02:47,310 --> 00:02:50,460 Beware that if you try to concatenate 60 00:02:51,090 --> 00:02:53,095 arrays that have different dimensions, 61 00:02:53,120 --> 00:02:55,257 [No audio] 62 00:02:55,282 --> 00:02:59,021 you will have, you'll get an error. So im_c 63 00:02:59,045 --> 00:03:01,080 has 3-dimensions if you can 64 00:03:01,080 --> 00:03:06,030 remember that from up here. Now you'll 65 00:03:06,030 --> 00:03:07,710 learn how to concatenate and how about 66 00:03:07,710 --> 00:03:11,220 splitting an array into smaller arrays. 67 00:03:12,030 --> 00:03:14,970 Well, to do that, you could create a 68 00:03:14,970 --> 00:03:19,421 variable and then numpy.horizontalsplit 69 00:03:20,063 --> 00:03:23,015 for splitting horizontally, and then this 70 00:03:23,040 --> 00:03:26,430 expects NumPy array, and how many arrays 71 00:03:26,430 --> 00:03:29,430 you want to produce all of that? So 72 00:03:29,430 --> 00:03:31,680 horizontally, let's say 3. 73 00:03:31,704 --> 00:03:36,965 [No audio] 74 00:03:36,990 --> 00:03:39,450 Now this says array split does not 75 00:03:39,450 --> 00:03:43,170 result in an equal division, and the reason 76 00:03:43,208 --> 00:03:47,768 to that is, you know, we're trying this array has 77 00:03:47,795 --> 00:03:51,695 1, 2, 3, 4, 5 columns. So it's trying to divide 78 00:03:51,720 --> 00:03:54,270 5 with 3 and NumPy cannot decide 79 00:03:54,295 --> 00:03:57,445 how to do that. So should it give to you 80 00:03:57,810 --> 00:04:00,840 a NumPy array with these two column first, 81 00:04:00,879 --> 00:04:03,309 and then another NumPy with 2 columns, 82 00:04:03,485 --> 00:04:05,135 and then the last NumPy array with 1 83 00:04:05,160 --> 00:04:09,000 column. So NumPy cannot do that. What 84 00:04:09,025 --> 00:04:13,525 you should do is maybe 5 there and 85 00:04:13,977 --> 00:04:15,437 lst 86 00:04:15,462 --> 00:04:18,000 [No audio] 87 00:04:18,025 --> 00:04:19,770 and that should give you 5 88 00:04:19,770 --> 00:04:22,170 different NumPy arrays, which in this 89 00:04:22,170 --> 00:04:24,420 case happens to be, you know, 1 array 90 00:04:24,420 --> 00:04:29,555 for each column, and you can do verticalsplitting. 91 00:04:29,718 --> 00:04:30,965 This time we can try 92 00:04:30,990 --> 00:04:33,840 3 because we have 9 rows there. 93 00:04:33,865 --> 00:04:36,665 [No audio] 94 00:04:36,690 --> 00:04:39,621 And yeah, we get 3 small arrays. 95 00:04:39,645 --> 00:04:41,857 [No audio] 96 00:04:41,882 --> 00:04:44,940 Note that this lst, so the split, what it 97 00:04:44,940 --> 00:04:48,279 produces is a Python list of NumPy arrays. 98 00:04:49,791 --> 00:04:51,956 so it's a plain python list 99 00:04:52,387 --> 00:04:55,445 and after that you can access each of the 100 00:04:55,470 --> 00:04:58,740 NumPy arrays if you like, you know 101 00:04:58,740 --> 00:05:03,150 that. So it outputs the first array, and 102 00:05:03,150 --> 00:05:05,430 yeah, that's about concatenating and 103 00:05:05,430 --> 00:05:08,160 splitting a NumPy arrays. Hope I give 104 00:05:08,160 --> 00:05:11,460 you a good overview of NumPy. I tried to 105 00:05:11,490 --> 00:05:13,860 do some practical examples like opening 106 00:05:13,860 --> 00:05:16,950 images. But this is still yet the 107 00:05:16,950 --> 00:05:18,900 theoretical part. So NumPy is quite 108 00:05:18,900 --> 00:05:21,840 theoretical, and you only understand it 109 00:05:21,840 --> 00:05:25,050 when you get to use like this real world 110 00:05:25,050 --> 00:05:27,510 examples like working with pandas, as we 111 00:05:27,510 --> 00:05:30,420 did previously in the course and also 112 00:05:30,420 --> 00:05:32,970 working with images. So we're going to 113 00:05:32,970 --> 00:05:35,100 do some hardcore image processing here 114 00:05:35,130 --> 00:05:36,750 in the next lectures, in the next 115 00:05:36,750 --> 00:05:39,840 section, and you'll see how NumPy comes 116 00:05:39,840 --> 00:05:42,330 in handy in there, and yeah, I hope you enjoyed 117 00:05:42,330 --> 00:05:44,400 this and I'll see you later.