accessing_parts_of_arrays/script.rst
changeset 522 d33698326409
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
       
     1 .. Objectives
       
     2 .. ----------
       
     3    
       
     4    .. By the end of this tutorial, you will be able to:
       
     5    
       
     6    ..   1. Access and change individual elements of arrays, both one
       
     7    ..   dimensional and multi-dimensional.
       
     8    ..   2. Access and change rows and columns of arrays. 
       
     9    ..   3. Access and change other chunks from an array, using slicing
       
    10    ..   and striding. 
       
    11    ..   4. Read images into arrays and perform processing on them, using
       
    12    ..   simple array manipulations. 
       
    13 
       
    14 .. Prerequisites
       
    15 .. -------------
       
    16 
       
    17 ..   1. getting started with arrays
       
    18 
       
    19 .. #[anand: internal reviewer not mentioned]     
       
    20 .. Author              : Puneeth
       
    21    Internal Reviewer   : 
       
    22    External Reviewer   :
       
    23    Language Reviewer   : Bhanukiran
       
    24    Checklist OK?       : <06-11-2010, Anand,  OK> [2010-10-05]
       
    25 
       
    26 Script
       
    27 ------
       
    28 
       
    29 {{{ Screen shows welcome slide }}}
       
    30 
       
    31 Welcome to the tutorial on accessing pieces of arrays
       
    32 
       
    33 {{{ Show the outline for this tutorial }}} 
       
    34 
       
    35 In this tutorial we shall learn to access individual elements of
       
    36 arrays, get rows and columns and other chunks of arrays using
       
    37 slicing and striding. 
       
    38 
       
    39 {{{ switch back to the terminal }}}
       
    40 
       
    41 As usual, we start IPython, using 
       
    42 ::
       
    43 
       
    44   ipython -pylab 
       
    45 
       
    46 
       
    47 {{ Show the slide with the arrays, A and C }}
       
    48 
       
    49 Let us have two arrays, A and C, as the sample arrays that we will
       
    50 use to work through this tutorial. 
       
    51 
       
    52 ::
       
    53 
       
    54   A = array([12, 23, 34, 45, 56])
       
    55 
       
    56   C = array([[11, 12, 13, 14, 15],
       
    57              [21, 22, 23, 24, 25],
       
    58              [31, 32, 33, 34, 35],
       
    59              [41, 42, 43, 44, 45],
       
    60              [51, 52, 53, 54, 55]])
       
    61 
       
    62 Pause the video here and make sure you have the arrays A and C,
       
    63 typed in correctly.
       
    64 
       
    65 {{{ Pause the recording and type the arrays A,C }}}
       
    66 
       
    67 Let us begin with the most elementary thing, accessing individual
       
    68 elements. Also, let us first do it with the one-dimensional array
       
    69 A, and then do the same thing with the two-dimensional array. 
       
    70 
       
    71 To access, the element 34 in A, we say, 
       
    72 
       
    73 ::
       
    74 
       
    75   A[2]
       
    76 
       
    77 A of 2, note that we are using square brackets.
       
    78 
       
    79 Like lists, indexing starts from 0 in arrays, too. So, 34, the
       
    80 third element has the index 2. 
       
    81 
       
    82 Now, let us access the element 34 from C. To do this, we say
       
    83 ::
       
    84 
       
    85   C[2, 3]
       
    86 
       
    87 C of 2,3.
       
    88 
       
    89 34 is in the third row and the fourth column, and since indexing
       
    90 begins from zero, the row index is 2 and column index is 3. 
       
    91 
       
    92 Now, that we have accessed one element of the array, let us change
       
    93 it. We shall change the 34 to -34 in both A and C. To do this, we
       
    94 simply assign the new value after accessing the element. 
       
    95 ::
       
    96 
       
    97   A[2] = -34
       
    98   C[2, 3] = -34
       
    99 
       
   100 Now that we have accessed and changed a single element, let us
       
   101 access and change more than one element at a time; first rows and
       
   102 then columns.
       
   103 
       
   104 Let us access one row of C, say the third row. We do it by saying, 
       
   105 ::
       
   106 
       
   107   C[2] 
       
   108 
       
   109 How do we access the last row of C? We could say,
       
   110 ::
       
   111 
       
   112   C[4] 
       
   113 
       
   114 for the fifth row, or as with lists, use negative indexing and say
       
   115 ::
       
   116 
       
   117   C[-1]
       
   118 
       
   119 Now, we could change the last row into all zeros, using either 
       
   120 ::
       
   121 
       
   122   C[-1] = [0, 0, 0, 0, 0]
       
   123 
       
   124 or 
       
   125 
       
   126 ::
       
   127   
       
   128   C[-1] = 0
       
   129 
       
   130 Now, how do we access one column of C? As with accessing individual
       
   131 elements, the column is the second parameter to be specified (after
       
   132 the comma). The first parameter, is replaced with a ``:``. This
       
   133 specifies that we want all the elements of that dimension, instead of
       
   134 just one particular element. We access the third column by
       
   135 
       
   136 ::
       
   137   
       
   138   C[:, 2]
       
   139 
       
   140 Following is an exercise that you must do. 
       
   141 
       
   142 {{ show slide containing Question 1}} 
       
   143 
       
   144 %%1%% Change the last column of C to zeroes. 
       
   145 
       
   146 Please, pause the video here. Do the exercises and then continue. 
       
   147 
       
   148 ::
       
   149   
       
   150   C[:, -1] = 0
       
   151 
       
   152 Since A is one dimensional, rows and columns of A don't make much
       
   153 sense. It has just one row and 
       
   154 ::
       
   155 
       
   156   A[:] 
       
   157 
       
   158 gives the whole of A. 
       
   159 
       
   160 Following is an exercise that you must do. 
       
   161 
       
   162 {{ show slide containing Question 2}} 
       
   163 
       
   164 %%2%% Change ``A`` to ``[11, 12, 13, 14, 15]``. 
       
   165 
       
   166 Please, pause the video here. Do the exercises and then continue. 
       
   167 
       
   168 To change A, we say
       
   169 ::
       
   170 
       
   171   A[:] = [11, 12, 13, 14, 15]
       
   172 
       
   173 Now, that we know how to access, rows and columns of an array, we
       
   174 shall learn how to access other pieces of an array. For this
       
   175 purpose, we will be using image arrays. 
       
   176 
       
   177 To read an image into an array, we use the ``imread`` command. We
       
   178 shall use the image ``squares.png`` present in ``/home/fossee``. We
       
   179 shall first navigate to that path in the OS and see what the image
       
   180 contains. 
       
   181 
       
   182 {{{ switch to the browser and show the image }}}
       
   183 
       
   184 {{{ switch back to the ipython terminal }}}
       
   185 
       
   186 Let us now read the data in ``squares.png`` into the array ``I``. 
       
   187 ::
       
   188 
       
   189   I = imread('/home/fossee/squares.png')
       
   190 
       
   191 We can see the contents of the image, using the command
       
   192 ``imshow``. We say, 
       
   193 ::
       
   194 
       
   195   imshow(I) 
       
   196 
       
   197 to see what has been read into ``I``. We do not see white and black
       
   198 because, ``pylab`` has mapped white and black to different
       
   199 colors. This can be changed by using a different colormap. 
       
   200 
       
   201 To see that ``I`` is really, just an array, we say, 
       
   202 ::
       
   203 
       
   204   I 
       
   205 
       
   206 at the prompt, and see that an array is displayed. 
       
   207 
       
   208 To check the dimensions of any array, we can use ``.shape``. We say
       
   209 
       
   210 ::
       
   211 
       
   212   I.shape 
       
   213 
       
   214 to get the dimensions of the image. As we can see, ``squares.png``
       
   215 has the dimensions of 300x300. 
       
   216 
       
   217 Our goal for this part of the tutorial would be to get the
       
   218 top-left quadrant of the image. To do this, we need to access, a
       
   219 few of the rows and a few of the columns of the array. 
       
   220 
       
   221 To access, the third column of C, we said, ``C[:, 2]``. Essentially,
       
   222 we are accessing all the rows in column three of C. Now, let us
       
   223 modify this to access only the first three rows, of column three
       
   224 of C. 
       
   225 
       
   226 We say, 
       
   227 ::
       
   228 
       
   229   C[0:3, 2]
       
   230 
       
   231 to get the elements of rows indexed from 0 to 3, 3 not included
       
   232 and column indexed 2. Note that, the index before the colon is
       
   233 included and the index after it is not included in the slice that
       
   234 we have obtained. This is very similar to the ``range`` function,
       
   235 where ``range`` returns a list, in which the upper limit or stop
       
   236 value is not included.
       
   237 
       
   238 Now, if we wish to access the elements of row with index 2, and in
       
   239 columns indexed 0 to 2 (included), we say, 
       
   240 ::
       
   241 
       
   242   C[2, 0:3]
       
   243 
       
   244 Following is an exercise that you must do. 
       
   245 
       
   246 {{ show slide containing Question 3 }} 
       
   247 
       
   248 %%3%% First, obtain the elements [22, 23] from C. Then, obtain the
       
   249 elements [11, 21, 31, 41] from C. Finally, obtain the elements [21,
       
   250 31, 41, 0]. 
       
   251 
       
   252 Please, pause the video here. Do the exercises and then continue. 
       
   253 
       
   254 {{ show slide containing Solution 3 }}
       
   255 
       
   256 ::
       
   257 
       
   258   C[1, 1:3] 
       
   259 
       
   260 gives the elements [22, 23]
       
   261 ::
       
   262 
       
   263   C[0:4, 0]
       
   264 
       
   265 gives the elements [11, 21, 31, 41]
       
   266 ::
       
   267 
       
   268   C[1:5, 0]
       
   269 
       
   270 gives the elements [21, 31, 41, 0]
       
   271 
       
   272 Note that when specifying ranges, if you are starting from the
       
   273 beginning or going up-to the end, the corresponding element may be
       
   274 dropped. So, in the previous example to obtain [11, 21, 31, 41], we
       
   275 could have simply said, ::
       
   276 
       
   277   C[:4, 0]
       
   278 
       
   279 and 
       
   280 ::
       
   281 
       
   282   C[1:, 0]
       
   283 
       
   284 gives the elements [21, 31, 41, 0]. If we skip both the indexes,
       
   285 we get the slice from end to end, as we already know. 
       
   286 
       
   287 Following is an exercise that you must do. 
       
   288 
       
   289 {{ show slide containing Question 4 }} 
       
   290 
       
   291 %%4%% Obtain the elements [[23, 24], [33, -34]] from C. 
       
   292 
       
   293 Please, pause the video here. Do the exercises and then continue. 
       
   294 
       
   295 {{ show slide containing Solution 4 }} 
       
   296 
       
   297 ::
       
   298 
       
   299   C[1:3, 2:4] 
       
   300 
       
   301 gives us the elements, [[23, 24], [33, -34]]. 
       
   302 
       
   303 Now, we wish to obtain the top left quarter of the image. How do
       
   304 we go about doing it? Since, we know the shape of the image to be
       
   305 300, we know that we need to get the first 150 rows and first 150
       
   306 columns. 
       
   307 ::
       
   308 
       
   309   I[:150, :150]
       
   310 
       
   311 gives us the top-left corner of the image. 
       
   312 
       
   313 We use the ``imshow`` command to see the slice we obtained in the
       
   314 form of an image and confirm. 
       
   315 ::
       
   316 
       
   317   imshow(I[:150, :150])
       
   318 
       
   319 Following is an exercise that you must do. 
       
   320 
       
   321 {{ show slide containing Question 5 }} 
       
   322 
       
   323 %%5%% Obtain the square in the center of the image.
       
   324 
       
   325 Please, pause the video here. Do the exercises and then continue. 
       
   326 
       
   327 {{ show slide containing Solution 5 }} 
       
   328 
       
   329 ::
       
   330 
       
   331   imshow(I[75:225, 75:225])
       
   332 
       
   333 Our next goal is to compress the image, using a very simple
       
   334 technique to reduce the space that the image takes on disk while
       
   335 not compromising too heavily on the image quality. The idea is to
       
   336 drop alternate rows and columns of the image and save it. This way
       
   337 we will be reducing the data to a fourth of the original data but
       
   338 losing only so much of visual information. 
       
   339 
       
   340 We shall first learn the idea of striding using the smaller array
       
   341 C. Suppose we wish to access only the odd rows and columns (first,
       
   342 third, fifth). We do this by, 
       
   343 ::
       
   344 
       
   345   C[0:5:2, 0:5:2]
       
   346 
       
   347 if we wish to be explicit, or simply, 
       
   348 ::
       
   349 
       
   350   C[::2, ::2]
       
   351 
       
   352 This is very similar to the step specified to the ``range``
       
   353 function. It specifies, the jump or step in which to move, while
       
   354 accessing the elements. If no step is specified, a default value
       
   355 of 1 is assumed. 
       
   356 ::
       
   357 
       
   358   C[1::2, ::2] 
       
   359 
       
   360 gives the elements, [[21, 23, 0], [41, 43, 0]]
       
   361 
       
   362 {{ show slide containing Question 6 }} 
       
   363 
       
   364 Following is an exercise that you must do. 
       
   365 
       
   366 %%6%% Obtain the following. 
       
   367 [[12, 0], [42, 0]]
       
   368 [[12, 13, 14], [0, 0, 0]]
       
   369 
       
   370 Please, pause the video here. Do the exercises and then continue. 
       
   371 
       
   372 {{ show slide containing Solution 6 }} 
       
   373 
       
   374 ::
       
   375 
       
   376   C[::3, 1::3]
       
   377 
       
   378 gives the elements [[12, 0], [42, 0]]
       
   379 ::
       
   380 
       
   381   C[::4, 1:4]
       
   382 
       
   383 gives the elements [[12, 13, 14], [0, 0, 0]]
       
   384 
       
   385 Now, that we know how to stride over an array, we can drop
       
   386 alternate rows and columns out of the image in I. 
       
   387 ::
       
   388 
       
   389   I[::2, ::2]
       
   390 
       
   391 To see this image, we say, 
       
   392 ::
       
   393 
       
   394   imshow(I[::2, ::2])
       
   395 
       
   396 This does not have much data to notice any real difference, but
       
   397 notice that the scale has reduced to show that we have dropped
       
   398 alternate rows and columns. If you notice carefully, you will be
       
   399 able to observe some blurring near the edges. To notice this
       
   400 effect more clearly, increase the step to 4. 
       
   401 ::
       
   402 
       
   403   imshow(I[::4, ::4])
       
   404 
       
   405 {{{ show summary slide }}}
       
   406 
       
   407 That brings us to the end of this tutorial. In this tutorial, we
       
   408 have learnt to access parts of arrays, specifically individual
       
   409 elements, rows and columns and larger pieces of arrays. We have
       
   410 also learnt how to modify arrays, element wise or in larger
       
   411 pieces.
       
   412 
       
   413 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   414 
       
   415 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   416 
       
   417 Hope you have enjoyed and found it useful.
       
   418 Thank you!
       
   419 
       
   420 .. 
       
   421    Local Variables:
       
   422    mode: rst
       
   423    indent-tabs-mode: nil
       
   424    sentence-end-double-space: nil
       
   425    fill-column: 70
       
   426    End: