Difference between revisions of "Python/C3/Accessing-parts-of-arrays/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !Visual Cue !Narration |- | 0:00 | Hello friends and Welcome to the tutorial on 'Accessing pieces of arrays'. |- | 0:05 | At the end of this tutorial, you will be ab…')
 
Line 193: Line 193:
 
|-
 
|-
 
| 7:30
 
| 7:30
| Change <tt>A</tt> to <tt><nowiki>[11, 12, 13, 14, 15]</nowiki></tt>.
+
| Change A to [11, 12, 13, 14, 15]  
  
 
|-
 
|-
Line 217: Line 217:
 
|-
 
|-
 
| 8:19
 
| 8:19
| To read an image into an array, we use the <tt>imread</tt> command.  
+
| To read an image into an array, we use the imread command.  
  
 
|-
 
|-
 
| 8:25
 
| 8:25
| We shall use the image <tt>squares dot png</tt> present in <tt>slash home slash fossee</tt>.  
+
| We shall use the image squares dot png present in slash home slash fossee .  
  
 
|-
 
|-
Line 229: Line 229:
 
|-
 
|-
 
| 8:42
 
| 8:42
| Let us now read the data in <tt>squares dot png</tt> into the array <tt>I</tt>.
+
| Let us now read the data in squares dot png into the array I .
  
 
|-
 
|-
Line 237: Line 237:
 
|-
 
|-
 
| 9:07
 
| 9:07
| We can see the contents of the image, using the command <tt>imshow</tt>.
+
| We can see the contents of the image, using the command imshow.
  
 
|-
 
|-
 
| 9:13
 
| 9:13
|We say, imshow within bracket I to see what has been read into <tt>I</tt>.
+
|We say, imshow within bracket I to see what has been read into I .
  
 
|-
 
|-
Line 249: Line 249:
 
|-
 
|-
 
| 9:26
 
| 9:26
| We do not see white and black because, <tt>pylab</tt> has mapped white and black to different colors.  
+
| We do not see white and black because, pylab has mapped white and black to different colors.  
  
 
|-
 
|-
Line 257: Line 257:
 
|-
 
|-
 
| 9:37
 
| 9:37
| To see that <tt>I</tt> is really, just an array, we say, I, at the prompt,so type I
+
| To see that I is really, just an array, we say, I, at the prompt,so type I
  
 
|-
 
|-
Line 265: Line 265:
 
|-
 
|-
 
| 9:50
 
| 9:50
| To check the dimensions of any array, we can use <tt>dotshape</tt> function.
+
| To check the dimensions of any array, we can use dotshape function.
  
 
|-
 
|-
Line 289: Line 289:
 
|-
 
|-
 
| 10:24
 
| 10:24
| To access, the third column of C, we said, <tt><nowiki>C with square brackets colon dot 2</nowiki></tt>.
+
| To access, the third column of C, we said, C with square brackets colon dot 2
  
 
|-
 
|-
Line 314: Line 314:
 
|-
 
|-
 
| 11:16
 
| 11:16
| This is very similar to the </nowiki><tt>range</tt> function, where <tt>range</tt> returns a list, in which the upper limit or stop value is not included.
+
| This is very similar to the range function, where range returns a list, in which the upper limit or stop value is not included.
  
 
|-
 
|-
Line 403: Line 403:
 
|-
 
|-
 
| 14:32
 
| 14:32
| Type the command<nowiki>C within square bracket 1 colon 3 comma 2 colon 4 will give us the required elements.</nowiki>
+
| Type the command C within square bracket 1 colon 3 comma 2 colon 4 will give us the required elements.  
  
 
|-
 
|-
Line 419: Line 419:
 
|-
 
|-
 
| 15:04
 
| 15:04
| <nowiki>I[colon 150 comma colon 150] gives us the top-left corner of the image.</nowiki>
+
| I[colon 150 comma colon 150] gives us the top-left corner of the image.
  
 
|-
 
|-
Line 427: Line 427:
 
|-
 
|-
 
|  15:28
 
|  15:28
| We use the <tt>imshow</tt> command to see the slice we obtained in the form of an image and confirm.
+
| We use the imshow command to see the slice we obtained in the form of an image and confirm.
  
 
|-
 
|-
Line 435: Line 435:
 
|-
 
|-
 
| 15:58
 
| 15:58
|Obtain the square in the center of the image.With the exercise that we are about to do.So please Pause the video here
+
|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
 
| 16:14
Line 442: Line 442:
 
|-
 
|-
 
|  16:16
 
|  16:16
| Type the command. Hence, we get the center of the image.
+
| Type the command. Hence, we get the centre of the image.
  
 
|-
 
|-
Line 478: Line 478:
 
|-
 
|-
 
| 17:52
 
| 17:52
| This is very similar to the step specified in the <tt>range</tt> function.  
+
| This is very similar to the step specified in the range function.  
  
 
|-
 
|-
Line 502: Line 502:
 
|-
 
|-
 
| 18:31
 
| 18:31
| <nowiki>Obtain the following. [[12, 0],[42, 0]]then in bracket [[12, 13, 14], [0, 0, 0]]</nowiki>
+
| Obtain the following. [[12, 0],[42, 0]]then in bracket [[12, 13, 14], [0, 0, 0]]
  
 
|-
 
|-
Line 570: Line 570:
 
|-
 
|-
 
| 20:33
 
| 20:33
| 1.Given the array, <tt><nowiki>A = array([12, 15, 18, 21])</nowiki></tt>, how do we access the element <tt>18</tt>?
+
| 1.Given the array,A = array([12, 15, 18, 21]) , how do we access the element 18 ?
  
 
|-
 
|-
Line 576: Line 576:
 
| 2. Given the array,
 
| 2. Given the array,
  
  <nowiki>B = array([[10, 11, 12, 13],</nowiki>
+
  B = array([[10, 11, 12, 13],
            <nowiki>[20, 21, 22, 23],</nowiki>
+
          20, 21, 22, 23],
            <nowiki>[30, 31, 32, 33],</nowiki>
+
          [30, 31, 32, 33],
            <nowiki>[40, 41, 42, 43]])</nowiki>
+
          [40, 41, 42, 43]])
  
 
|-
 
|-
 
| 20:52
 
| 20:52
| Obtain the elements, <tt><nowiki>[[21, 22] comma [31, 32]]</nowiki></tt>
+
| Obtain the elements,[[21, 22] comma [31, 32]]  
 
+
 
|-
 
|-
 
| 20:58
 
| 20:58
 
| 3. Given the array,
 
| 3. Given the array,
  
<nowiki>C = array([[10, 11, 12, 13],</nowiki>
+
  C = array([[10, 11, 12, 13],  
            <nowiki>[20, 21, 22, 23]])</nowiki>
+
          [20, 21, 22, 23]])  
  
 
|-
 
|-
Line 596: Line 595:
 
| Change the array to
 
| Change the array to
  
<nowiki>C = array([[10, 11, 10, 11],</nowiki>
+
  C = array([[10, 11, 10, 11],  
             <nowiki>[20, 21, 20, 21]])</nowiki>
+
             [20, 21, 20, 21]])  
  
 
|-
 
|-
Line 613: Line 612:
 
|-
 
|-
 
| 21:33
 
| 21:33
| 2. To obtain the center four numbers in the array B, we say,B of 1 colon 3 comma 1 colon 3</nowiki>
+
| 2. To obtain the centre four numbers in the array B, we say,B of 1 colon 3 comma 1 colon 3  
  
 
|-
 
|-
Line 619: Line 618:
 
| 3. We can change the elements of array C,by using slicing and striding
 
| 3. We can change the elements of array C,by using slicing and striding
  
  <nowiki>B[colon 2, 2 colon] = B[colon 2, colon 2]</nowiki>
+
  B[colon 2, 2 colon] = B[colon 2, colon 2]  
  
 
|-
 
|-

Revision as of 15:19, 18 March 2013

Visual Cue Narration
0:00 Hello friends and Welcome to the tutorial on 'Accessing pieces of arrays'.
0:05 At the end of this tutorial, you will be able to,
  1. Access and change individual elements of arrays, both one dimensional and multi-dimensional.
  2. Access and change rows and columns of arrays.
  3. Access and change other chunks from an array, using slicing and striding.
  4. And read images into arrays and perform processing on them, using simple array manipulations.
0:32 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with arrays".
0:40 Now open the terminal and type ipython hypen pylab
0:50 Let us begin with the help of an example.
0:54 Let us have two arrays, A and C, as the sample arrays that we will use to work through this tutorial.
1:08 Let us begin with the most elementary thing, accessing individual elements.
1:14 Also, let us first do it with the one-dimensional array A, and then do the same thing with the two-dimensional array.
1:30 Type A = array within bracket square bracket 12 comma 23 coma 34 comma 45 comma 56
1:45 C = array within bracket square bracket 11 comma 12 comma 13 comma 14 comma 15 comma
          within square bracket 21 to 25 comma 
          within square bracket 31 to 35 comma 
          within square bracket 41 to 45 comma 
          within square bracket 51 to 55 then close the bracket and hit enter.
2:36 To access, the element 34 in array A, we say, A of 2, note that we are using square brackets.
2:44 So type A within square bracket and hit enter.
2:51 Like lists, indexing starts from 0 in arrays, too.
2:56 So, 34, the third element has the index 2.
3:03 Now, let us access the element 34 from C.
3:06 To do this, we say, C of 2,3.
3:11 So type C within square bracket 2 comma 3 and hit enter
3: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.
3:31 Now, that we have accessed one element of the array,let us change it.
3:38 We shall change 34 to minus 34 in both A and C.
3:43 To do this, we simply assign the new value after accessing the element.
3:47 So type A within square bracket 2 = minus 34 and for C type C within square bracket 2 comma 3 = minus 34
4:06 Let us check our operations,
4:11 So type A within square bracket 2 hit enter and C within square bracket 2 comma 3
4:21 Now that we have accessed and changed a single element,
4:25 let us access and change more than one element at a time; first rows and then columns.
4:31 Let us access one row of C, say the third row
4:35 We do it by saying,C within square bracket 2 and hit enter.
4:47 How do we access the last row of C? We could say,so type C within square bracket 4 and hit enter
4:59 or as with lists,we could use negative indexing and say,C within bracket -1.
5:11 Now, we could change the last row into all zeros, using either C within square bracket -1 = within square bracket five zeros and hit enter
5:31 or, we can use,C within square bracket -1=0
5:45 Now, how do we access one column of C?
5:50 As with accessing individual elements, the column is the second parameter to be specified (after comma).
5:58 The first parameter, is replaced with a semi colon a colon.
6:05 This specifies that we want all the elements of that dimension, instead of just one particular element.
6:12 We access the third column by saying,type C within square bracket colon comma 2 and hit enter
6:24 Pause the video here,try out the following exercise.
6:34 Change the last column of C to zeros.
6:39 Switch to the terminal for solution.
6:44 To change the entire last column of C to zeros, we simply say,

C within square bracket colon comma minus -1 = 0 and hit enter

7:04 Since A is one dimensional, rows and columns of A don't make much sense.
7:09 It has just one row and A of colon gives the whole of A.
7:15 So type A within square bracket colon and hit enter
7:21 Now pause the video here,try out the following exercise and resume the video.
7:30 Change A to [11, 12, 13, 14, 15]
7:38 Switch to the terminal for solution.
7: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
8:06 Now, that we know how to access, rows and columns of an array,
8:13 we shall learn how to access other pieces of an array.
8:15 For this purpose, we will be using image arrays.
8:19 To read an image into an array, we use the imread command.
8:25 We shall use the image squares dot png present in slash home slash fossee .
8:32 We first navigate to that path in the OS and see what the image contains.
8:42 Let us now read the data in squares dot png into the array I .
8:48 Type I=imread within bracket in single quote slash home slash fossee slash squares dot png
9:07 We can see the contents of the image, using the command imshow.
9:13 We say, imshow within bracket I to see what has been read into I .
9:19 So type imshow within bracket I and hit enter
9:26 We do not see white and black because, pylab has mapped white and black to different colors.
9:33 This can be changed by using a different color map.
9:37 To see that I is really, just an array, we say, I, at the prompt,so type I
9:47 We see that an array is displayed.
9:50 To check the dimensions of any array, we can use dotshape function.
9: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 with 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 with 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 within 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 also be 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, 1.Manipulate single & multi dimensional arrays.
20:14 2.Access and change individual elements by using their index numbers.
20:19 3.Access and change rows and columns of arrays by specifying the row and column numbers.
20:24 4.Slice and stride on arrays.
20:26 5.Read images into arrays and manipulate them.
20:29 Here are some self assessment questions for you to solve
20:33 1.Given the array,A = array([12, 15, 18, 21]) , how do we access the element 18 ?
20:44 2. 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 3. 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 1.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 2. To obtain the centre four numbers in the array B, we say,B of 1 colon 3 comma 1 colon 3
21:43 3. 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!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha