arrays.txt
changeset 60 5ef46f8d330e
parent 54 46a3575919f5
child 55 1fe734b20950
equal deleted inserted replaced
59:b62177acce71 60:5ef46f8d330e
       
     1 Hello and welcome to the tutorial on Matrices.
       
     2 All matrices operations are done using arrays.
       
     3 We have already seen in previous session that how arrays are better suited for particular mathematical operations. In this session we are going to cover more details on Arrays, how to create/initialize them, how to manipulate and use them for solving given problem.
       
     4 
       
     5 First thing first, we start with creating a normal array by:
       
     6 a (equal to)= array([5, 8, 10, 13])
       
     7 
       
     8 and we have a as array. we can cross the content by
       
     9 a
       
    10 
       
    11 Here a is single dimension array, that is it has only one row. We can create multi-dimensional arrays by
       
    12 
       
    13 c = array([[11,12,13], [21,22,23], [31,32,33]])
       
    14 
       
    15 both c and a are arrays but with different dimensions or shape
       
    16 we can check shape of arrays by using shape attribute of arrays.
       
    17 a.shape
       
    18 c.shape
       
    19 
       
    20 some other handy array initialization methods are also available to make life easier.
       
    21 say we want to create a array of size 3x4 with all values initialized to be 1, we can use
       
    22 b = ones((3, 4))
       
    23 check value by
       
    24 b
       
    25 similarly, we already have a array, and we want to create one more array with same shape and initial values to be one, for that we will use
       
    26 d = ones_like(c)
       
    27 and d will be 3x3 array with all values 1
       
    28 
       
    29 Similarly there are functions like zeros and zeros_like which initialize array with all values being 0. One more useful function available is 'identity', it create identity matrix(array) of given order
       
    30 i = identity(3)
       
    31 i
       
    32 i = identity(5)
       
    33 i
       
    34 
       
    35 We have seen 
       
    36     Welcome to the Tutorial on arrays. 
       
    37 
       
    38     As mentioned in [the previous tutorial] arrays are much faster and
       
    39     more efficient. In this tutorial we shall look at creating arrays,
       
    40     accessing elements and changing them. 
       
    41 
       
    42     ---
       
    43 
       
    44     Let's start with creating simple arrays. We've already seen how to
       
    45     convert lists to arrays. Inputting a new array is similar to that. 
       
    46 
       
    47     In []: 
       
    48 
       
    49     Type /a/, to see what it is. 
       
    50 
       
    51     In []: a
       
    52     
       
    53     We enter a multi-dimensional array this way -
       
    54     
       
    55     In []: c = array([[11,12,13],
       
    56                      [21,22,23],
       
    57                       [31,32,33]])
       
    58 
       
    59     To see what c is, we just type c in the prompt. 
       
    60 		      
       
    61     In []: c
       
    62 
       
    63     To see the dimensions of the array c, we use c.shape
       
    64     In []: c.shape 
       
    65 
       
    66     Now let us look at some special methods of creating an
       
    67     array. There are various functions that allow us to create special
       
    68     arrays. 
       
    69 
       
    70     The first one we shall look at is, /arange/. /arange/ is similar to
       
    71     the range command, except that it returns an array and accepts
       
    72     float arguments. 
       
    73     
       
    74     In []: a = arange(10)
       
    75     
       
    76     In []: a
       
    77     This is the array we just created. 
       
    78     
       
    79     In []: a.shape
       
    80     Note that /a/ is one dimensional and has 10 elements, as expected. 
       
    81 
       
    82     We could also use a.shape to change the shape of the array a. 
       
    83     In []: a.shape = 2,5
       
    84     Note that the total size of new array must be unchanged. 
       
    85 
       
    86     We type a, to see what it looks like
       
    87     In []: a
       
    88 
       
    89     ones command can be used to get an array with all the entries as
       
    90     ones. We pass it the shape of the array that we require. 
       
    91     
       
    92     In []: 
       
    93 
       
    94     Look at b, by printing it out. 
       
    95     In []: b 
       
    96 
       
    97     To create an array with all entries as ones, with it's shape
       
    98     similar to an already existing array, we use the ones_like
       
    99     command.  
       
   100     In []: b = ones_like(a)
       
   101 
       
   102     zeros and zeros_like are similar commands that can give you arrays
       
   103     with all zeros. empty and empty_like give you empty arrays (arrays
       
   104     with no initialization done.)
       
   105 
       
   106     In []: b = zeros((3, 4))
       
   107     In []: b = zeros_like(a)
       
   108 
       
   109     The identity command can be used to obtain a square array with
       
   110     ones on the main diagonal. 
       
   111     
       
   112     In []: identity(3)
       
   113 
       
   114     To obtain a 2-D array, that is not necessarily square, eye command
       
   115     can be used. Look at the documentation of eye (using eye?) for
       
   116     more info. 
       
   117 
       
   118     ---
       
   119     
       
   120     Now that we have learnt how to create arrays, let's move on to
       
   121     accessing elements and changing them. 
       
   122     
       
   123     Let's work with the c, array which we had already created. 
       
   124 
       
   125     In []: c 
       
   126 
       
   127     Let's say we want to access the element 23 in c, we say
       
   128 
       
   129     In []: c[1][2]
       
   130     Note that this is similar to accessing an element inside a list of
       
   131     lists. Also, note that counting again starts from 0. 
       
   132     
       
   133     But arrays provide a more convenient way to access the elements. 
       
   134     In []: c[1, 2]
       
   135     
       
   136     Now, we can also change the element using a simple assignment. 
       
   137     In []: c[1, 2] = -23
       
   138 
       
   139     Let's look at accessing more than one elements at a time. We begin
       
   140     with accessing rows. 
       
   141     In []: c[1] gives us the second row. (counting starts from 0)
       
   142 
       
   143     To get a column, we use a syntax that is similar to the one used
       
   144     to access a single element. 
       
   145     In []: c[:,1], gives us the first column. 
       
   146     
       
   147     The colon specifies that we wish to obtain all elements in that
       
   148     dimension from the array.  
       
   149 
       
   150     So, we could use a more explicit way to access the second row of
       
   151     the array. 
       
   152     In []: c[1,:]
       
   153     
       
   154     The colon can be used to access specific portions of the array,
       
   155     similar to the way we do with lists. 
       
   156     In []: c[1,1:3]
       
   157     Observe that we get the second and third columns from the second
       
   158     row. As with lists, the number after the colon is excluded when
       
   159     slicing a portion of the array. 
       
   160 
       
   161     In []: c[1:3,1]
       
   162     Now, we get the second and third rows from the first column. 
       
   163 
       
   164     In []: c[1:3,1:3]
       
   165     We get the second and third rows and the second and third
       
   166     columns. 
       
   167 
       
   168     The numbers before and after the colons are optional. If the
       
   169     number before the colon is omitted, it is assumed to be zero by
       
   170     default. If the element after the colon is omitted, it is assumed
       
   171     to be until the end. 
       
   172 
       
   173     In []: c[1:, 1:]
       
   174     This is essentially similar to the previous example. We are using
       
   175     the default value i.e, the end, instead of specifying 3,
       
   176     explicitly. 
       
   177 
       
   178     In []: c[:2, :2]
       
   179     We have omitted specifying the zero before the colon, explicitly. 
       
   180 
       
   181     --- 
       
   182     
       
   183     You may have observed the similarity of the semi-colon notation to
       
   184     the notation used in lists. As expected, the semi-colon notation
       
   185     also provides a way to specify a jump. This {concept/idea} is
       
   186     termed as Striding. 
       
   187 
       
   188     To get every alternate row of c, starting from the first one, we say
       
   189     In []: c[::2,:]
       
   190 
       
   191     To get every alternate row of c, starting from the second one, we
       
   192     say 
       
   193     In []: c[1::2,:]
       
   194 
       
   195 
       
   196     In []: c[:,::2]
       
   197     In []: c[::2,::2]
       
   198 
       
   199     ---
       
   200 
       
   201     We come to the end of this tutorial on arrays. In this tutorial,
       
   202     you've learnt how to create arrays and access, change elements. 
       
   203 
       
   204     Thank you. 
       
   205 
     1 Hello friends and welcome to the second tutorial in the series of spoken tutorials on Python for Scientific computing. 
   206 Hello friends and welcome to the second tutorial in the series of spoken tutorials on Python for Scientific computing. 
     2 
   207 
     3 In the previous tutorial we learnt about arrays and we told you that numpy arrays are faster and more efficient . In this tutorial we shall look at creating arrays, accessing elements and changing them. 
   208 In the previous tutorial we learnt about arrays and we told you that numpy arrays are faster and more efficient . In this tutorial we shall look at creating arrays, accessing elements and changing them. 
     4 
   209 
     5 
   210 
     6 Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. 
   211 Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. 
     7 
   212 
     8 On your Ipython terminal type a = array open parenthesis and then open square brackets 5,8,10,13 ,close square brackets and close parenthesis . This create an array a . You can see what a is by typing a on the terminal .
   213 On your Ipython terminal type a = array open parenthesis and then open square brackets 5,8,10,13 close square brackets and close parenthesis . This create an array a . You can see what a is by typing a on the terminal .
     9 Now we will try to create a multi-dimensional array type in your ipython terminal
   214 Now we will try to create a multi-dimensional array type in your ipython terminal
    10 c= array open parenthesis , then open square brackets 11,12,13 close square bracket 'comma' start square bracket 21 , 22 ,23close square bracket 'comma' open 31,32,33 close square bracket another close square bracket which closes the first sqaure bracket and parenthesis which closes the first parenthesis . Now to see the dimensions of the array c we will do c.shape . We can see that c is a 3 by 3 matrix .
   215 c= array open parenthesis , then open square brackets 11,12,13 close square bracket 'comma' start square bracket 21 , 22 ,23close square bracket 'comma' open 31,32,33 close square bracket another close square bracket which closes the first sqaure bracket and parenthesis which closes the first parenthesis . Now to see the dimensions of the array c we will do c.shape . We can see that c is a 3 by 3 matrix .
    11 
   216 
    12 There are other special methods of creating arrays as well we will now look at them .
   217 There are other special methods of creating arrays as well we will now look at them .
    13 The first one is the command arange which is similar to range except that it returns an array.
   218 The first one is the command arange which is similar to range except that it returns an array.
    14 We will type on our Ipython interpreter a = arange(10). We will see what a is now . Type a . As we can see This returns us an array of one dimension and has 10 elements .  
   219 We will type on our Ipython interpreter a = arange(10). We will see what a is now . Type a . As we can see This returns us an array of one dimension and has 10 elements .  
    15 Ones can be use to get all entries as ones . We can pass it the shape of the array as required .
   220 Ones can be use to get all entries as ones . We can pass it the shape of the array as required .
    16 type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out .
   221 type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out .
    17 To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like
   222 To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like
    18 command.  type b= ones_like in parenthesis a . 
   223 command.  type b= ones_like in parenthesis a .   
    19 
       
    20  
       
    21 
       
    22  
       
    23      
       
    24    
       
    25 
       
    26 
       
    27