arrays.org
changeset 10 a63d7dcba725
parent 4 4dee50d4804b
equal deleted inserted replaced
9:538f59bb598c 10:a63d7dcba725
     1 * Arrays
     1 * Arrays
     2 *** Outline
     2 *** Outline
     3 ***** Introduction
     3 ***** Introduction
     4 ******* Why do we want to do that?
     4 ******* What do we want to do
     5 ******* We shall use arrays (introduced before) for matrices
     5 ********* We shall use arrays (mentioned before) which
       
     6 ********* shall be used for matrices in future
     6 ******* Arsenal Required
     7 ******* Arsenal Required
     7 ********* working knowledge of lists
     8 ********* working knowledge of lists
     8 ***** Initializing matrices
     9 ***** Initializing matrices
     9 ******* Entering element-wise
    10 ******* Entering element-wise
    10 ******* using special functions
    11 ******* using special functions
    11 ***** Accessing and Changing elements
    12 ***** Accessing and Changing elements
    12 ******* Slicing
    13 ******* Slicing??
    13 ******* Striding
    14 ******* Striding??
    14 ***** {{Simple operations??}}
    15 ***** {{Simple operations??}}
    15 *** Script
    16 *** Script
    16     Welcome to the Tutorial on arrays. 
    17     Welcome to the Tutorial on arrays. 
    17 
    18 
    18     As mentioned in [the previous tutorial] arrays are much faster and
    19     As mentioned in [the previous tutorial] arrays are much faster and
    20     accessing elements and changing them. 
    21     accessing elements and changing them. 
    21 
    22 
    22     ---
    23     ---
    23 
    24 
    24     Let's start with creating simple arrays. We've already seen how to
    25     Let's start with creating simple arrays. We've already seen how to
    25     convert lists to arrays. Entering an array is similar to that. 
    26     convert lists to arrays. Inputting a new array is similar to that. 
    26 
    27 
    27     a = array([5, 8, 10, 13])
    28     In []: a = array([5, 8, 10, 13])
       
    29 
       
    30     Type /a/, to see what it is. 
       
    31 
       
    32     In []: a
    28     
    33     
    29     We enter a multi-dimensional array this way -
    34     We enter a multi-dimensional array this way -
    30     
    35     
    31     In []: c = array([[11,12,13],
    36     In []: c = array([[11,12,13],
    32                       [21,22,23],
    37                      [21,22,23],
    33                       [31,32,33]])
    38                       [31,32,33]])
    34 
    39 
    35     To see what c is, we just type c in the prompt. 
    40     To see what c is, we just type c in the prompt. 
    36 		      
    41 		      
    37     In []: c
    42     In []: c
    41 
    46 
    42     Now let us look at some special methods of creating an
    47     Now let us look at some special methods of creating an
    43     array. There are various functions that allow us to create special
    48     array. There are various functions that allow us to create special
    44     arrays. 
    49     arrays. 
    45 
    50 
    46     The first one we shall look at is, arange. arange is similar to
    51     The first one we shall look at is, /arange/. /arange/ is similar to
    47     the range command, except that it returns an array and accepts
    52     the range command, except that it returns an array and accepts
    48     float arguments. [is range covered?]
    53     float arguments. 
    49     
    54     
    50     In []: a = arange(10)
    55     In []: a = arange(10)
    51     
    56     
    52     In []: a
    57     In []: a
    53     This is the array we just created. 
    58     This is the array we just created. 
    54     
    59     
    55     In []: a.shape
    60     In []: a.shape
    56     Note that a is one dimensional and has 10 elements, as expected. 
    61     Note that /a/ is one dimensional and has 10 elements, as expected. 
    57 
    62 
    58     We could also use a.shape to change the shape of the array a. 
    63     We could also use a.shape to change the shape of the array a. 
    59     In []: a.shape = 2,5
    64     In []: a.shape = 2,5
    60     Note that the total size(number of elements) of new array must 
    65     Note that the total size of new array must be unchanged. 
    61     be unchanged. 
       
    62 
    66 
    63     We check re-shaped 'a' by
    67     We type a, to see what it looks like
    64     In []: a
    68     In []: a
    65 
    69 
    66     'ones' function can be used to get an array with all the entries as
    70     ones command can be used to get an array with all the entries as
    67     1s. We pass it the shape of the required array. For ex. 
    71     ones. We pass it the shape of the array that we require. 
    68     
    72     
    69     In []: b = ones((3, 4))
    73     In []: b = ones((3, 4))
    70 
    74 
    71     b is 3(cross)4 array with all 1s
    75     Look at b, by printing it out. 
    72     In []: b 
    76     In []: b 
    73 
    77 
    74     To create an array with all entries as ones, with a shape similar to
    78     To create an array with all entries as ones, with it's shape
    75     an already existing array, we use the ones_like function.
    79     similar to an already existing array, we use the ones_like
       
    80     command.  
    76     In []: b = ones_like(a)
    81     In []: b = ones_like(a)
    77 
    82 
    78     zeros and zeros_like are similar commands that can give you arrays
    83     zeros and zeros_like are similar commands that can give you arrays
    79     with all zeros. empty and empty_like give you empty arrays (arrays
    84     with all zeros. empty and empty_like give you empty arrays (arrays
    80     with no initialization done.)
    85     with no initialization done.)
    81 
    86 
    82     {Do an up arrow and quickly show them a couple of examples?}
    87     In []: b = zeros((3, 4))
       
    88     In []: b = zeros_like(a)
    83 
    89 
    84     The identity command can be used to obtain a square array with
    90     The identity command can be used to obtain a square array with
    85     ones on the main diagonal. 
    91     ones on the main diagonal. 
    86     
    92     
    87     In []: identity(3)
    93     In []: identity(3)
    90     can be used. Look at the documentation of eye (using eye?) for
    96     can be used. Look at the documentation of eye (using eye?) for
    91     more info. 
    97     more info. 
    92 
    98 
    93     ---
    99     ---
    94     
   100     
    95     Now that we have learnt how to create arrays, lets move on to
   101     Now that we have learnt how to create arrays, let's move on to
    96     accessing elements and changing them. 
   102     accessing elements and changing them. 
    97     
   103     
    98     Let's work with the c, array which we had already created. 
   104     Let's work with the c, array which we had already created. 
    99 
   105 
   100     In []: c 
   106     In []: c 
   101 
   107 
   102     Let's say we want to access the element 23 in c(second row 
   108     Let's say we want to access the element 23 in c, we say
   103     third column), we say
       
   104 
   109 
   105     In []: c[1][2]
   110     In []: c[1][2]
   106     Note that this is similar to accessing an element inside a list of
   111     Note that this is similar to accessing an element inside a list of
   107     lists. Also, note that counting again starts from 0. 
   112     lists. Also, note that counting again starts from 0. 
   108     
   113     
   109     Additionally arrays provide a more convenient way to access the 
   114     But arrays provide a more convenient way to access the elements. 
   110     elements. 
       
   111     In []: c[1, 2]
   115     In []: c[1, 2]
   112     
   116     
   113     Now, we can also change the element using a simple assignment. 
   117     Now, we can also change the element using a simple assignment. 
   114     In []: c[1, 2] = -23
   118     In []: c[1, 2] = -23
   115 
   119 
   119 
   123 
   120     To get a column, we use a syntax that is similar to the one used
   124     To get a column, we use a syntax that is similar to the one used
   121     to access a single element. 
   125     to access a single element. 
   122     In []: c[:,1], gives us the first column. 
   126     In []: c[:,1], gives us the first column. 
   123     
   127     
   124     The colon is a way to tell python to get all elements in that
   128     The colon specifies that we wish to obtain all elements in that
   125     dimension from the array. 
   129     dimension from the array.  
   126 
   130 
   127     So, we could use a more explicit way to access a row of a
   131     So, we could use a more explicit way to access the second row of
       
   132     the array. 
   128     In []: c[1,:]
   133     In []: c[1,:]
   129     
   134     
   130     We could use the colon to access specific portions of an array. 
   135     The colon can be used to access specific portions of the array,
   131     In []: c[1,1:2]
   136     similar to the way we do with lists. 
   132     second column, from second row(1) till third(2) and excluding it
   137     In []: c[1,1:3]
   133     In []: c[1:2,1]
   138     Observe that we get the second and third columns from the second
   134     In []: c[1:2,1:2]
   139     row. As with lists, the number after the colon is excluded when
   135     ...
   140     slicing a portion of the array. 
   136     [Oh, by the way this is termed as slicing. :)]
   141 
   137     {How many examples should we show here?}
   142     In []: c[1:3,1]
       
   143     Now, we get the second and third rows from the first column. 
       
   144 
       
   145     In []: c[1:3,1:3]
       
   146     We get the second and third rows and the second and third
       
   147     columns. 
       
   148 
       
   149     The numbers before and after the colons are optional. If the
       
   150     number before the colon is omitted, it is assumed to be zero by
       
   151     default. If the element after the colon is omitted, it is assumed
       
   152     to be until the end. 
       
   153 
       
   154     In []: c[1:, 1:]
       
   155     This is essentially similar to the previous example. We are using
       
   156     the default value i.e, the end, instead of specifying 3,
       
   157     explicitly. 
       
   158 
       
   159     In []: c[:2, :2]
       
   160     We have omitted specifying the zero before the colon, explicitly. 
   138 
   161 
   139     --- 
   162     --- 
   140     
   163     
   141     You may have observed the similarity of the semi-colon notation to
   164     You may have observed the similarity of the semi-colon notation to
   142     the range command. As expected, the semi-colon notation also
   165     the notation used in lists. As expected, the semi-colon notation
   143     provides a way to specify a jump. This {concept/idea} is termed as
   166     also provides a way to specify a jump. This {concept/idea} is
   144     Striding. 
   167     termed as Striding. 
   145 
   168 
   146     To get every alternate row of c, starting from the first one, we say
   169     To get every alternate row of c, starting from the first one, we say
   147     In []: c[::2,:]
   170     In []: c[::2,:]
   148 
   171 
   149     To get every alternate row of c, starting from the second one, we
   172     To get every alternate row of c, starting from the second one, we
   150     say
   173     say 
   151     In []: c[1::2,:]
   174     In []: c[1::2,:]
   152 
   175 
   153 
   176 
   154     In []: c[:,::2]
   177     In []: c[:,::2]
   155     In []: c[::2,::2]
   178     In []: c[::2,::2]
   156     {How many examples should we show here?}
       
   157 
   179 
   158     ---
   180     ---
   159 
   181 
   160     We come to the end of this tutorial on arrays. In this tutorial,
   182     We come to the end of this tutorial on arrays. In this tutorial,
   161     you've learnt how to create arrays and access, change elements. 
   183     you've learnt how to create arrays and access, change elements.