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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 46: Line 46:
 
|1:45
 
|1:45
 
|C = array within bracket square bracket 11 comma 12 comma 13 comma 14 comma 15 comma  
 
|C = array within bracket square bracket 11 comma 12 comma 13 comma 14 comma 15 comma  
          within square bracket 21 to 25 comma  
+
            Next in square bracket 21 to 25 comma  
           within square bracket 31 to 35 comma  
+
           Next one in square bracket 31 to 35 comma  
           within square bracket 41 to 45 comma  
+
           Next one is square bracket 41 to 45 comma  
           within square bracket 51 to 55 then close the bracket and hit enter.
+
           And the final one is  square bracket 51 to 55 then close the bracket and hit enter.
  
 
|-
 
|-
Line 57: Line 57:
 
|-
 
|-
 
|2:44
 
|2:44
|So type A within square bracket and hit enter.
+
|So type A within square bracket2 and hit enter.
  
 
|-
 
|-
 
| 2:51
 
| 2:51
| Like lists, indexing starts from 0 in arrays, too.  
+
| Like lists, indexing starts from 0 in arrays,.  
  
 
|-
 
|-
 
| 2:56
 
| 2:56
| So, 34, the third element has the index 2.
+
| So, 34, the third element have the index 2.
  
 
|-
 
|-
Line 97: Line 97:
 
|-
 
|-
 
|3:47
 
|3:47
|So type A within square bracket 2 = minus 34 and for C type C within square bracket 2 comma 3 = minus 34
+
|So type A in square bracket 2 = minus 34 and for C type C in square bracket 2 comma 3 = minus 34
  
 
|-
 
|-
Line 105: Line 105:
 
|-
 
|-
 
|4:11
 
|4:11
|So type A within square bracket 2  hit enter and C within square bracket 2 comma 3
+
|So type A in square bracket 2  hit enter then C in square bracket 2 comma 3
  
 
|-
 
|-
Line 120: Line 120:
 
|-
 
|-
 
| 4:35
 
| 4:35
| We do it by saying,C within square bracket 2 and hit enter.
+
| We do it by saying,C in square bracket 2 and hit enter.
  
 
|-
 
|-
Line 132: Line 132:
 
|-
 
|-
 
| 5:11
 
| 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
+
| 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
  
 
|-
 
|-
Line 173: Line 173:
 
| 6:44
 
| 6:44
 
| To change the entire last column of C to zeros, we simply say,
 
| 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
+
C in square bracket colon comma minus 1 = 0 and hit enter
  
 
|-
 
|-
Line 197: Line 197:
 
|-
 
|-
 
| 7:38
 
| 7:38
| Switch to the terminal for solution.
+
| For that Switch to the terminal for solution.
  
 
|-
 
|-
Line 249: Line 249:
 
|-
 
|-
 
| 9:26
 
| 9:26
| We do not see white and black because,  pylab  has mapped white and black to different colors.  
+
| We do not see white and black because,  pylab  has mapped white and black to different colours.  
  
 
|-
 
|-
Line 450: Line 450:
 
|-  
 
|-  
 
|16:27
 
|16:27
|So type imshow within bracket I and with square bracket 75 colon 225 comma 75 colon 225 and hit enter
+
|So type imshow within bracket I and in square bracket 75 colon 225 comma 75 colon 225 and hit enter
  
 
|-
 
|-
Line 490: Line 490:
 
|-
 
|-
 
|18:05
 
|18:05
|Type C with square bracket  1 colon colon 2 comma colon colon 2
+
|Type C in square bracket  1 colon colon 2 comma colon colon 2
  
 
|-
 
|-
Line 518: Line 518:
 
|-
 
|-
 
| 18:55
 
| 18:55
| To see this image, we say,I within square bracket  colon colon 2 comma colon colon 2
+
| To see this image, we say,I in square bracket  colon colon 2 comma colon colon 2
  
 
|-
 
|-
Line 538: Line 538:
 
|-
 
|-
 
|19:47
 
|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.
+
|Then type imshow then within bracket colon colon 4 and a name colon colon 4 It can even  replaced by previous command given.
  
 
|-
 
|-
Line 546: Line 546:
 
|-
 
|-
 
| 20:10
 
| 20:10
| In this tutorial, we have learnt to, 1.Manipulate single & multi dimensional arrays.
+
| In this tutorial, we have learnt to,Manipulate single & multi dimensional arrays.
  
 
|-
 
|-
 
| 20:14
 
| 20:14
| 2.Access and change individual elements by using their index numbers.
+
|Access and change individual elements by using their index numbers.
  
 
|-
 
|-
 
| 20:19
 
| 20:19
| 3.Access and change rows and columns of arrays by specifying the row and column numbers.
+
| Access and change rows and columns of arrays by specifying the row and column numbers.
  
 
|-
 
|-
 
| 20:24
 
| 20:24
| 4.Slice and stride on arrays.
+
| Slice and stride on arrays.
  
 
|-
 
|-
 
| 20:26
 
| 20:26
| 5.Read images into arrays and manipulate them.
+
| Read images into arrays and manipulate them.
  
 
|-
 
|-

Revision as of 12:01, 19 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
           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.
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 bracket2 and hit enter.
2:51 Like lists, indexing starts from 0 in arrays,.
2:56 So, 34, the third element have 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 in square bracket 2 = minus 34 and for C type C in square bracket 2 comma 3 = minus 34
4:06 Let us check our operations,
4:11 So type A in square bracket 2 hit enter then C in 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 in 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 in 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 in 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 For that 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 colours.
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 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 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