 Good morning everybody. Let us get started. I want to spend may be 15 minutes or so reviewing the various test questions you have written and after that we will get back to the rest of the plotting and the array related stuff we have left off. I said we will just discuss the first few, the questions, various questions asked in the test. We will start with the latest test. The first question asked for that you are given two lists, produce one list which contains the elements, elements in both the lists, removing any duplicates and arranged in ascending order. Now, many people have I think treated this as a fairly difficult question and taken too much time. I do not know why. I will show you how one should go about doing this is to work a little on the interpreter, get your bits and pieces working, then have an editor in which you type the code. At least for future reference please do not use the application we have given as the IDE or as your editor. That is a very poor use of your time and that app. I have one where my Python interpreter is running. I need to have two lists. So, let me another list. Now, I know the answer has to be 1, 2, 3, 4, 5, 7. So, now the important things when you try with some test data is to first have the answer ready. So, I know the answer has to be this. So, I am supposed to have a list which contains both the elements. So, produce a list which contains both the elements and that would be, how do I remove duplicates? We discussed this yesterday. A set will remove duplicates. But I want a list. So, convert it back to a list. It is sorted, but that is not a guarantee we have because we put it into this set and got back. So, to be just absolutely certain you do this. So, the answer asked for just this. You wrote a function with this one line of code in it. You would have got the answer and you would have got here if you had interactively experimented. Now, before we pronounce this as the final answer, we need to check some special cases. What happens if A is empty? Since the only place we should have trouble is A plus B, let us check. Let us say C equal to empty. Then what is A plus C? No errors and produces the right answer. So, if one of them is empty, we would not have a difficulty. What happens if both are empty? That is still the empty set. What about gives the null set or the empty set again? So, our solution would not fail in these special cases. So, we are through. So, we can say our solution works. This is the simplest way one should go about developing a piece of code. Start with one concrete example. Try out few lines of code interactively. Try to move where you want to move once you have got it settled. So, go back and say def combined A comma B return sorted of list of set A plus B. Let us look at the other problem. The other problem was given two strings of the form H H colon mm colon ss. This is this took a little more bookkeeping work. Again, we are told that the second one would be later. So, the answer required is the total elapsed time between the two time stamps in seconds. The advantage of typing in the interpreter and checking one line or says, you will know you can interactively do something like this. It is we are getting what we want. Now, we have to find the total time in seconds of the two and then subtract that is all. Now, this is wrong because s 1 m 1 are strings, but this is the advantage of doing it in the interface. Oh no, you got a huge string. So, turn around because of the previous s 1 it is still wrong. So, you have to do a split once again and then do this. We got what we wanted. So, we now look at what we have done and say you know what all I need is. We could have done this in i python also. I chose to do the direct python interpreter but i python would have been better. Why? Because you could have saved the history. So, let me go there and redo this problem and see how one uses the interpreter to once again making a mistake is not the end of the world because we have not done the split yet. S 2 is not defined, but still in the interpreter the code is available at the top arrow key. So, I got it look at the lines of code I did and you almost all the lines will be simpler to edit those away. We can start from 4 to 10. Percentage save, percentage have options by name. So, I do not remember I can check it up. Percentage save.py-10. We do not need this. We need to add a def line. We use T 1, T 2. We will have less retyping to do. All the lines go in. The lines we used to print are not required. This line was written prematurely. It gave an error. So, this is the code. Now you can see this here. We have once again little bit of experimentation on the editor rather than on the interpreter or i python or the regular python would have given you lot more time to solve the problem and whatever work you did in solving, interactively exploring what all to use you could have cut and paste and produced your final answer. Let us look at some of the earlier questions. Do you have any specific question you want to discuss first? Because I have my favorites. All right. Let us since no one seems to have any preference, I will move to my favorite problem which is the collate sequence. Now, good idea to remember is in any problem sometimes the condition given does not necessarily translate to the simplest condition in our head. Many people in my experience who encounter the collate sequence or the 4 to 1 sequence make a mistake of thinking the sequence ends when the number 1 is encountered. That is wrong. That is a wrong way of thinking about it. The sequence ends when number 4 is encountered because 1 if you say it ends when 1 is encountered, if somebody gives the input itself as 1 you will stop immediately which is not correct. 1 should give to 1 4 2 1. Similarly, 2 should give way to 2 1 4 2 1. So, the correct way to say is it ends at 4, but prints 2 1 1 after ending. If you are able to think about it like this, if you arrive at it and this arriving at it has nothing to do with python or any other language, it is simply the ability to formulate the problem then you will now the problem was for collage with a starting number. Now, we can define three cases. If it is the number is 4, we know what should we return? Yes or no? If n is 4, what do we return? No difficulties. Now, that you have taken care of the end condition, nano, but I do not know. Let me change the editor because people are finding it difficult I think to see the, I only lose a few lines. You have e max? I do not think you will get the font size in g edit. Now, we have an end condition for our collage. So, now we can say if n is odd what do you want to do? Any way n will appear. It is not a bad idea to write the condition here again even though we know that is what is going to be satisfied to improve the readability and to make it easy to understand. It is not a bad idea to rewrite the condition though strictly speaking it is not required. If the function drops to that outside the second if, it is neither 4 nor odd. We know that much. So, there is no need for that third if, but improving readability is never a bad idea. So, we can do this. Once again if you are able to think in small chunks and then reduce to a previously solved problem, you will be able to write this code lot more easily than you start thinking in terms of if n n equal to n by 2. In other words at least for some people thinking in terms of recursion and thinking in terms of small cases and reducing to those cases helps. Some people say recursion is difficult, but many people find it easy. So, we will take one more example of this type of thinking before we move on to the next problem. All the theory is fine, but does it work? So, we might as well run that first. I have to have something to test it. It only has a collage function. So, in order to test it, I need to give it input. So, made a typing error collage is a function. So, it should be called with n by 2. I typed the wrong brackets. We did not ask the final to be printed. We just called collatian. We should have it printed. Reasonable seems to work. Then the last problem we will discuss now. Let us look at the vowel problem. Now, we know the answer for this is supposed to be E O. So, what do we do? This is in my opinion a straightforward enough problem. The only question is to decide whether you want to do it in a way where you check whether something is a vowel and copy or you want to check whether something is not a vowel and not copy. In other words, remove both are possible. But it is simpler to just copy if it is a vowel. So, we are told that we need only lower case. So, this helps. After that, we start with a empty list or we will start with a empty string. Not a problem. Both work well. We know a far loop can be used to step through a string. That is all. In fact, it is not even required to convert it before. We could have easily written dot lower converting it here. That way the function need not affect the given parameter at all. So, you want to bake it into a function and test it out. You may want to do that. So, for the code one last time, then we will once again think in terms of python's idioms of far loops and in. Do not necessarily think in terms of the other languages you know. Do not use an index to look into each character in a string. That is a very bad way of using a far loop in python. If you find yourself using an index variable in a far loop or using something like for i in range n, 90 percent of the time you are writing c code. You happen to be writing in python, but it is not python code. 90 percent of the time a far loop using the range of an object, the length of an object as a range means you are writing c code. You are not writing python code. We started yesterday by discussing arrays. We talked about operations on arrays. We said there are some special methods like zeros and so on, but we said operations on arrays. We made a comment on the fact that everything happens on all the elements of the array. So, then we said we will discuss accessing and changing elements. That we did. We talked about how indexing works very much like this. We talked about how to access columns. We talked about the a comma b notation for row a column b and we made an observation that column can be used to indicate all and that gives us a very simple way to access a particular column or a row. Then we stopped there saying we will discuss slicing particularly using an image processing as a mechanism. Let us do that. Having made sure we are in the right directory where the data is, we are saying do a command called im read. This loads, im read reads an image and loads it as an array. How do we know? Not because I say so, but you can look at what is the type of I? It is an array. What is its shape? 300 by 300. So, that is the size of the image. It is a 300 pixel by 300 pixel. So, we can now, if this represents the 300 pixel by 300 pixel image, if I want any portion of that image, I simply have to choose the corresponding elements of the array. What do you mean by that? Slicing, we talked about works just like with list. We make that point. So, if you want half one-fourth of this, then think about that and we say half the x coordinates and the half the y coordinates is what I require. So, I colon 150 will take all the elements up to the x values 0, 1, 2, 1, 49 which is the half of it. Similarly, the y values that is, now this, these pixels represent one-fourth of the image. How do we know? We can use an im show which will take an array and interpret it as an image. There you go. But, is this the picture? How do we know? Yes, let us do an im show of the whole image first, so that we know what you are talking about. This is the complete image squares dot p and g. So, we know the plotting tool nicely gives us the numbers to show us that. So, we know that x 150 will stop here. 0 to 150, please note, the way numbering is done. 0 to 150 would be this. So, now we can see where this is coming from. Now, if you want to see both, we are better off doing a figure command, so that this is the complete picture. This is the top corner. This was arrived by looking at treating it as an array. The next example given is 75 to 25. We should take a look and try to decide. 75 is here. So, 75 to 225, it will produce the inner four squares. So, this is the top corner. Looks good. Let us try it. 75, 25. Oops, I forgot to change the figure. So, but it introduced what we wanted. Figure 2 is producing the inner square as we saw. So, this is what the slides show. We are giving a very simple example again. Compress the image to a fourth. Let me do something else, so that it goes back to. Figure 2 has the full picture. In figure 1, I want to… This is the full picture. This is 75 to 125. Very good. Now, let us work through the next command and see what will happen. What does this command colon colon to? What does that say? Take every alternate element. In the case of an image, we are saying every alternate pixel in the x direction and every alternate pixel in the y direction, which essentially means every fourth pixel in a square of 2 by 2 is going to be taken. So, the total image size will be reduced to one fourth. Now, it looks like the same picture. This is the original and this is what has changed. You notice carefully what has changed is the numbers on the axis, which tells you the image has effectively been shrunk both in physical size and the size of the file would have come down. Now, this picture, this image has a very simple pattern. Somebody makes a comment on what exactly is, why do we need IM show? Why cannot we just print? What is I? If you just print I, you will just see a whole host of zeros and ones. I python is a little intelligent. So, it would not print every single element. So, essentially, we are treating the image as an array of pixels with the value there, the color there, everything defined by the values in the array in that position. So, you cannot print I or print this two and expect to see anything meaningful. What instead you have to do is, do an IM show. IM show treats it as interprets as an image and then shows the corresponding image. The array is a representation of the image. So, please remember the array did not is converted. Let us go back to the slides and see. Squares dot PNG is a regular picture. So, where is it available? It is available in the test files. This is the PNG file as it is stored in the OS and this is what we are trying to read and we read it into a variable. There is a question which has been asked. The picture is in gray. Why is the output in color? We will explain that in a few minutes. But please note what we are doing is to read that pictures, constituent elements, the element into an array and this array is a representation of that image, but the array is not the image. So, for it to be displayed as an image, something which displays an image has to be there to take a one minute extra on that. For example, each byte in a file is simply 8 bits. So, it is simply a number between 0 and 255. But you do not know whether it represents the numbers 97 or it represents a letter which is the corresponding ASCII value. Now, how do we know that? We do not know that. Only by looking at the number, there is nothing which tells us whether it is supposed to be an integer or a character or even more confusingly, it would not be either. It could simply be something else because it is in the middle of one 4 byte sequence or 8 byte sequence representing a floating point number. So, simply asking what does 97 represent without giving any context is not correct. But assuming there is no other context, still you cannot answer the question because it could still be a 97 could still be a number or a character. In the same way, an array of 1s and 0s can be just that an array. But please interpret it as an image, then I can interpret it as the 1s represent a black dot, the 0s represent a white dot or the other way around and then say this is my picture and this is the size of the picture is the size of my array, whatever is the value there represents the value of the color in that pixel position. So, the shape of the array represents the size of the image in pixels and whatever is there in each element represents in some coded form the value of the pixel. In other words, what is the color in that pixel? Now, that is an interpretation, that interpretation is provided by I am read and I am show. But if you say I am show of an arbitrary array, it is not going to work because it may or may not be in the correct format. We will move on. If arrays are this one of the simplest way to implement matrices in Python, what do you mean by implementing matrices? We want to be able to use all the matrix operations we have as you can see on this list. You want to transpose a matrix or take its determinant or compute the Eigen values of a matrix or the inverse of a matrix all of that without writing any of the code you have seen in numerical analysis courses. You just want to use it, then there is a built in method available already. You simply have to declare an array of the correct shape and load the data what is expected in the matrix. We will take half a minute to look at this and understand that all these methods are defined on arrays. Some of them may or may not have a direct for example, I do not recall the sum of all the elements ever being usefully discussed in a matrix class. I do not know, I have not done much of computation of this type. Now, let us see how to use these and we will talk about one of the earlier problems we have seen in order to provide the motivation for it. So, we have loaded the data, this is L, this is T. We know how to calculate square of it or we could have as shown here, we could have simply set T star T because we know matrix operations are done element wise. Now, if I plot L versus T square as you probably remember, the first B O will plot in using dots in blue color or will plot a line in red. Let us see how it looks, that is the blue dots, that is the red line, that is a terrible graph. Now, this is not the line we want to draw when given experimental data of this form, this is not a line. So, normally when we want to plot a trend showing, if you want to show the overall trend rather than the individual, we would like to look at what is the best fitting line that passes through given these points, what is the best fitting line. So, that is what is the very definition of a least square fit. Technically speaking, the least square line would be that line which when drawn, the square of the error between the line and the point for all the points is minimum. That is you draw a line y equal to m x plus c and you will get a line and not all the points will lie on the line, it is obvious looking at this, but you can see a broad linear shape for the way the dots are distributed. Then out of many possible lines, you can imagine from somewhere between this to here to here, you can start anywhere and somewhere between here to here, you can end anywhere and join any point on this area to this area, any of those lines could be thought of as potential candidates. One of them has this very nice property that the error between the actual point on the line, the square of the error is the least. So, that is why it is called the least square fit and doing a least square fit is one of the standard things we do. The slide explains that in detail, we are trying to fit a least square fit between t square and l. So, t square equal to m into l plus c, but now note that t and l or t square and l in this case are matrices. So, we are trying to do a matrix equation as the slide explains. We need to find best values of m and c for this. In other words, think of this is a matrix equation and we have two unknowns m and c to solve. So, in Python, you almost always type something and there you are. We first create a new array. You remember the ones like and zeros like we discussed yesterday. So, we will get one pair of brackets was missing. Now, what does that give us? Let us go back to the problem description and see. This l 1 to l n is all the various l data we have read into that l and it is supposed to have another one in this. That is the way to matrix representation of this equation. So, we need to first find a build a new array of that type. So, you look at type of a, a dot shape. This 2 rows by 90, but we want 90 by 2. So, transpose it. Now, you have a and t s q. So, the problem we are trying to solve is result has a lot of stuff in it. Result 0 contains something you want. So, that is the m and c value. The other elements of result give you some estimate about what is the possible error in the values and so on. You can read up the manual entry for l s t s q. That is l s t s q question mark to understand more about this. Sorry, the error is due to the fact I typed a full stop rather than a comma. Now, we have got this. So, now what we want to do is we want to print a line using this m and c values y equal to m x plus c, but we know what the x values are x values are the l s. So, we have to instead of t, t square we are going to have something called t s q, which is generated through a fit. We will clear the plotting area, then say do this, which is all the blue points. Now, instead of connecting every one of these lines, every one of these dots, sorry, instead let me plot l against t s q fit, but this time use a line. So, the data points will be the experimental data points and the line would be the least square line for those points giving us something like this. I am not sure probably the size is too small. Let me increase the line width. So, that probably is more visible. So, as you can see not all the points. In fact, very few points seem to actually lie on that line, but rest assured that is the best fitting line in the sense of the least squares. Now, please note even if you do not understand the theory behind least squares and all that you are required to produce a least square fit. All you need to do is just convert it into the right format and then type that one line l s t s q. You will notice that using python to solve these problems is extremely simple.