1 00:00:00,000 --> 00:00:05,250 All right, you know how to slice lists. 2 00:00:05,275 --> 00:00:07,296 [No audio] 3 00:00:07,320 --> 00:00:10,140 0 to 1 will give you the first item 4 00:00:10,140 --> 00:00:12,540 of the list, and 0 to 2, the first 5 00:00:12,540 --> 00:00:14,488 and the second item of the list, and so on. 6 00:00:15,178 --> 00:00:17,585 With NumPy arrays, you do basically 7 00:00:17,610 --> 00:00:20,820 the same thing. Except with NumPy 8 00:00:20,820 --> 00:00:22,860 arrays, sometimes you have 2 or more 9 00:00:22,860 --> 00:00:26,250 dimensionals, or 3. So let me go 10 00:00:26,250 --> 00:00:29,310 ahead and I'll create, I'll start with a 11 00:00:29,310 --> 00:00:32,820 2 dimensional array. So I'll get that 12 00:00:32,820 --> 00:00:36,570 from these variable. So this is the NumPy 13 00:00:36,570 --> 00:00:39,300 array that we'll be working with, and 14 00:00:39,300 --> 00:00:44,340 yeah, if you want to extract this number 15 00:00:44,340 --> 00:00:47,760 here, this here, this one, and this, so 16 00:00:47,760 --> 00:00:51,540 these four numbers, well, you'd first needs 17 00:00:51,540 --> 00:00:54,420 to set the index of the rows that you 18 00:00:54,420 --> 00:01:01,080 want to slice. So that'd be 0 to 2, which 19 00:01:01,080 --> 00:01:03,720 gives you the first 2 rows. Actually, 20 00:01:03,720 --> 00:01:06,450 we can try that, and you'll see that you 21 00:01:06,450 --> 00:01:09,540 get the first 2 rows. Now, if you want 22 00:01:09,755 --> 00:01:12,275 only this portion here, so these 23 00:01:12,300 --> 00:01:14,970 two numbers and these two, you would 24 00:01:14,970 --> 00:01:16,030 want to pass 25 00:01:16,055 --> 00:01:18,055 [No audio] 26 00:01:18,080 --> 00:01:20,460 comma there, and now the index 27 00:01:20,460 --> 00:01:22,980 for the columns, which would be, you 28 00:01:22,980 --> 00:01:27,187 know, 0 here, one, so two, three, 29 00:01:27,211 --> 00:01:30,480 and yeah 2 to 4 maybe, you know, that 30 00:01:30,480 --> 00:01:32,688 should do it, and that's it. 31 00:01:32,712 --> 00:01:35,081 [No audio] 32 00:01:35,106 --> 00:01:37,770 That means you have this indexing system. So it 33 00:01:37,770 --> 00:01:40,590 start from 0 for rows, and then row, the 34 00:01:40,590 --> 00:01:43,380 second row is 1, and 2, and so on, 35 00:01:43,500 --> 00:01:47,970 for columns are the same thing, 0, 1, 2, 3, 36 00:01:48,090 --> 00:01:51,720 and 4, and actually, if you like, you 37 00:01:51,745 --> 00:01:55,045 can see the shape of your NumPy array. 38 00:01:55,380 --> 00:01:57,780 So it's 3 by 5, which means 0 39 00:01:57,780 --> 00:02:02,220 to 2, and 0 to 4, great, and of 40 00:02:02,220 --> 00:02:05,160 course, you can use the convention of 41 00:02:05,190 --> 00:02:08,070 the list indexing. So you can pass 0 and 42 00:02:08,070 --> 00:02:10,410 everything after that or just like that. 43 00:02:11,850 --> 00:02:15,270 So you get all the rows and only columns 44 00:02:15,270 --> 00:02:18,390 from 2 to 3. Similarly, you can 45 00:02:18,390 --> 00:02:19,915 get only one value if you like. 46 00:02:19,939 --> 00:02:24,169 [No audio] 47 00:02:24,194 --> 00:02:27,600 Not particularly this one, because 3 is 48 00:02:27,600 --> 00:02:31,650 out of bounds for axis 0. So axis 0 49 00:02:31,710 --> 00:02:35,010 is the horizontal axis, which says that 50 00:02:35,010 --> 00:02:38,940 there is no row with index 3. So we 51 00:02:38,940 --> 00:02:43,410 set is 0, 1, 2. So let's pass 2 there, and you 52 00:02:43,410 --> 00:02:47,790 get 182, which is the very last item 53 00:02:47,790 --> 00:02:50,400 of the NumPy array. And yeah, that's about 54 00:02:50,430 --> 00:02:52,710 indexing. How about iterating through a 55 00:02:52,710 --> 00:02:55,920 NumPy array? Well, there are two ways to 56 00:02:55,920 --> 00:02:58,290 do that. The first way is to say, let's 57 00:02:58,315 --> 00:03:02,635 say for i in your numpy array, 58 00:03:04,080 --> 00:03:09,120 print i. And what that will do is it will 59 00:03:09,120 --> 00:03:12,720 print out the rows of your NumPy 60 00:03:12,720 --> 00:03:16,650 arrays, and so what these i access is in 61 00:03:16,650 --> 00:03:19,350 your NumPy array is the rows. So in each 62 00:03:19,350 --> 00:03:21,570 iteration, it gets the first row and it 63 00:03:21,570 --> 00:03:23,910 prints it out in the first iteration, 64 00:03:23,910 --> 00:03:25,922 then the second one, and then the third one, 65 00:03:27,629 --> 00:03:30,180 and if you want to iterate through 66 00:03:30,180 --> 00:03:32,670 columns, you'd want to access the 67 00:03:32,700 --> 00:03:36,660 transposed version of your NumPy array, and 68 00:03:36,660 --> 00:03:39,270 that's how you do it, and lastly, if 69 00:03:39,270 --> 00:03:41,460 you want to iterate value by value, 70 00:03:41,940 --> 00:03:49,020 you'd say for i in image and .flat 71 00:03:49,044 --> 00:03:51,725 [No audio] 72 00:03:51,750 --> 00:03:53,355 print i. 73 00:03:53,379 --> 00:03:55,616 [No audio] 74 00:03:55,641 --> 00:03:56,910 So this properly what it 75 00:03:56,910 --> 00:03:59,520 does is it allows you to access the 76 00:03:59,580 --> 00:04:02,100 values of your NumPy array one by one. 77 00:04:03,425 --> 00:04:05,345 And that's about indexing, slicing, and 78 00:04:05,370 --> 00:04:07,866 iterating. I'll see you in the next lecture.