accessing-pieces-arrays.rst
changeset 144 476ea1730aee
equal deleted inserted replaced
143:e75538bca178 144:476ea1730aee
       
     1 ========
       
     2  Script
       
     3 ========
       
     4 
       
     5 
       
     6 {{{ Screen shows welcome slide }}}
       
     7 
       
     8 Welcome to the tutorial on accessing pieces of arrays
       
     9 
       
    10 {{{ Show the outline for this tutorial }}} 
       
    11 
       
    12 In this tutorial we shall learn to access individual elements of
       
    13 arrays, get rows and columns and other chunks of arrays using
       
    14 slicing and striding. 
       
    15 
       
    16 {{{ switch back to the terminal }}}
       
    17 
       
    18 As usual, we start IPython, using 
       
    19 ::
       
    20 
       
    21   ipython -pylab 
       
    22 
       
    23 Let us have two arrays, A and C, as the sample arrays that we will
       
    24 use to work through this tutorial. 
       
    25 
       
    26 ::
       
    27 
       
    28   A = array([12, 23, 34, 45, 56])
       
    29 
       
    30   C = array([[11, 12, 13, 14, 15],
       
    31              [21, 22, 23, 24, 25],
       
    32              [31, 32, 33, 34, 35],
       
    33              [41, 42, 43, 44, 45],
       
    34              [51, 52, 53, 54, 55]])
       
    35 
       
    36 Pause the video here and make sure you have the arrays A and C,
       
    37 typed in correctly.
       
    38 
       
    39 Let us begin with the most elementary thing, accessing individual
       
    40 elements. Also, let us first do it with the one-dimensional array
       
    41 A, and then do the same thing with the two-dimensional array. 
       
    42 
       
    43 To access, the element 34 in A, we say, 
       
    44 
       
    45 ::
       
    46 
       
    47   A[1]
       
    48 
       
    49 Like lists, indexing starts from 0 in arrays, too. So, 34, the
       
    50 third element has the index 2. 
       
    51 
       
    52 Now, let us access the element 34 from C. To do this, we say
       
    53 ::
       
    54 
       
    55   C[2, 3]
       
    56 
       
    57 34 is in the third row and the fourth column, and since indexing
       
    58 begins from zero, the row index is 2 and column index is 3. 
       
    59 
       
    60 Now, that we have accessed one element of the array, let us change
       
    61 it. We shall change the 34 to -34 in both A and C. To do this, we
       
    62 simply assign the new value after accessing the element. 
       
    63 ::
       
    64 
       
    65   A[2] = -34
       
    66   C[2, 3] = -34
       
    67 
       
    68 Now that we have accessed and changed a single element, let us
       
    69 access and change more than one element at a time; first rows and
       
    70 then columns.
       
    71 
       
    72 Let us access one row of C, say the third row. We do it by saying, 
       
    73 ::
       
    74 
       
    75   C[2] 
       
    76 
       
    77 How do we access the last row of C? We could say,
       
    78 ::
       
    79 
       
    80   C[4] 
       
    81 
       
    82 for the fifth row, or as with lists, use negative indexing and say
       
    83 ::
       
    84 
       
    85   C[-1]
       
    86 
       
    87 Now, we could change the last row into all zeros, using either 
       
    88 ::
       
    89 
       
    90   C[-1] = [0, 0, 0, 0, 0]
       
    91 
       
    92 or 
       
    93 
       
    94 ::
       
    95   
       
    96   C[-1] = 0
       
    97 
       
    98 Now, how do we access one column of C? As with accessing
       
    99 individual elements, the column is the second parameter to be
       
   100 specified (after the comma). The first parameter, is now replaced
       
   101 with a ``:`` to say, that we want all the elements of that
       
   102 dimension, instead of one particular element. We access the third
       
   103 column by
       
   104 
       
   105 ::
       
   106   
       
   107   C[:, 2]
       
   108 
       
   109 %%1%% Pause the video here and change the last column of C to
       
   110 zeroes and then resume the video.
       
   111 
       
   112 ::
       
   113   
       
   114   C[:, -1] = 0
       
   115 
       
   116 Since A is one dimensional, rows and columns of A don't make much
       
   117 sense. It has just one row and 
       
   118 ::
       
   119 
       
   120   A[:] 
       
   121 
       
   122 gives the whole of A. 
       
   123 
       
   124 %%2%% Pause the video here and change ``A`` to ``[11, 12, 13, 14, 15]``
       
   125 and then resume the video. 
       
   126 
       
   127 To change A, we say
       
   128 ::
       
   129 
       
   130   A[:] = [11, 12, 13, 14, 15]
       
   131 
       
   132 Now, that we know how to access, rows and columns of an array, we
       
   133 shall learn how to access other pieces of an array. For this
       
   134 purpose, we will be using image arrays. 
       
   135 
       
   136 To read an image into an array, we use the ``imread`` command. We
       
   137 shall use the image ``squares.png`` present in ``/home/fossee``. We
       
   138 shall first navigate to that path in the OS and see what the image
       
   139 contains. 
       
   140 
       
   141 {{{ switch to the browser and show the image }}}
       
   142 
       
   143 {{{ switch back to the ipython terminal }}}
       
   144 
       
   145 Let us now read the data in ``squares.png`` into the array ``I``. 
       
   146 ::
       
   147 
       
   148   I = imread('/home/fossee/squares.png')
       
   149 
       
   150 We can see the contents of the image, using the command
       
   151 ``imshow``. We say, 
       
   152 ::
       
   153 
       
   154   imshow(I) 
       
   155 
       
   156 to see what has been read into ``I``.
       
   157 
       
   158 To see that ``I`` is really, just an array, we say, 
       
   159 ::
       
   160 
       
   161   I 
       
   162 
       
   163 at the prompt, and see that an array is displayed. 
       
   164 
       
   165 To check the dimensions of any array, we can use the method
       
   166 shape. We say
       
   167 ::
       
   168 
       
   169   I.shape 
       
   170 
       
   171 to get the dimensions of the image. As we can see, ``squares.png``
       
   172 has the dimensions of 300x300. 
       
   173 
       
   174 Our goal for this part of the tutorial would be to get the
       
   175 top-left quadrant of the image. To do this, we need to access, a
       
   176 few of the rows and a few of the columns of the array. 
       
   177 
       
   178 To access, the third column of C, we said, ``C[:, 2]``. Essentially,
       
   179 we are accessing all the rows in column three of C. Now, let us
       
   180 modify this to access only the first three rows, of column three
       
   181 of C. 
       
   182 
       
   183 We say, 
       
   184 ::
       
   185 
       
   186   C[0:3, 2]
       
   187 
       
   188 to get the elements of rows indexed from 0 to 3, 3 not included
       
   189 and column indexed 2. Note that, the index before the colon is
       
   190 included and the index after it is not included, in the slice that
       
   191 we have obtained. This is very similar to the ``range`` function,
       
   192 where ``range`` returns a list, in which the upper limit or stop
       
   193 value is not included.
       
   194 
       
   195 Now, if we wish to access the elements of row with index 2, and in
       
   196 columns indexed 0 to 2 (included), we say, 
       
   197 ::
       
   198 
       
   199   C[2, 0:3]
       
   200 
       
   201 E%% %% Pause the video here, and first, obtain the elements [22,
       
   202 23] from C. Then, obtain the elements [11, 21, 31, 41] from
       
   203 C. Finally, obtain the elements [21, 31, 41, 0]. Then, resume the
       
   204 video.
       
   205 ::
       
   206 
       
   207   C[1, 1:3] 
       
   208 
       
   209 gives the elements [22, 23]
       
   210 ::
       
   211 
       
   212   C[0:4, 0]
       
   213 
       
   214 gives the elements [11, 21, 31, 41]
       
   215 ::
       
   216 
       
   217   C[1:5, 0]
       
   218 
       
   219 gives the elements [21, 31, 41, 0]
       
   220 
       
   221 Note that when specifying ranges, if you are starting from or
       
   222 going up-to the end, the corresponding element may be dropped. So,
       
   223 in the previous example to obtain [11, 21, 31, 41], we could have
       
   224 simply said, 
       
   225 ::
       
   226 
       
   227   C[:4, 0]
       
   228 
       
   229 and 
       
   230 ::
       
   231 
       
   232   C[1:, 0]
       
   233 
       
   234 gives the elements [21, 31, 41, 0]. If we skip both the indexes,
       
   235 we get the slice from end to end, as we already know. 
       
   236 
       
   237 E%% %% Pause the video here. Obtain the elements [[23, 24], [33,
       
   238 -34]] and then resume the video. 
       
   239 ::
       
   240 
       
   241   C[1:3, 2:4] 
       
   242 
       
   243 gives us the elements, [[23, 24], [33, -34]]. 
       
   244 
       
   245 Now, we wish to obtain the top left quarter of the image. How do
       
   246 we go about doing it? Since, we know the shape of the image to be
       
   247 300, we know that we need to get the first 150 rows and first 150
       
   248 columns. 
       
   249 ::
       
   250 
       
   251   I[:150, :150]
       
   252 
       
   253 gives us the top-left corner of the image. 
       
   254 
       
   255 We use the ``imshow`` command to see the slice we obtained in the
       
   256 form of an image and confirm. 
       
   257 ::
       
   258 
       
   259   imshow(I[:150, :150])
       
   260 
       
   261 E%% %% Pause the video here, and obtain the square in the center
       
   262 of the image. 
       
   263 ::
       
   264 
       
   265   imshow(I[75:225, 75:225])
       
   266 
       
   267 Our next goal is to compress the image, using a very simple
       
   268 technique to reduce the space that the image takes on disk while
       
   269 not compromising too heavily on the image quality. The idea is to
       
   270 drop alternate rows and columns of the image and save it. This way
       
   271 we will be reducing the data to a fourth of the original data but
       
   272 losing only so much of visual information. 
       
   273 
       
   274 We shall first learn the idea of striding using the smaller array
       
   275 C. Suppose we wish to access only the odd rows and columns (first,
       
   276 third, fifth). We do this by, 
       
   277 ::
       
   278 
       
   279   C[0:5:2, 0:5:2]
       
   280 
       
   281 if we wish to be explicit, or simply, 
       
   282 ::
       
   283 
       
   284   C[::2, ::2]
       
   285 
       
   286 This is very similar to the step specified to the ``range``
       
   287 function. It specifies, the jump or step in which to move, while
       
   288 accessing the elements. If no step is specified, a default value
       
   289 of 1 is assumed. 
       
   290 ::
       
   291 
       
   292   C[1::2, ::2] 
       
   293 
       
   294 gives the elements, [[21, 23, 0], [41, 43, 0]]
       
   295 
       
   296 E%% %% Pause the video here, and obtain the following. 
       
   297 [[12, 0], [42, 0]]
       
   298 [[12, 13, 14], [0, 0, 0]]
       
   299 Then, resume the video. 
       
   300 ::
       
   301 
       
   302   C[::3, 1::3]
       
   303 
       
   304 gives the elements [[12, 0], [42, 0]]
       
   305 ::
       
   306 
       
   307   C[::4, 1:4]
       
   308 
       
   309 gives the elements [[12, 13, 14], [0, 0, 0]]
       
   310 
       
   311 Now, that we know how to stride over an image, we can drop
       
   312 alternate rows and columns out of the image in I. 
       
   313 ::
       
   314 
       
   315   I[::2, ::2]
       
   316 
       
   317 To see this image, we say, 
       
   318 ::
       
   319 
       
   320   imshow(I[::2, ::2])
       
   321 
       
   322 This does not have much data to notice any real difference, but
       
   323 notice that the scale has reduced to show that we have dropped
       
   324 alternate rows and columns. If you notice carefully, you will be
       
   325 able to observe some blurring near the edges. To notice this
       
   326 effect more clearly, increase the step to 4. 
       
   327 ::
       
   328 
       
   329   imshow(I[::4, ::4])
       
   330 
       
   331 {{{ show summary slide }}}
       
   332 
       
   333 That brings us to the end of this tutorial. In this tutorial, we
       
   334 have learnt to access parts of arrays, specifically individual
       
   335 elements, rows and columns and larger pieces of arrays. We have
       
   336 also learnt how to modify arrays, element wise or in larger
       
   337 pieces.
       
   338 
       
   339 Thank You!