Python/C3/Accessing-parts-of-arrays/English-timed
From Script | Spoken-Tutorial
Revision as of 12:06, 28 April 2017 by PoojaMoolya (Talk | contribs)
Time | Narration |
00:00 | Hello friends and Welcome to the tutorial on 'Accessing pieces of arrays'. |
00:05 | At the end of this tutorial, you will be able to,
Access and change individual elements of arrays, both one dimensional and multi-dimensional. Access and change rows and columns of arrays. Access and change other chunks from an array, using slicing and striding. And read images into arrays and perform processing on them, using simple array manipulations. |
00:32 | Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with arrays". |
00:40 | Now open the terminal and type ipython hypen pylab |
00:50 | Let us begin with the help of an example. |
00:54 | Let us have two arrays, A and C, as the sample arrays that we will use to work through this tutorial. |
01:08 | Let us begin with the most elementary thing, accessing individual elements. |
01:14 | Also, let us first do it with the one-dimensional array A, and then do the same thing with the two-dimensional array. |
01:30 | Type A = array within bracket square bracket 12 comma 23 coma 34 comma 45 comma 56 |
01:45 | C = array within bracket square bracket 11 comma 12 comma 13 comma 14 comma 15 comma
Next in square bracket 21 to 25 comma Next one in square bracket 31 to 35 comma Next one is square bracket 41 to 45 comma And the final one is square bracket 51 to 55 then close the bracket and hit enter. |
02:36 | To access, the element 34 in array A, we say, A of 2, note that we are using square brackets. |
02:44 | So type A within square bracket2 and hit enter. |
02:51 | Like lists, indexing starts from 0 in arrays,. |
02:56 | So, 34, the third element have the index 2. |
03:03 | Now, let us access the element 34 from C. |
03:06 | To do this, we say, C of 2,3. |
03:11 | So type C within square bracket 2 comma 3 and hit enter |
03:18 | 34 is in the third row and the fourth column, and since indexing begins from zero, the row index is 2 and column index is 3. |
03:31 | Now, that we have accessed one element of the array,let us change it. |
03:38 | We shall change 34 to minus 34 in both A and C. |
03:43 | To do this, we simply assign the new value after accessing the element. |
03:47 | So type A in square bracket 2 = minus 34 and for C type C in square bracket 2 comma 3 = minus 34 |
04:06 | Let us check our operations, |
04:11 | So type A in square bracket 2 hit enter then C in square bracket 2 comma 3 |
04:21 | Now that we have accessed and changed a single element, |
04:25 | let us access and change more than one element at a time; first rows and then columns. |
04:31 | Let us access one row of C, say the third row |
04:35 | We do it by saying,C in square bracket 2 and hit enter. |
04:47 | How do we access the last row of C? We could say,so type C within square bracket 4 and hit enter |
04:59 | or as with lists,we could use negative indexing and say,C within bracket -1. |
05:11 | Now, we could change the last row into all zeros, using either C in square bracket -1 = within square bracket five zeros and hit enter |
05:31 | or, we can use,C within square bracket -1=0 |
05:45 | Now, how do we access one column of C? |
05:50 | As with accessing individual elements, the column is the second parameter to be specified (after comma). |
05:58 | The first parameter, is replaced with a semi colon a colon. |
06:05 | This specifies that we want all the elements of that dimension, instead of just one particular element. |
06:12 | We access the third column by saying,type C within square bracket colon comma 2 and hit enter |
06:24 | Pause the video here,try out the following exercise. |
06:34 | Change the last column of C to zeros. |
06:39 | Switch to the terminal for solution. |
06:44 | To change the entire last column of C to zeros, we simply say,C in square bracket colon comma minus 1 = 0 and hit enter |
07:04 | Since A is one dimensional, rows and columns of A don't make much sense. |
07:09 | It has just one row and A of colon gives the whole of A. |
07:15 | So type A within square bracket colon and hit enter |
07:21 | Now pause the video here,try out the following exercise and resume the video. |
07:30 | Change A to [11, 12, 13, 14, 15] |
07:38 | For that Switch to the terminal for solution. |
07:42 | To change A, we say,A within square bracket colon = within square bracket 11 comma 12 comma 13 comma 14 comma 15 and hit enter |
08:06 | Now, that we know how to access, rows and columns of an array, |
08:13 | we shall learn how to access other pieces of an array. |
08:15 | For this purpose, we will be using image arrays. |
08:19 | To read an image into an array, we use the imread command. |
08:25 | We shall use the image squares dot png present in slash home slash fossee . |
08:32 | We first navigate to that path in the OS and see what the image contains. |
08:42 | Let us now read the data in squares dot png into the array I . |
08:48 | Type I=imread within bracket in single quote slash home slash fossee slash squares dot png |
09:07 | We can see the contents of the image, using the command imshow. |
09:13 | We say, imshow within bracket I to see what has been read into I . |
09:19 | So type imshow within bracket I and hit enter |
09:26 | We do not see white and black because, pylab has mapped white and black to different colours. |
09:33 | This can be changed by using a different color map. |
09:37 | To see that I is really, just an array, we say, I, at the prompt,so type I |
09:47 | We see that an array is displayed. |
09:50 | To check the dimensions of any array, we can use dotshape function. |
09:55 | So type I dot shape |
10:00 | As we can see,we got the dimensions of the image. |
10:04 | The image,``squares dot png`` has the dimensions of 300x300. |
10:11 | Our goal for this part of the tutorial would be to get the top-left quadrant of the image. |
10:17 | To do this, we need to access, a few of the rows and a few of the columns of the array. |
10:24 | To access, the third column of C, we said, C with square brackets colon dot 2 |
10:33 | Essentially, we are accessing all the rows in column three of C. |
10:40 | Now, let us modify this to access only the first three rows, of column three of C. |
10:46 | We say, |
10:51 | C within square bracket 0 colon 3 comma 2 gives, the elements of rows indexed from 0 to 3, 3 not included and column indexed 2. |
11:06 | Note that, the index before the colon is included and the index after it is not included in the slice that we have obtained. |
11:16 | This is very similar to the range function, where range returns a list, in which the upper limit or stop value is not included. |
11:26 | So type C within square bracket 0 colon 3 comma 2 and hit enter |
11:37 | Now, if we wish to access the elements of row with index 2, and in columns indexed 0 to 2 (included), we say, |
11:48 | So type C within square bracket 2 comma 0 colon 3 and hit enter |
12:02 | Pause the video here, try out the exercise and resume the video. |
12:09 | First, obtain the elements [22, 23] from C. |
12:17 | Then, obtain the elements [11, 21, 31, 41] from C. |
12:23 | And then the third one and Final one, obtain the elements [21,31, 41, 0]. |
12:32 | Now switch to the terminal. |
12:38 | So type C within square bracket 1 comma 1 colon 3 and hit enter. |
12:47 | C[1, 1 is to 3] gives the elements [22, 23]. |
12:57 | C within square bracket 0 colon 4 comma 0 gives the elements [11, 21, 31, 41]. |
13:15 | C[1 colon 5, 0] gives the elements [21, 31, 41, 0]. |
13:24 | Note that when specifying ranges, if you are starting from the beginning or going up-to the end, the corresponding element may be dropped. |
13:32 | So, in the previous example to obtain [11, 21, 31, 41], we could have simply said, |
13:39 | So type C within square bracket 1 colon 5 comma 0 and hit enter. |
13:50 | We get the elements [21, 31, 41, 0]. |
13:55 | If we skip both the indexes, we get the slice from end to end, as we already know. |
14:02 | So type C within square bracket colon 4 comma 0 and hit enter.So type C within square bracket 1 colon comma 0 and hit enter |
14:16 | Now pause the video here, try out the following exercise and resume the video. |
14:21 | Obtain the elements [[23, 24] comma [33, -34]] from C. |
14:29 | Now switch to the terminal for solution. |
14:32 | Type the command C within square bracket 1 colon 3 comma 2 colon 4 will give us the required elements. |
14:44 | Now, we wish to obtain the top left quarter of the image. |
14:50 | How do we go about doing it? |
14:54 | Since, we know the shape of the image is 300, we know that we need to get the first 150 rows and the first 150 columns. |
15:04 | I[colon 150 comma colon 150] gives us the top-left corner of the image. |
15:12 | So type I within square bracket colon 150 comma colon 150 and hit enter |
15:28 | We use the imshow command to see the slice we obtained in the form of an image and confirm. |
15:36 | So type imshow within bracket I within square bracket colon 150 comma colon 150 and hit enter |
15:58 | Obtain the square in the centre of the image.With the exercise that we are about to do.So please Pause the video here |
16:14 | Now switch to the terminal for solution. |
16:16 | Type the command. Hence, we get the centre of the image. |
16:20 | Our next goal is to compress the image, using a very simple technique. |
16:27 | So type imshow within bracket I and in square bracket 75 colon 225 comma 75 colon 225 and hit enter |
16:53 | The idea is to drop alternate rows and columns of the image and save it. |
17:01 | This way we will be reducing the data to one-fourth of the original data but losing only a little of visual information. |
17:07 | We shall first learn the idea of striding using the smaller array C. |
17:12 | Suppose we wish to access only the odd rows and columns (first, third, fifth). |
17:19 | We do this by,Typing C within square bracket 0 colon 5 colon 2 comma 0 colon 5 colon 2 |
17:36 | if we wish to be explicit, then we say,C colon colon 2 with square bracket in colon colon 2 |
17:52 | This is very similar to the step specified in the range function. |
17:56 | It specifies, the jump or step in which to move, while accessing the elements. |
18:00 | If no step is specified, a default value of 1 is assumed. |
18:05 | Type C in square bracket 1 colon colon 2 comma colon colon 2 |
18:18 | we get the elements, [[21, 23, 0] comma [41, 43, 0]] |
18:25 | Pause the video here, try out the following exercise and resume the video. |
18:31 | Obtain the following. [[12, 0],[42, 0]]then in bracket [[12, 13, 14], [0, 0, 0]] |
18:43 | The solution is on your screen. |
18:47 | Now, that we know how to stride over an array, |
18:50 | we can drop alternate rows and columns out of the image in I. |
18:55 | To see this image, we say,I in square bracket colon colon 2 comma colon colon 2 |
19:09 | Then does not have much data to notice any real difference, but notice that the scale has reduced to show that we have dropped alternate rows and columns. |
19:18 | If you notice carefully, you will be able to observe some blurring near the edges. |
19:22 | To notice this effect more clearly, increase the step to 4. |
19:27 | So type imshow within bracket I with square bracket colon colon 2 comma colon colon 2 and hit enter |
19:47 | Then type imshow then within bracket colon colon 4 and a name colon colon 4 It can even replaced by previous command given. |
20:07 | This brings us to the end of this tutorial. |
20:10 | In this tutorial, we have learnt to,Manipulate single & multi dimensional arrays. |
20:14 | Access and change individual elements by using their index numbers. |
20:19 | Access and change rows and columns of arrays by specifying the row and column numbers. |
20:24 | Slice and stride on arrays. |
20:26 | Read images into arrays and manipulate them. |
20:29 | Here are some self assessment questions for you to solve |
20:33 | Given the array,A = array([12, 15, 18, 21]) , how do we access the element 18 ? |
20:44 | Given the array,B = array([[10, 11, 12, 13],20, 21, 22, 23],[30, 31, 32, 33],[40, 41, 42, 43]]) |
20:52 | Obtain the elements,[[21, 22] comma [31, 32]] |
20:58 | Given the array, C = array([[10, 11, 12, 13], [20, 21, 22, 23]]) |
21:05 | Change the array to C = array([[10, 11, 10, 11], [20, 21, 20, 21]]) |
21:14 | And lets look at the answers, |
21:18 | The element 18 in array A has index number 2. |
21:25 | Hence, we access it as A of 2,answer is A in square bracket 2. |
21:33 | To obtain the centre four numbers in the array B, we say,B of 1 colon 3 comma 1 colon 3 |
21:43 | We can change the elements of array C,by using slicing and striding. B[colon 2, 2 colon] = B[colon 2, colon 2] |
21:58 | Hope you have enjoyed this tutorial and found it useful. |
22:02 | Thank you! |