changes to array.txt vWO-ID
authorasokan <asokan@fossee.in>
Tue, 18 May 2010 15:40:17 +0530
changeset 126 2eac725a5766
parent 125 27ddf1255daa (diff)
parent 124 67c4a2c75aee (current diff)
child 127 76fd286276f7
changes to array.txt
--- a/arrays.txt	Tue May 18 15:27:05 2010 +0530
+++ b/arrays.txt	Tue May 18 15:40:17 2010 +0530
@@ -1,36 +1,45 @@
 Hello friends and welcome to this tutorial on Matrices.
+
 In python all matrix operations are done using arrays.
-We have already seen in previous session that how arrays are better suited for certain mathematical operations. In this session we shall see how to perform efficient matrix operations using arrays. We shall see how to create them, how to initialize them, how to manipulate and use them to perform some basic image processing. For this tutorial we shall need the lena.png image. Hope you have the image with you. 
+
+We saw in the previous session that arrays are better suited for mathematical operations. We saw this in the context of simple statistical functions such as mean.
+
+In this session we shall see how to perform efficient matrix operations using arrays. We will create arrays, initialize them, manipulate them and perform simple image processing using them. For this tutorial we shall need the lena.png image. Hope you have the image with you. 
 
 Let's now start off. As you can see our lena image is on the desktop, so first let's navigate to the desktop by cd Desktop.
 
-Let's now fire up Ipython, ipython -pylab
+Let's now start Ipython, using the command python -pylab
 
-First things first, let's start by creating a normal array, type:
-a equal to array([5, 8, 10, 13])
+First things first, let's start by creating a normal array. 
+
+Type:
+p equal to array([5, 8, 10, 13])
 
 let's check the value of a by typing
-a
+p
+Note how python displays an array, compared to a list.
 
-Here a is single dimension array, that is it has only one row. Let's now look at creating multi-dimensional arrays by
+Here p is single dimension array, that is it has only one row. Let us now create a multi-dimensional array.
 
-c = array([[11,12,13], [21,22,23], [31,32,33]])
+Type:
 
-both c and a are arrays but with different dimensions or shape
+q = array([[11,12,13], [21,22,23], [31,32,33]])
+
+both p and q are arrays but with different dimensions or shape
 we can check shape of arrays by using shape attribute of arrays.
-a.shape
-c.shape
+p.shape
+q.shape
 
-A few other handy array initialization methods are also available to make life easier.
+A few array initialization methods are also available to make life easier;
 say we want to create an array of size 3x4 with all the elements initialized to 1, we use
 b = ones((3, 4))
 and b is
 b
-similarly, suppose we already have an array, and we want to create another array with the same shape but with initial values equal to one, for eg, to get an array similar in shape to the array 'c' but with all elements as 1 we type:
-d = ones_like(c)
+similarly, suppose we already have an array, and we want to create another array with the same shape but with initial values equal to one, for eg, to get an array similar in shape to the array 'q' but with all elements as 1 we type:
+d = ones_like(q)
 and d is a 3x3 array with all values equal to 1
 
-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 of given order
+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 unit matrix of given order
 i = identity(3)
 i
 
@@ -39,98 +48,107 @@
 ----------------
 Now that we have covered creation of arrays, we shall see how to access and change values of particular elements. 
 Remember we created a 3x3 matrix earlier, 
-c
+q
 
 to access the element 23 we type
-c[1][2]
+q[1][2]
 
-It is at the second row of the third column of the matrix/array c. Note that index values of arrays also start from 0.
+It is at the second row of the third column of the matrix/array q. Note that index values of arrays also start from 0.
 Alternatively, the more popular way of doing the same is
-c[1, 2]
+q[1, 2]
 
 here ',' is used as separator for row and column value. Similarly any value from the array can be accessed.
 
-to access particular row completely we simply skip the column value
-c[1]
-gives us the entire second row. 
+to access particular row completely we specify the row value alone: 
+q[1]
+This gives us the entire second row. 
 
 We can assign a new value to an element, the same way we accessed it. For eg., 
-c[1, 1] = -22
-c
+q[1, 1] = -22
+q
 
-In order to change an entire row we type:
-c[1] = 0
-c
+One of the most powerful aspects of a high-level language like python is the way it supports matrix operations. We can use them like we do it maths rather than think of them as elements like a programmer.
+
+For example to change a whole row, we type:
+q[1] = 0
+q
 as we can see, all elements of the second row are now 0
 
-Accessing a row is straight forward we skip column part
-but the same cannot be done to access columns. In order to access the whole column we have to use ':'
-c[:,2]
+In order to access a column, we need to syntactically indicate that the number given is the column index rather than the row index. In other words we need a placeholder for the row position. We cannot use space. That is we cannot say q[, 1] as that would be a syntax error ( q[m, n] being the method to access an _element_).
+
+We have to say all rows and a column(s) by extending the slice notation seen earlier.
+q[:,2]
 returns the third column.
-here the ':' part mentioned for row value symbolises entire row.
-the c[1] we were using earlier can also be written as c[1,:]
+Here the ':' part specifies the row numbers of the slice; as we have seen before by leaving the from and to parts of the slice empty we effectively say ALL. Thus q[:, n] stands for a matrix which is the submatrix obtained by taking all rows and  column n+1.
+
+The row reference q[1] can also be written as q[1,:]
 
-':' actually takes two value. for any row or column we can mention
-start:end values, and rows or columns starting for 'start' till 'end' will be returned. Lets try some examples for better understanding
-c[0:2,:]
-results in rows starting from row zero(0) upto the second row and all columns. Note here that 'end', in this case, '2' will not be included in resulting array.
+As we have seen ':' takes two values namely start and end. As before rows or columns starting from 'start' till 'end' --excluding end-- will be returned. Lets try some examples:
+q [0:2,:]
+results in a matrix which is rows 0 and 1 and all columns. Note here that 'end', in this case, '2' will not be included in resulting matrix.
 
-c[1:3,:] 
-gives second and third row.
+Similarly q[1:3,:] 
+gives second and third rows.
 
-similarly we can try this on columns also:
-c[:, 0:2] gives us first two column
-This whole concept of accessing chunks of arrays is known as 'slicing'
+q[:, 0:2] gives us first two columns
+
+This manner of accessing chunks of arrays is also known as 'slicing'. Since the idea is the same as slicing for lists the name is also the same.
 
-There is one more interesting and handy feature of slicing. We saw earlier that how only ':' means entire row or column.
-It actually means if we don't specify start and end part of slice default is from zero to end.
+As noted the default values for slices carry over.
+Thus
+q[:, :2]
+gives us the first two columns
 
-so
-c[:, :2]
-also gives us first two columns
-and c[:, 1:] returns all columns excluding the 0th column.
+q[:, 1:] 
+returns all columns excluding the 0th column.
 
-c[1:, :2]
+q[1:, :2]
 returns first two columns of all rows excepting the 0th row.
 
-Now we shall look into one more powerful feature of arrays: 'striding'.
-Striding allows us to jump or skip, rows or columns by a certain interval. We can specify the step size.
-c[:,:] gives us entire array
-we add one more ':' to row or column part to specify a step size.
-c[:, ::2]
-gives us first and third column. Since step size is two, it starts with the first column(blank before : means 0) and then we jump one column and then third(blank after : means end)
-similarly 
-c[::2,:] returns a 2x3 array with the first and the third row
+When slicing lists we saw the idea of striding. Recall that if L is a list,
+L[start : stop : step],
+produces a new list whose first element is L[start] and has all elements whose index is start + n * step; stop signals the last index _before_ which we should stop.
 
-and c[::2, ::2] gives us a 2x2 array with the first and the third row and column 
+Matrices also support striding--that is skip, rows or columns by a certain interval. 
+We add one more ':' to row or column part to specify a step size.
+Let us type 
+q[:, ::2]
+and see what is shown.
+The first colon specifies that we pick all rows, the comma signals that we start specifying columns. The empty places indicate defaults. That is start from the 0th and go to the end. The presence of step--in this case 2--tells us that we will select alternate columns. Thus we q[:, ::2] extracts all rows and alternate columns starting with 0th column.
 
-Lets us try to use these concepts of slicing and striding for doing some basic image manipulation
+q[::2,:]
+returns a 2x3 matrix with the first and the third row.
 
-pylab has a function named imread to read images. We shall use the '(in)famous' lena image for our experimentation. Its there on desktop. 
+q[::2, ::2] 
+gives us a 2x2 array with the first and the third rows and first and third columns. 
+
+Lets us use slicing and striding for doing some basic image manipulation
+
+pylab has a function named imread to read images. We shall use lena.png image for our experimentation. Its there on desktop. 
 
 a = imread('lena.png')
-a is a numpy array with the 'RGB' values of each pixel
+Now a is an array with the RGB and Alpha channel values of each pixel
 a.shape
-
-its a 512x512x3 array.
+tells us that 
+it is an 512x512x4 array.
 
 to view the image write
 imshow(a)
 
-lets try to crop the image to top left quarter. Since a is a normal array we can use slicing to get the top left quarter by
-imshow(a[:255,:255]) (half of 512 is 256)
+lets try to crop the image to top left quarter. Since a is an array we can use slicing to get the top left quarter by
+imshow(a[:256,:256]) (half of 512 is 256)
 
-Lena's hat is not of much interest for us. Let's crop the image so that only her face is visible. And to do that we'll need some rough estimates of pixels. 
+Let's crop the image so that only her face is visible. And to do that we'll need some rough estimates of the coordinates of the face.  
 imshow(a)
-now move your mouse pointer over the image, it gives us x, y coordinates of the mouse pointer's current location. We can get rough estimate of lena's face. We can observe that Lena's face begins from somewhere around 200, 200 and ends at 400, 400. Now cropping to these boundaries is simple
+now move your mouse pointer over the image, it gives us x, y coordinates of the mouse pointer's current location. With this we can get rough estimate of lena's face. We observe that Lena's face begins from somewhere around 200, 200 and ends at 400, 400. Now cropping to these boundaries is simple
 imshow(a[200:400, 200:400])
 
 Next we shall try striding on this image. We shall resize the image by skipping alternate pixels. We have already seen how to skip alternate elements so,
 imshow(a[::2, ::2])
-note now the size of image is just 256x256 and still quality of image is not much compromised.
--------------------------
+note that the size of image is just 256x256. 
+------------------
 
-Till now we have covered initializing and accessing elements of arrays. Now we shall concentrate on functions available for arrays. We start this by creating 4x4 array by
+Till now we have covered initializing and accessing elements of arrays. Now we shall look at other manipulations for arrays. We start by creating a 4x4 array 
 
 a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]])
 a
@@ -148,9 +166,9 @@
 a + b
 
 lets try multiplication now, 
-a * b returns the element wise product of two matrices.
+a * b returns a new matrix whose elements are products of corresponding elements. THIS IS NOT matrix multiplication.
 
-To get matrix product of a and b we use
+To get the usual product of matrices a and b we use
 dot(a, b)
 
 To get the inverse of a matrix we use,
@@ -159,11 +177,11 @@
 
 det(a) returns determinant of matrix a
 
-we shall create an array e
+we shall create an matrix e
 e = array([[3,2,4],[2,0,2],[4,2,3]])
-and then to evaluate eigenvalues of array
+and then to evaluate eigenvalues of the same. 
 eig(e)
-it returns both eigen values and eigen vector of given matrix
+returns both eigen values and eigen vector of given matrix
 to get only eigen values use
 eigvals(e)
 
@@ -176,193 +194,3 @@
 
 Thank you
 
-----------------
-We have seen 
-    Welcome to the Tutorial on arrays. 
-
-    As mentioned in [the previous tutorial] arrays are much faster and
-    more efficient. In this tutorial we shall look at creating arrays,
-    accessing elements and changing them. 
-
-    ---
-
-    Let's start with creating simple arrays. We've already seen how to
-    convert lists to arrays. Inputting a new array is similar to that. 
-
-    In []: 
-
-    Type /a/, to see what it is. 
-
-    In []: a
-    
-    We enter a multi-dimensional array this way -
-    
-    In []: c = array([[11,12,13],
-                     [21,22,23],
-                      [31,32,33]])
-
-    To see what c is, we just type c in the prompt. 
-		      
-    In []: c
-
-    To see the dimensions of the array c, we use c.shape
-    In []: c.shape 
-
-    Now let us look at some special methods of creating an
-    array. There are various functions that allow us to create special
-    arrays. 
-
-    The first one we shall look at is, /arange/. /arange/ is similar to
-    the range command, except that it returns an array and accepts
-    float arguments. 
-    
-    In []: a = arange(10)
-    
-    In []: a
-    This is the array we just created. 
-    
-    In []: a.shape
-    Note that /a/ is one dimensional and has 10 elements, as expected. 
-
-    We could also use a.shape to change the shape of the array a. 
-    In []: a.shape = 2,5
-    Note that the total size of new array must be unchanged. 
-
-    We type a, to see what it looks like
-    In []: a
-
-    ones command can be used to get an array with all the entries as
-    ones. We pass it the shape of the array that we require. 
-    
-    In []: 
-
-    Look at b, by printing it out. 
-    In []: b 
-
-    To create an array with all entries as ones, with it's shape
-    similar to an already existing array, we use the ones_like
-    command.  
-    In []: b = ones_like(a)
-
-    zeros and zeros_like are similar commands that can give you arrays
-    with all zeros. empty and empty_like give you empty arrays (arrays
-    with no initialization done.)
-
-    In []: b = zeros((3, 4))
-    In []: b = zeros_like(a)
-
-    The identity command can be used to obtain a square array with
-    ones on the main diagonal. 
-    
-    In []: identity(3)
-
-    To obtain a 2-D array, that is not necessarily square, eye command
-    can be used. Look at the documentation of eye (using eye?) for
-    more info. 
-
-    ---
-    
-    Now that we have learnt how to create arrays, let's move on to
-    accessing elements and changing them. 
-    
-    Let's work with the c, array which we had already created. 
-
-    In []: c 
-
-    Let's say we want to access the element 23 in c, we say
-
-    In []: c[1][2]
-    Note that this is similar to accessing an element inside a list of
-    lists. Also, note that counting again starts from 0. 
-    
-    But arrays provide a more convenient way to access the elements. 
-    In []: c[1, 2]
-    
-    Now, we can also change the element using a simple assignment. 
-    In []: c[1, 2] = -23
-
-    Let's look at accessing more than one elements at a time. We begin
-    with accessing rows. 
-    In []: c[1] gives us the second row. (counting starts from 0)
-
-    To get a column, we use a syntax that is similar to the one used
-    to access a single element. 
-    In []: c[:,1], gives us the first column. 
-    
-    The colon specifies that we wish to obtain all elements in that
-    dimension from the array.  
-
-    So, we could use a more explicit way to access the second row of
-    the array. 
-    In []: c[1,:]
-    
-    The colon can be used to access specific portions of the array,
-    similar to the way we do with lists. 
-    In []: c[1,1:3]
-    Observe that we get the second and third columns from the second
-    row. As with lists, the number after the colon is excluded when
-    slicing a portion of the array. 
-
-    In []: c[1:3,1]
-    Now, we get the second and third rows from the first column. 
-
-    In []: c[1:3,1:3]
-    We get the second and third rows and the second and third
-    columns. 
-
-    The numbers before and after the colons are optional. If the
-    number before the colon is omitted, it is assumed to be zero by
-    default. If the element after the colon is omitted, it is assumed
-    to be until the end. 
-
-    In []: c[1:, 1:]
-    This is essentially similar to the previous example. We are using
-    the default value i.e, the end, instead of specifying 3,
-    explicitly. 
-
-    In []: c[:2, :2]
-    We have omitted specifying the zero before the colon, explicitly. 
-
-    --- 
-    
-    You may have observed the similarity of the semi-colon notation to
-    the notation used in lists. As expected, the semi-colon notation
-    also provides a way to specify a jump. This {concept/idea} is
-    termed as Striding. 
-
-    To get every alternate row of c, starting from the first one, we say
-    In []: c[::2,:]
-
-    To get every alternate row of c, starting from the second one, we
-    say 
-    In []: c[1::2,:]
-
-
-    In []: c[:,::2]
-    In []: c[::2,::2]
-
-    ---
-
-    We come to the end of this tutorial on arrays. In this tutorial,
-    you've learnt how to create arrays and access, change elements. 
-
-    Thank you. 
-
-Hello friends and welcome to the second tutorial in the series of spoken tutorials on Python for Scientific computing. 
-
-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. 
-
-
-Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. 
-
-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 .
-Now we shall try to create a multi-dimensional array type in your ipython terminal
-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 do c.shape . We can see that c is a 3 by 3 matrix .
-
-There are other special methods of creating arrays as well we shall now look at them .
-The first one is the command arange which is similar to range except that it returns an array.
-We type on our Ipython interpreter a = arange(10). We see what a is now . Type a . As we can see This returns us an array of one dimension and has 10 elements .  
-Ones can be use to get all entries as ones . We can pass it the shape of the array as required .
-type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out .
-To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like
-command.  type b= ones_like in parenthesis a .