Python for linear algebra (for absolute beginners)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone my name is Mike his name is mister plant pretty sure it's a guy anyway in this course you will get a gentle introduction to using the Python programming language to learn a branch of mathematics called linear algebra linear algebra is all about organizing numbers into vectors and matrices in order to implement computations linear algebra powers statistics machine learning deep learning AI telecommunications signal processing computer graphics pretty much any computation that is done by a computer is done using linear algebra programming is a really powerful way to learn mathematics particularly if you think you are not very good at math so the purpose of this course is to give you an opportunity to see if you'd like to pursue studying mathematics using coding this course has a little bit less than two hours of video instruction and every video has a coding exercise at the end so if you do all of the exercises then the whole course altogether it's probably around three hours of time at the end of the course you will either realize that you really like learning this stuff and then you can take more advanced courses that will really teach you how to be a mathematical programmer or you will decide that this isn't the right path for you and that's totally fine it's much better to spend a few hours in a free course to figure that out then to spend a bunch of money buying a bunch of books and a bunch of courses struggling through them for hours and hours and hours after days and weeks only then to finally realize that this isn't the right thing for you you don't need any previous experience with coding or with linear algebra to complete this course the only thing you need is motivation and willingness to commit three hours of your time to learning something new you got nothing to lose so come join the course welcome to this course I hope you are excited to spend the next two or three hours learning about Python learning about numpy learning about using Python as a tool to learn mathematics in particular a bit about linear algebra in this video I'm gonna show you how to connect to Python online so you can participate in the coding exercises and follow along with me in the lectures so what you do is you go to this website Jupiter org slash tri just be mindful of the awkward spelling this is not Jupiter the planet okay you're gonna get to a site that looks like this there's a couple of buttons now you know it might change slightly over time but there's gonna be some button might be the top left that says try classic notebook that's the one you want classic notebook with the blue and yellow snakes on it like this so we click on this and that's going to open up another site called binder now it might take a minute or so to load and if you get an error message if you don't get to a page that looks like this and instead it says something about the servers being full then basically just wait 30 seconds and try again refresh the page eventually you will get through to a website that looks like this this is our MAME Python window and we can interact with Python here now this main page here already comes with some introductions to using Python notebooks and Python in general so you can click on these these are hyperlinks we can do you know notebook basics and then we can go through and sort of read this and basically do what it says and learn a little bit about the Python notebook now if you want to do that on your own that's fine if you were curious to learn more about Jupiter notebook then go for it you can go through all of these but just to be clear you do not need to go through these for this course I'm gonna tell you everything you need to know for this course so this will be just you know if you want to learn more on your own so when you get to this site you go up here click on file and then new notebook and python 3 and then this is going to open the Python window that we are going to be using in this course so this is called a cell this is where you can write code let's see like three plus seven and I'm gonna press on my keyboard ctrl enter and you can see that gives me an output of ten now I'm gonna talk more in the next video about what this means how to interact with all this stuff and so on the main thing I want to tell you here is about saving these files so the thing is we are not actually running Python locally this is not running Python in the browser instead this is running a session of Python somewhere on a remote server I don't even know where it is it's on planet Venus or something it's somewhere else now the thing is this server could cut you off maybe there's gonna be a server overload maybe you're gonna have some you know the internet is gonna hiccup or what also happens if you spend too much time idling I think it's 15 minutes or something if you spend 15 minutes or whatever the time limit is without doing anything without interacting with Python here you are going to get cut off from the server now I really do not want you to be in a situation where you spend you know half an hour writing really great code you're working really hard on the exercises and then you you lose the connection to the server so make sure to save your work and then download it you can download this entire window all the code that you've written you can download it here by clicking this button that will download it automatically if you use github or binder you can also connect with those and then store your scripts on github or binder if you don't know what these are then don't worry about it it doesn't matter you can also click on file download as and then you want to download as a notebook so you can see there's multiple formats here you want the notebook dot I py and B this is for ipython notebook and then once you've downloaded this if you need to you can go here back to file open and then that's going to open this other window which is actually listing all the files that we have in our current session and then you can click here on upload and then that's going to allow you to select a file on your computer so then you can select the file that you've downloaded and you will see that file here and then you can click on that and you get back to the file that you were working on before so again just to reiterate this is not Python running locally on your browser this is Python running on a server this file is in your browser but as soon as you close this file or if something happens with the connection with the server you could possibly lose your work so don't forget to save it connect to a service or download the file so you can re-upload this again so you don't lose your work ok so I actually recommend for this course going through Jupiter online just because it's easy I want the the entry barrier to be low you don't have to install anything locally on your computer for this stuff to work all that said if you are going to be more seriously using Python in the future for other more longer or more in-depth courses if you're gonna be using Python for you know data analysis or statistics or whatever your web development anything then I actually recommend downloading Python and running it locally on your computer the best way to do that is through the anaconda package you can get to it at anaconda com and then you would go to individual Edition and you have to let's see find download there's a couple of things you have to click here so it's gonna be you know depending on your operating system and then you you basically always want python 3 not python 2 and i'm not going to go through all the installation and how to use it installed locally on your computer because in this course i want to keep things simple which means i'm gonna keep things online but i'm just letting you know about this in case you decide after this course that you really are serious about learning Python then it's probably best to shift from doing everything online to doing everything locally on your computer so let's get started by creating some very and doing some really simple arithmetic so first what we need to do is is make sure that we're actually going to be typing inside this cell of code here notice that it's blue it's highlighted in blue here and then when I click inside the cell it turns green that's how we know that we are typing actively inside this cell I don't notice also that this this is empty here and in between these square brackets it's empty you're gonna see that change in a moment so I'm gonna type a equals three now so far that doesn't do anything we haven't really run any code we haven't created any variables this is just some text on a screen what we need to do is tell python to actually evaluate this code to run this code there's a couple of ways to do that one of which is to press the Run button here so that's going to run whatever cell is the active one whatever cell you're currently typing in now notice that this number changed to a 1 it was blank it looked like this and now we get a 1 here so that tells us that Python ran all of the code inside this cell and it labelled it as 1 and you'll see we're running again and it's gonna become a 2 so each time you run a cell our each time you run some code in Python Python will update this counter and it just lets you know how many lines of code you've run ok now this you can do but I find it gets a little bit tedious because you keep having to use the mouse and so on a shortcut a keyboard shortcut is you press ctrl enter on your keyboard and notice I press ctrl enter and then this turned into a 2 now a few other little keyboard shortcuts are handy as well and you'll learn those a little bit later in the course ok so now we've created this variable but what does that mean what is this variable so what we've done here is assign to the variable a the value 3 or the number 3 so now I'm going to type a again without any other assignment and now we get an output we didn't get that before so now we have an output and that's because here Python doesn't think that we are interested in seeing anything and here when we don't have an assignment like this we just have the variable name on its own then we get the output so we see that the value of a is equal to three so there's more that we can do with this we can say a plus two and that's going to give us an output of five so a corresponds to three and then plus two is five no we can change this as well we can make this how about seven point one so now I expect this to say nine point one and it does so that shows that we can do some really simple math with Python and we can also work with more than one variable so let's set a variable B and I'll set that to be ten and now I can say a plus B and maybe just to make things a little bit more interesting I'll say a plus B star to the star here's for multiplication so now I'll press control enter and run this cell and now we get twenty seven point one let's make sure this makes sense we have seven point one plus ten that would be seventeen point one but then we're multiplying the 10 by 2 so that gives us twenty seven point one okay so that all makes sense now if you are just getting started with Python you have a tiny tiny bit of code it's fine to use variables that have a single letter like this but once you start really programming and python or any other language you will find that using just one letter variables gets to be really confusing it's really taxing for your memory because you have to remember what variable a means what variable B corresponds to so in practice it's better to use more meaningful longer variable names so I'm going to set a variable name called Mike and set that equal to how about 17 so let's see what happens if I say Mike times B plus 1 okay so that gives us a 171 so on 17 times 10 plus 1 now I want to try this again Mike how about raised to the power of B so to take powers in Python you use two asterisks so it looks like you're doing multiplication twice but this is actually the special way to do to raise powers so let's say mike ^ b now that's gonna be some really astronomically large number at 17 ^ 10 so that's the this number whatever that number is but hey what happened to this number we lost that one that doesn't show up anymore in the output here so it turns out that python only prints out the very very last thing in a given cell now if you want to see if you want to print out multiple results within the same cell you can use the print function so we write print and then open parenthesis and then close parentheses at the end of the line so now we're gonna get this answer from print and then we get this output from the last line now you can see this doesn't say out and this does say out so in fact I'm gonna write print here again and now we don't get any out messages we just get these things printed out now I prefer the this kind of visual formatting I think this looks nicer than having it something that looks like this but that's just my preference the important thing to know is that if you want to output multiple variables if you want to inspect multiple things inside the same cell you need to use the print function and the way that works is you just type the word print and then in between the parentheses is the thing that you want to print out now let's say I want to go back and change some of these variables here so I'm gonna change B to B 0 and I want to change B to B 0 because that's interestingly enough that's gonna give me 0 or 1 for both of these expressions so now I change this to 0 you can see it's 0 and now I'm running this again and I get exactly the same result here so what is going on well this is like what I said in the beginning of this video that this is just some text on a screen this is not anything that gets stored in Python until we actually run this code so we have to press ctrl enter again run this code again and now python is going to overwrite the previous value of b with what's specified here so then I can run this again and now the numbers here change B cuz I've changed be from running this code again okay so now you can see we only have these two cells here and I was pressing ctrl enter so I'm gonna show you another keyboard shortcut here for creating a new cell so we're in this cell I'm gonna press shift enter instead of ctrl enter that runs this line at this cell of code and then it gives me a new empty cell a blank cell underneath okay what I want to show you now is that we can create variables that are strings as well as in fact many different types of variables in Python so these are numeric variables because they are storing numbers there's some more subtleties here as well this is an integer and this is a floating point decimal but for now it's sufficient to know that these are numerical I so now I want to create some strings so let's say I'm gonna create a string called greeting and it's gonna say hello my name is and then I run this and of course nothing happens we don't get any output now I could print this out I could say print greeting and it says hello my name is now different people and different cultures have different customs of course but I believe this is a little bit rude you know you don't just walk up to someone and say hello my name is and then walk away instead you have to tell them your name so let's say my name my name is Mike so how do I say you know I want to print out here hello my name is Mike but these are two separate variables maybe you try doing something like this let's see if that works hmm okay so first of all this illustrates one of the ways that you learned programming is you just take a guess and you try it and see what happens sometimes it works very often it doesn't but when it doesn't work hopefully you learn something so here we get our first error message in Python this has invalid syntax so clearly it doesn't like something here it turns out that all we have to do here is add a plus sign so we are not really adding greeting and name that's what we were doing up here but instead we are concatenated them so this is a concept in computer programming called overloading so we have exactly the same symbol the same character but it's it has different meaning depending on the context so that's called overloading here the plus sign means addition like with math and here it means string concatenation so now we can run this again and look at that beautiful result hello my name is Mike but you know what we need a punctuation mark here so I want to add a punctuation mark I'm gonna say plus and now we could create a variable that would be just a period but instead I'm going to input it directly into this print function here so plus and then how about an exclamation point like this so hello my name is Mike this looks nice alright so we are making some pretty good progress here there's one more thing that I want to introduce you to and then I will give you the assignment for the end of this video yes there is an assignment there's an exercise at the end of this video so let's say I also want to print in my name so I'm gonna say age equals I'm too shy to tell you my actual age so let's just say that I'm 10 years old I know I have a very deep voice for a 10 year old but that's okay so now I want to say hello my name is Mike and then on this next line I want to say I am and then it's gonna be something we know it's gonna be age plus years old by the way notice that I'm putting spaces in here because otherwise this we're not gonna get any spaces in the text so let's try this and we get an error the this the error message says must be stir not int so what it what do these mean well sometimes the Python error messages are really easy to interpret and sometimes they are a little bit less friendly or useful so as you work with Python more you'll start to be able to interpret these a little bit better this says that what we're inputting here must be a string which means characters like this letters inside quotes and not an integer which is what we actually have here this is a number this is an integer so basically Python is saying you know I'm working with strings here I don't know what to do with this number so what we need to do is convert the number 10 into the string 1 0 and we can do that using a function called STR notice also that you have these special functions and they turn green the font turns green instead of black so let's try this again all right so this is pretty cool we can say hello my name is Mike and I am 10 years old now obviously this is not like some amazing super awesome advanced programming but this is a great way to get started ok so now for your assignment this is your homework problem so in this assignment I'm going to write out a message and you are going to have to fill in the rest of the blanks basely to get this message to work ok so here's some text so what I want you to do after this video and before starting the next video is to basically copy all of this so you want to write all this stuff down on your screen and then you can try running this you see it's it's just not gonna work for many reasons so so basically you have to just fiddle around with this code until it produces a message that looks nice and that you are happy with and along the way you can inspect some of the things that I've put in here that I haven't told you about so you're gonna have to play around a little bit and see if you can figure out what these various things mean based on what you see in the output I'm gonna start here by introducing you to a new variable type this is called a list I'm gonna create a number list and it's going to be just a list of numbers how about something like that so you can see it looks a little bit like the variables we created in the previous video where we specify the variable name and then the assignment the equal sign but now we have multiple numbers now it's important that all of these numbers are separated by commas and the whole thing is encased in square brackets not parentheses and not curly brackets but square brackets so this gives us what's called a list so we can see what it looks like here so this is our list and in this case it's a list of numbers now the list type this variable type in python is pretty versatile you can do a lot more things with lists than just storing individual numbers like this but that's sufficient for now now hold that in mind what I want to do is show you that the basic Python version is really limited there's a ton of stuff that we cannot do with basic Python and that's why we need to import modules that are more specialized for different areas of of processing so for example let's say we want to take the absolute value of -5 so we know that the absolute value of a negative number would are the absolute value of a number in general is its distance from 0 so the absolute value of -5 should be 5 now there is a function called ABS for absolute value and that does indeed return 5 so that's the correct answer let's say we wanted to do something else what if we want to take the square root of 4 now I'm just guessing that the square root function might be sq RT but you can already see it's probably not right because this turned green and here we have black text so it turns out that Python actually doesn't have a square root function how about let's try something else what if we wanted to take the average of all of these five values so you know the average we sum them all up and then we divide by the number of numbers so you could try something like average of num list and that's not going to work oh maybe it's called mean no but that also doesn't work it turns out there is no function in Python to compute the average in the base environment of Python now you're probably thinking that Python is totally worthless you know how can we use a program if we can't even compute the average of numbers well it turns out that in Python there are many many what's called modules or sometimes called toolboxes that allow us to do a lot of other kinds of processing that is not with functions that are not in the base environment so here's how we import a module we type import and then the name of the module and in this case I'm going to be using something called numpy and now I'm gonna write as NP so there is entire module it's a set of functions I don't know how many functions are in numpy it's got to be hundreds and we are now importing the numpy module so we're gonna import all the functions inside numpy into our current workspace of Python and I'm gonna call this NP so let me show you what that does so now meme still doesn't exist it's not a thing but mean is a function that's inside the numpy module so we have to type NP dot so this tells python that we are going to look for this function not in the base environment of Python but instead inside the numpy module and now we get an answer so it turns out that the average of these five numbers is 2.8 and you know just from looking at the numbers that seems pretty plausible and of course numpy also has a square root function so we can take the square root of 4 and that gives us 2 as I mentioned there are many many functions inside the numpy of module I'm going to introduce you to another one that's called linspace so the function linspace will create linearly spaced numbers between the first number let's say it's 10 and the second number sorry I'm at 1 and then the second number which is 10 and then how many do you think we should just shout out shout at your computer screen maybe I'll hear you in the past how many numbers do we want between 1 and 10 now well 7 I think I heard someone say 7 ok so now I'm gonna run this and this returns this gives us an output which looks a little bit different from the kinds of outputs we've seen before this is something called a numpy array but you can see it's really really similar to a list it's it's almost the same thing as a list it's kind of a special case of a list so we get one two three four five six seven numbers and the smallest one is one and the largest one is ten now you might be wondering you know how do I learn all of these functions that are available in all the modules in Python well the answer is nobody knows all the functions that are available in Python and the what the more common ones the ones that are more relevant for what you're working on those you will eventually commit to memory just by using them over and over again and other ones you just you know you know what you want to do in Python and then you look it up online and you find it really simply now I want to show you another way that you can get help on these functions so if you know what the function is called but you're not really sure what the inputs are or how the function works you can press so click with the cursor inside the function inside the parentheses and then you press shift tab on your keyboard and that opens up a little tiny window which you can expand with this plus sign and this is called the help text or the doc string and basically this tells you how to use this function so the first input is the start the second input is the stop and then if you see inputs with an equal sign that means that these are optional so the default value for the number of steps between start and stop is 50 so if we don't put anything in here then we're gonna get 50 numbers between 1 and 10 or we can add a third input and well okay this isn't very interesting because there's only two numbers that are linearly spaced between 1 and 10 but I think you get the idea so maybe this is an important array this is an important output that we need to use in the future now it's a little bit tedious to have to type this thing over and over and over again so instead what we can do is create another variable and set that equal to the output of this function here so let's create a variable how about let's call it fun out so fun because it's the output of a function and also I'm having a lot of fun here I hope you are too so let's see I'm actually just gonna copy and paste this here and then this is just another reminder that we have code that no longer gives any output here because there's other code after it within the same cell okay so now we have this fun out variable so we can look at it we can say fun out and not terribly interesting it's the same thing as this and we can also use this in mathematical expressions just like we did in the previous video so I could say fun out plus two and now we get numbers that go from three to twelve instead of from one to ten all right so that was like a mini crash course on on importing numpy and using numpy now it's time for the assignment it's time for your homework so there's four parts to this assignment so part one is going to be to create and let me just get this a little bit higher on the screen so part one is to create a list of fifteen numbers from four to 100 and then step two is going to be to round those numbers to the nearest integer and store story store in another variable now I have not told you in this video or in the previous video how to round numbers to the nearest integer so you have two options for figuring out this part of the assignment either you can just guess and start playing around with function names that you think might do what you want and if that doesn't work or if you have difficulties understanding it then to go to the Internet and just search for how to round numbers to the nearest integer in Python you will you will be amazed at how quickly you get the answer or maybe you won't be the answer to basically everything is out there somewhere on the internet you just have to find it okay then you want to print out number two basically you want to print out these numbers rounded to the nearest integer and then the final step that you want to do here is to print out these square root of each number in the list so each one of these from from these in this video I'm going to introduce you to the matplotlib module this is the main way of visualizing data in Python so we're going to get started just by importing it straight away so I'm gonna write import and then the module is called the mat plot Lib now we don't actually need the entire matplotlib module we need only one section of it it's like a sub module so that's called dot Pi plot so we write import matplotlib PI plot now this is a lot to write so you know in the previous video you learned about numpy numpy isn't so much to write but imagine if you had to keep writing all this stuff over and over and over again so instead we write import as PLT it's pretty standard shortcut or abbreviation for matplotlib pi plot and I'm also going to import numpy as NP now notice that I'm putting all of the import statements all the way up here in the first cell at the top of the code script this is common procedure and it's a really good idea because then you know that kind of helps you organize your script it helps you know right from the beginning of the script which modules are being used in here now I'm gonna add something else it's a little piece of code that's called a comment so this comment is going to be import modules now notice that this comment is so the font is a different color and it's also in this italics format so it looks different from the rest of the code now when you have a hashtag in front of the in front of a line of code then python just completely ignores that line python is not going to try to interpret this as code so that's why this is called a comment these are for you know comments for for you to remind you of what the code is comments for other people to help them read your code and of course I've already shown you comments in previous videos particularly with lists the assignments but I didn't actually define what they were yet okay so import these two modules and now let's just do some basic plotting so I'm gonna right PLT dots because the function that I'm going to call here is not in the base environment in Python so it's only in matplotlib that PI plot which we're abbreviating as PLT so PLT plot now the way this works the way the plot function works is you input at the x-coordinate and the y-coordinate now I'm going to press control enter on the screen and now we see a plot so we see the axes here and the axes you know the coordinates look right it's 1 & 3 but we don't actually see anything in the plot so what's actually going on here is that Python drew a line but because that line is just a single point it's actually totally invisible so you know technically it's there but we can't see it so I'm gonna add an optional third input and that's going to describe the characteristics the visual properties of the line so I'm gonna make it a circle so we write Oh four circles there we get a dot now there's other options that you will become more familiar with as you continue using Python there's circles and squares and triangles and stars and all sorts of things I'm gonna go back to the making a circle and I want to change the color so I'm gonna set this to be red so R for red again there's a lot of other options you will learn as you continue learning Python for example K is for black but I'm gonna stick with red for now so you can also see we get this output here so I often write the PLT show and then we just get the plot now depending on which version of Jupiter you're using and how exactly or what with which environment you're using for python you may or may not need this in the standard current modern Jupiter notebook you don't technically need to use PLT dot show but it's often nice for other reasons as well okay now plotting in python would be pretty depressing if all we could do is plot 1 dot at so what I'm going to do now is plot multiple dots at the same time so I'm going to introduce you to a new function in the numpy module it's called a range now a range allows us to specify a range of numbers and we're gonna go from minus 9 to now it looks like we're gonna go to 10 but let's let's see what this actually does so I'm gonna print out X and although so we start from minus 9 and then we get all these numbers but then it goes up to plus 9 so this is a little bit of a quirky way that Python works sometimes it takes a little bit of getting used to these idiosyncrasies but when you when you use the numpy function a range then the second input is actually one pass the number that it goes up to so if you actually wanted to go from minus 9 to 10 then you would need to write 11 in here and then you get numbers up to 10 but I'm gonna stick with going up to plus 9 here because that's gonna give me you know I like this symmetry here ok so now I'm going to define a mathematical function I'm gonna say y equals x squared now you might remember what this function looks like x squared it's gonna look like a smiley face basically that's because these numbers are negative so when we square them they get positive and when we get closer to 0 the function gets closer to 0 the numbers the Y values get smaller so let's plot this here say PLT dot plot X comma Y and let's make this a red now I'm not specifying a red circle just red so this is actually going to give me a red line so there you go here's a red line and let's say we want to plot something else on top of this in the same plot no problem we can plot multiple things on the same plot it's really easy all we have to do is repeat this command so I'm gonna write plot PLT and now I also want to show you that in this plot function you can directly apply some arithmetic so let's do Y divided by 2 and I'll make this green squares okay so then we see we get two things on this plot so I don't know what you think this looks like maybe it's a big smile and these are teeth or something a big cartoon teeth you smile anyway so what I'd like to do next is show you how to plot not a set of numbers here but just a single line that goes from one point to another point well the answer of how we get it is kind of already embedded in here so X is a vector and Y is a vector and it's changing over space here so what we want to do to plot a line is just specify the two x coordinates for the starting and ending points and the two Y coordinates for the starting and ending points so let's say I want to create a plot that goes from 0 to 3 on the x axis and then from minus 1 to plus 1 on the y axis so let's see what this looks like so here we have on the x axis we're going from 0 to 3 so that's these two numbers here and then on the y axis we go from minus 1 up to plus 1 so that's these numbers here so that's how you create a line in Python you specify the start and the stop points for X and then the start and the stop points for y so now I'd like to add another line to this so let's see PLT dot plot how about let's go like minus 2 to 0 and maybe minus 4 to 1 just kind of making up these numbers all right so that looks pretty neat we get two different lines here but which line corresponds to which are which of these lines corresponds to which line of code well you know we could kind of look at the values and look back at here but it's possible that you're gonna send someone this graph maybe you export this as a file and you're gonna email this to your grandmother so you need a way to label these lines so your grandmother knows which line corresponds to which label so to do that we're gonna add another optional input here it's gonna be label equals and I'll call this I don't know first line and then here I write label equals second line all right so let's run this code again oh and nothing happened that was pretty disappointing well so what we need to do is activate the legend so we specify the legend labels here but we need to tell Python to activate the legend so we type PLT dot legend without any inputs we don't need any inputs here because we've already described what each late legend label is going to be and now we get the legend okay and now I just want to show you that the the location of where you place this show line of code is important so what I'm gonna do is cut this and paste it here so I'm just swapping the order here and now we actually get a little warning message and then we get to two axes here two plots so what happened here is all the all the lines of code that we have before the plot show that all goes into one plot and as soon as we call plot that show then we're telling Python to create a separate plot so everything that comes before this is one plot everything that comes after this and before the next PLT show or the end of the cell that's gonna go in a separate plot so you can take a moment and you know just play around with this for example you can stick it up here and you can see that creates the first line and that creates the second line okay but obviously that's not really what we want at the end of the day we want a plot that looks something like this now I'd like to introduce you to how to visualize images using Python so let's start with matrices because matrices are actually represented as images in in in graphics so let's see we're gonna start by creating a matrix now this is the first time I'm really mentioning this term matrix here in the in the course I'll give you more of an introduction to matrices in a few videos but you know a matrix is just like a spreadsheet of numbers it's a table of numbers so I'm gonna use this function numpy it out random that Rand int so this is going to create random integers between zero and nine remember this is an exclusive upper limit just like with numpy a range that we saw earlier and we're gonna make the size of this matrix be four by five so let's see what this matrix M looks like so here we get a numpy right and you can see that it is four by five so there's four rows and five columns again I'm gonna talk about matrix and matrix sizes and things like that in a few videos from now but you can see that this is just a bunch of integers and it's a four by five matrix so we can keep running this over and over again the size of the matrix stays the same because these numbers are fixed here but the numbers in the matrix these values are changing every time I run this code and that's because we are generating random integers Rand int random integers so it looks different every time okay so we can visualize these this matrix like this just look at the numbers now for small matrices that's fine but once you get to larger matrices it's not really feasible just to print out they you know if you have like a 400 by 800 matrix you can't just print it out and look at all the numbers so instead we are going to create an image so it's say PLT dot I am show X so this is for image show and then PLT dot show so now we have a matrix which is an image so each of these pixels each one of these squares corresponds to one item in the matrix and the color of each square corresponds to the value so you can see at the bottom row we have two zeros and then a nine at the end then in here we have two very dark blues and a bright yellow so you can see that the color in each cell here in each little box maps on to the value of the of the element in the matrix okay and so now we get to our assignments so in this video you learned about making lines you learned about drawing lines on plots and you learned about drawing images so what you want to do now at the end of this video is may an X going through this matrix here so do you want to create a red X like you're crossing out this matrix so you need one line that's going to go from here to here and then another line that's gonna go from here to here now that you have a bit of a grasp on using Python I'm going to start introducing you to some concepts in linear algebra so let's start with vectors a vector is an ordered list of numbers so this is a vector now these two vectors have the same number of numbers and they have all the same numbers but they're in a different order so these are actually not the same two vectors so the order is really important for a vector so as one vector this is a completely distinct vector however they are both three-dimensional we call the dimensionality of the vectors the number of numbers or the number of elements so one two three elements so this is a three dimensional vector here you see some more examples these this is a two-dimensional vector and this is a one two three four five dimensional vector now vectors can have different orientations they can be standing upright like this in which case they're called column vectors or they can be lying flat on the ground like this and then they're called row vectors now these are still all vectors they just have different orientations so they're upright and then they're column vectors or they're laying flat and then are called row vectors so I call this the algebraic interpretation of a vector because we're looking at numbers there is also a geometric interpretation of vectors and this is no different from the algebraic definition it's just a different way of thinking about it so if we take our vector this is a 2-dimensional row vector and we can draw a line that goes from the origin of a two-dimensional space to the coordinate at you know 1 comma 2 which is the first element and then the second element so this is a line here but it's representing a vector so you can see that you know this geometric interpretation works for up to three dimensions but you know in a previous slide I showed you a five dimensional vector so how do you draw a five dimensional access space well you know we can't so so the geometric interpretation of vectors is useful when you're working in two or maybe in three dimensions and after that we switch back to thinking about it algebraically now I want to introduce you to a mathematical operation on vectors which is called vector scalar multiplication so you you now know what a vector is it's an ordered list of numbers and a scaler in linear algebra it just means a single number it's a bit of a fancy term just for a number but you will see in the next slide why we call these things scalars so a vector is a list of numbers a scalar is just a single number not embedded in a list so to multiply a scalar by a vector it's really simple you just so here's an example you just multiply the scalar by each individual element in the vector so the scalar vector multiplication of seven and this vector becomes minus seven zero seven so we just multiply the scalar by each individual element that's pretty straightforward okay now I want to show you the geometric interpretation of vector scalar multiplication so here I'm just defining some new vector to be the vector V multiplied by the scalar lambda is a Greek lowercase character at lambda so that looks something like this so we would get so this is our vector V here in blue and then the vector W in this case is is longer it says yellow line now if lambda is greater than one then you're gonna stretch this vector you're gonna make it longer but it points along the same line and if lambda is between zero and one so if it's greater than zero but smaller than one then the resulting vector so the result of scalar vector multiplication is going to be smaller than the original vector so that you see here and the third possibility is that if the scalar is negative if it's less than zero then you're gonna take this line and like spin it around 180 degrees now it's interesting because you can see that the line the the resulting vector is still along the same line if you think about a line going from you know all the way down here to all the way up here the scaled version of the vector is always on that same line sometimes it's longer sometimes it's shorter sometimes it's twisted 180 degrees but it always stays on this line now you might initially think about creating a vector as a list which I've already introduced you to before so let's say you know three four five and how about go back down to two so this kind of looks like a vector this looks really similar to the vectors that I showed you in the slides and let's come up with a scalar and let's just make that two so now let's say you know I told you about vector scalar multiplications so you might expect this result to be you know six eight ten four however when we run this well we actually get okay well here we see a little asterisk here that means that python is getting hung up on someone sometimes happens when you're using Python online the server might be a bit busy or something so if you see something like this python isn't producing a result you can click on kernel and restart and clear output that will just clean up everything and start again from scratch now that doesn't mean that you will need to rerun all of the code that you've already run including importing these modules okay so now it turns out that what we get is not what we expected instead we get a vector that seems much longer or a list that is much longer and in fact when you see what's happened here we've actually or Python has repeated this vector twice so we can we can test that idea by saying three and now we get this repetition three times so three four five two three four five two three four five two so it turns out that although this list looks like a vector Python does not treat this as a vector the way we want it to be like a vector so instead we're going to convert it into a numpy array so we type num pi NP dot array and then in parentheses and now this variable Veck is no longer a list instead it is a numpy array and a numpy array is what we think of as a vector so now we can run this and now it works as expected so three times three is nine and then twelve fifteen and six next I'd like to show you how to plot these vectors in Python now you actually already know how to do this from the previous video so I can go through this rather briskly because this isn't a whole lot of new information here however we're not going to be able to use this particular vector because this is a four dimensional vector we would need an a four dimensional axis to be able to plot this line and python is not quite capable of that nor are you know we humans really capable of thinking about a four dimensional geometric space ok so let's create a new vector I'll call this vector D and this is going to be a numpy array and let's just set this to be 1 comma 2 so you can already imagine what this vector should look like geometrically it just started the origin and it's gonna go one up and I went over and two up so it's gonna be a line that looks something like this ok now I want to come up with a couple of different scalars is basically to illustrate the concept that I showed in the slides a moment ago s 3 equals minus 1 ok and then we need to start drawing this so I mentioned in the previous video that to draw a line you need to draw the x-coordinate and the y-coordinate so what are the the two x coordinates and the two Y coordinates well this is only showing one x-coordinate and one y-coordinate so how do we do this how do we figure out this this problem here well you might have guessed that's the solution is to appreciate that this line that starts at the origin and these numbers actually tell us the end point of the line so we plot 0 comma Veck 2d and hmm how do we actually access just the first element I haven't taught you about that yet so we we don't want the entire vector we just want the first element in this vector vector D so the way we do that is by something called indexing so the way this works is you type the name of the variable and then you use square brackets like this it's important that these are square brackets we're not using parenthesis like this we're not using curly brackets like this this is just square brackets and then we indicate the number that we want to access so this is the first element so let's try writing one and see what happens huh and now we get it too so what is the deal here Python doesn't know how to count well so I kind of in fact Python stars counting at zero so this is the zeroeth element we in human language would call this the first element but python calls this zero and that means that this is the one or the first element so vector d square brackets 0 gives us the first element and then number 1 index number 1 is actually the second element and index number 2 doesn't exist so you know this is only has two elements so index number two is actually getting the element in the third position and there is no element in the third position so that's why we get an error here it's a little weird it takes a little while to get comfortable with this way of starting to count at zero but that's how Python works ok and that tells us our solution here so we want to say back to D 0 and this is the x-axis coordinate and then we need the y-axis coordinate and that also starts at 0 and then it goes to vector D it goes to the second element of vector D which is actually index number one ok and then I'm gonna get make this be a blue line with squares and I'm also gonna put a dash here now let me show you what what that does so here if we didn't have a dash here we would get two dots this is the dot for the start of the line and end of the line but I want to have the the squares here and a line between them so I use B for blue s for square and the line for a dashed line okay now I am actually going to copy and paste some code in here because there's a lot of code that isn't really any so all this code is nothing you haven't already seen and just it's it's just writing it looks like it's a lot of stuff but this is the first line to create this blue line and then we have s1 x so now I'm just doing scalar vector multiplication the way that I showed in the slides now there's a couple of other things that I haven't introduced you yet to you yet here so this line makes the access be square you can see it's a square instead of being a rectangle if we comment this out then we have a rectangle here so here we get a square plot this sets the x-axis limit and the y-axis limit in both cases to go from minus 4 to plus 4 so minus 4 to plus 4 on both x and y and this activates the grid that's these grey lines here and then legend you already know cool so that's about drawing vectors and working with vectors now it's time for your homework there's another assignment always assignments at the end of these things so your assignment is multifaceted so what you want to do is create two vectors and these should be two-dimensional vectors because you want to draw them in a plot like this so to create two-dimensional vectors now you can specify the numbers on your own or you can create them as random integers I've already shown you in a previous video how to create random integers ok and then what you want to do is plots each vector and then you want to plot vector V 1 plus vector V 2 and then you want to plot vector V 1 minus vector V 2 and then you want to plot vector V 1 times 4 plus vector V 2 divided by 2 so there's actually going to be a quite a lot of in this graph that you're going to make here now this is interesting because I haven't actually told you yet how to add and subtract vectors but it turns out that adding and subtracting vectors is really simple and the way that you're thinking of it is probably the correct answer so to add vectors means you just add each corresponding element of the two vectors same thing with subtraction and then here just to make sure this is clear this is actually turns out to be one vector what we're doing is is combining two different vectors into one resulting vector this is called a linear combination of vectors it's very important in linear algebra the dot product is one of the most important operations in all of linear algebra it's super important and you see it all over the place and so in this video I'm going to introduce you to the vector dot product you can see there's several different notations that are commonly used in the literature but kind of highlights the importance and ubiquitousness of the vector dot product the notation that you most often see is something that looks like this so these are the two vectors vector a and vector B and there's a superscripted T that's in between them this is called the transpose operator and I'm going to tell you all about the transpose operation in a few videos from now but for now suffice it to say that the dot product is indicated by a transpose B or a superscript B where a and B are two vectors so what is this mysterious dot product and how do we compute it well it turns out that the dot product is really simple to compute all you do is take your two vectors first of all you make sure that they are the same dimensionalities of the same number of elements and that's the case here they both have five elements and then what we do is multiply each corresponding element and then add up all of those individual multiplications so the dot product between vector V and vector W is 1 times 2 plus 0 times 8 plus two times minus six plus five times 1 plus minus 2 times 0 and that gives us a total of -5 so notice that the dot product between two vectors is always going to be a single number and that's because we do all these pairwise multiplications and then sum so in the end the dot product is always just one number it's a scaler regardless of how many elements are in the original vectors and here you can see that the dot product is not defined for two vectors that have different dimensionalities so you know you can multiply these numbers here but then you get here there's there's no corresponding element in W so the operation fails ok so here we are and actually we don't really need matplotlib in this particular video but that's ok we certainly do need numpy so let's see i'm gonna create two vectors and then compute the dot product between them so numpy array let's say i don't know 4 5 6 2 and let's create another vector numpy array and let's make this one like minus 4 whoops minus 4 3 and I don't know 0 okay and now to compute the dot product between them now I explained it in the video or in the slide that you do all the element wise multiplication x' and some button numpy there is a function that does this for us it's called numpy dot dot then all we do is input the two vectors here now I can imagine you are screaming at your computer you're throwing things at your computer screen because you know that this is not going to work and this is not going to work because these two vectors don't have the same dimensionality so let's see what happens when we tried to run this code anyway this is often a useful thing to do in programming to run code that you know is going to give an error you know should give an error and then you can see what is the error message going to be so this tells us that these shapes are the dimensionalities are not aligned ok so 4 does not equal 3 so we can either delete one number from here or add one number from here I will add a five and now we get a dot product of nine between these two so now I'm actually gonna get rid of one of these things I think I'll get rid of this first minus four then they line up nicely like this okay so now the dot product is actually a different number of course because the two vectors are different than how they were before so let's create a new vector let's set this to be I'll go actually I'll go back to minus 4 so minus 4 3 1 and now we can compute the dot product between vector 2 and vector 3 so that's no problem turns out to be negative thought products don't need to be positive numbers they can be negative numbers and now I want to show you multiplying with a dot product with a different vector so let's call this V 4 numpy array 0 0 0 now this is a vector of all zeros and you probably won't be surprised to learn that the term in linear algebra that we use for a vector of all zeros is the zeros vector so what do you think is going to be the dot product between this vector V 4 and this vector V 3 well I'm sure you guessed it's going to be 0 no surprise there we're just multiplying by zeros and then summing up all the zeros so of course whenever you multiply a vector by the zeros vector you're going to get a dot product of exactly 0 there's just no other way around it but it turns out that's not the only way to get a dot product equal to 0 so I'm gonna create a new vector now so let's call this V v numpy array and then we input let's make this 2 comma 3 comma minus 1 so now I'm gonna compute the dot product between v3 which is this one and v5 which is this one that I just created here and let's see what this turns out oops little typo that should be v5 now this is pretty interesting because we saw that the dot product between these two vectors so this vector and this vector actually I'm gonna just copy and paste this down here just so it's easier to see these two numbers like this so we see that the DA between these two vectors is zero which is initially a little surprising because none of these elements are actually zero so we can do this math quickly in our heads just to confirm so two times minus 4 is minus 8 and then 3 times 3 is 9 so minus 8 plus 9 is plus 1 and then minus 1 times 1 is minus 1 so then that gives us a 0 so we can have a dot product between two vectors neither of which is the zeros vector and that dot product turns out to be 0 now this is a really special thing it's called orthogonal so we say that vector I'll write this whole thing out vector V 3 is orthogonal to vector V 5 orthogonality means that D so this is the definition of orthogonality that two vectors have a dot product of zero and that leads us to the assignment for this video so your homework assignment here is to so let's see so step one is going to be to come up with three vectors and these should be two-dimensional vectors because we're going to plot them so you want three distinct vectors that are in a two-dimensional space so they have two elements and you want to create the vectors such that two of them are orthogonal but not orthogonal to the third I should say neither neither is orthogonal to the third so three vectors you can call them V 1 V 2 and V 3 and or you can call them anything you like you can call them Mike Bob and Sally so create three two-dimensional vectors and make sure that two of them are orthogonal but that each of these two is not orthogonal to the third and again not orthogonal to another vector just means that the dot product is not zero it can be any other number as long as it's not zero and then what you want to do is plot all three vectors on a two-dimensional plane and this is important make sure make sure the axes are square and equal so this is like what I showed in the previous video where I showed you how to create an axis that are the same x axis limits as the y axis limits and the x axis and y axis let's go from the same positive to negative number so it could be minus 4 2 plus 4 or you know you might need to use different numbers depending on which vectors you come up with but it should be the same number negative and positive and that the axis is square so that's important because what you want to do with this exercise is make some discoveries about what orthogonal vectors mean geometrically so that you're going to discover some something interesting about two orthogonal vectors versus the vectors not being orthogonal to the third a matrix is a two dimensional collection of numbers it's like a spreadsheet when talking about the size of the matrix we refer first to the number of rows and then to the number of columns so this would be a 4 by 3 matrix this would be a 3 by 7 matrix because there's 3 rows and 7 columns so it's always in this order you wouldn't call this a 7 by 3 matrix it is a 3 by 7 matrix the diagonal of the matrix refers to all the numbers that start from the top left entry and go one down and one to the right so one down and one to the right and this gives us the diagonal of the matrix all the other numbers these are called the off diagonal elements now this particular matrix has four rows and four columns so it works out that the diagonal goes from the top left to the bottom right that's a special size of a matrix that's called it a square matrix when it has the same number of rows and columns so square matrix but non square matrices and I'm called rectangular matrices also have a diagonal to them it's just that the diagonal doesn't go all the way to the bottom right corner but it's still the same operation we start at the top left and we go one down to the Wrights that's the next diagonal and then that's the next diagonal there are many special kinds of matrices in linear algebra and now I'm going to introduce you to just a small number just to give you a little bit of a taste of these special matrices this one in particular is called a symmetric matrix and a symmetric matrix is a square matrix so the same number of rows and columns where all of the elements above the diagonal are mirrored with all of the elements below the diagonal so you can see minus 1 minus 1 0 0 minus 4 minus 4 it's like you know if you would print out this matrix on a piece of paper you could fold that piece of paper along the diagonal and then you you know fold this top part of the this top corner over down to this lower corner and you would see that these are all mirrored so let's say symmetric matrix here we have a special kind of matrix called the identity matrix the identity matrix has all zeros on the off diagonal terms and all ones on the diagonal terms so zeros everywhere except for the diagonals where every diagonal element is 1 now sometimes you see matrices with these dots in them like this these are like continuation signs it means that it doesn't really matter how many rows there are there could be another two rows or a hundred rows there could be five more columns or a million more columns so this is showing the general form so 0s on the off diagonals and ones on the diagonals the identity matrix is so important in linear algebra that it gets its own special letter which is the letter I so anytime you see a capital boldface I in linear algebra you know that that's referring to the identity matrix sometimes there's a little number down here and that would indicate that this would be this is referring to a two by two identity matrix is a 5x5 identity matrix so it's the identity matrix with five rows and five columns but more often than not you just see with a capital I like this the last special matrix that I want to introduce you to is called the diagonal matrix now it's a little bit confusing because I already told you about the diagonal of the matrix but this so that that's this one minus 2/5 that's the diagonal of the matrix but this whole thing is called a diagonal matrix and what makes this a diagonal matrix is that all of the off diagonal elements are exactly equal to 0 every single one of them and the diagonal elements are nonzero they can also be zeros but only the diagonal elements are allowed to be nonzero so here's an example of a square diagonal matrix here's an example of a non square or rectangular matrix which is also a diagonal matrix because all the off diagonal elements are zeros and the only nonzero elements are in the diagonal of the matrix now of course in the previous slide I showed the identity matrix which is a form of a diagonal matrix so the identity matrix is a special case of the diagonal matrix I'm going to show you how to create some of these special matrices that I've already introduced you to so let's start with a identity matrix so there's a really neat formula here called I it's spelled I like the I in your face but it actually produces the identity matrix so we can write for example 3 and then we see the 3 by 3 identity matrix now because all identity matrices are square all identity matrices have the same number of rows and columns so we don't actually need to specify separately the number of rows and the number of columns and you can see this is the identity matrix that has ones on the diagonal and zeros on the off diagonal so that's for the identity matrix we can also create a matrix of all zeros now the thing is that a zeros matrix does not necessarily need to be square so we need to specify here that how many rows we want and how many columns we want so I'm going to create a 3 by 4 matrix here and you can see it's a matrix of all zeros and there's three rows and four columns okay and then there's one more function I'm gonna introduce you to here and that's gonna be that's called full so what a full what the full function allows us to do is create a matrix where we got to specify the size let's say 5 by 2 and then we can specify a number to fill in the matrix so this is going to be a matrix that's five by two so five rows and two columns and python is going to set every element to be seven and that's because I press 7 here so I said 17 s and now we get a matrix of all 17s so these are a couple of functions to create vectors you can also create your own vectors and I'm gonna show you that now so let's call this matrix M it's good letter for a matrix numpy dot array so you can see we are starting here in a similar way to how we created vectors with numpy and now the way this works for creating matrices is you have to use one set of square brackets like this to indicate the entire matrix and then you need another set of square brackets for each individual row so let's say 1 2 3 so this is gonna be the first row and then a comma and then the second row let's make that 4 comma 5 comma 6 and then let's make a 3 by 3 matrix so so it's 2 7 8 9 okay and then we can print out m and see what this looks like now this is a little bit confusing there's a lot of square brackets and we have all three rows on the same line now Python doesn't care this looks fine to Python but it's a little bit harder for human beings to read so what I'm going to do is just press ENTER here and just space this out a little bit and then run this again just to show you that to Python this is the same as how I had it before but of course to humans this looks this is you know more readable so now we see the first row the second row on the third row and then we see the the outer most brackets for indicating that all these rows goes into one numpy array one object okay so that's a pretty quick introduction to matrices now we get to the assignment the most fun part of these these videos on short so let's see what you want to do here is create three matrices and you need to specify their sizes so one matrix is going to be 2x2 another matrix is going to be also 2x2 but different numbers from this first matrix and then you want another matrix of three by two so three matrices that have two different sizes now you can create these matrices to be number that you specify like this you can create these matrices to be random integers like what I showed in a previous video that's all fun and then what you want to do is compute scalar matrix multiplication so actually there's three parts to this assignment so this is step one is to create these matrices step two is to compute scalar matrix multiplication now I have not told you in the slides how to implement scalar matrix multiplication but I did tell you about scalar vector multiplication so what you are going to do is discover whether scalar matrix multiplication is the same as scalar vector multiplication okay and then step three is to add all pairs of matrices so you know if you call this M 1 M 2 and M 3 then you're gonna add M 1 plus M 2 M 1 plus M 3 and M 2 plus M 3 and again I haven't yet told you how matrix addition works how it works with adding two matrices but you are going to be using Python as a tool to help you understand these concepts in mathematics in linear algebra and you will discover in the process of step three you will discover an important rule about matrix addition I've already mentioned to you this concept of transposing a vector or a matrix so let's see what this operation actually means so here we have a vector this is a column vector in particular it is a five dimensional column vector and we can transpose this column vector and that converts this column vector into a row vector now the important thing to know about transposing is that it doesn't change the elements themselves and it doesn't change the ordering of the elements so we have exactly the same numbers in exactly the same order all we've done is change the orientation of this vector from a column vector into a row vector and that's what the transpose operation does now the transpose operation in general flips rows into columns and columns into rows so transposing a row vector would get you into a column vector this isn't necessarily only about going into a row vector it's just about swapping whatever is the already existing orientation so this is for transposing a vector transposing a matrix is essentially the same thing so here we have a five by two matrix so two columns and we can transpose this and that gives us another matrix where the first column corresponds to the first row and the second column corresponds to the second row now the only thing you have to keep in mind about transposing is you might be tempted to think about the transpose operation visually such that you are just like rotating this matrix 90 degrees like this so imagine you put a pin in the five and you're rotating you're just swinging this matrix up that seems like a nice visualization but it doesn't work unfortunately because that visualization would give you this column is green column on the top row which is not the case so it's always the first column turns into the first row and now you can also picture this the other way so you can think about this as being the first row turning into the first column and the second row turning into the second column and so on so now that you know about the transpose operation week actually come back and revisit this special type of matrix called a symmetric matrix now remember I said that a symmetric matrix is mirrored across the diagonal now it turns out that what is special about a symmetric matrix is that the matrix equals its transpose this is a really unique property most matrices are not like this so only special symmetric matrices follow this pattern have this definition so we see that the first row is the same thing as the first column then the second row is the same thing as the second column and so on you can do this always you want so the here's the third column that's the same thing as the third row so the definition of a symmetric matrix is a matrix equaling its transpose so let's begin exploring the transpose operation with a vector so I'm just gonna define a vector exactly how we've been defining it so far let's say I don't know 2 3 and how about -1 so I'm gonna print out this vector and then I want to print out the transpose so to get the transpose we can actually put a dot T at the end of the vector like this so this is called a a method on this object but you don't have to worry about the fancy coding terminology so we just print the name of the vector or matrix and then a dot and then a t4 transpose so let's see what this looks like hmm well this might not look like what you would have expected it to look like so it started off as a row vector so you might have expected this to turn into a column vector now the issue here is although we are looking at this thing and interpreting it to be a row vector internally Python the numpy array of Python doesn't actually encode the orientation so python doesn't see that this is a row vector or a column vector it just sees it as a vector without any particular orientation so therefore the transpose actually does nothing so what we need to do in this case is add an optional second input we specify that the number of dimensions in this vector is 2 and now oops sorry it's not end omits nd min and I have to get the spelling right on these things so now and actually I'm gonna put in an empty space here just to give us a little more room so now we see that python is representing us as a row vector and this the transpose as a column vector then you can also see now that there are double square brackets and this is similar to the matrix formatting that I introduce you to previously so we have one outer set of square brackets for the entire matrix and then we have one like you call these inner square brackets for each individual row so this vector here is a row vector so it only has one set of square brackets here to indicate a row vector and here we have a column vector which actually means that there are three rows so that's why there's this proliferation of square brackets here again this stuff is a little bit confusing when you're first getting used to it but when you work with Python more and more then these kinds of you know little weird idiosyncratic things will start making more and more sense okay so that was for a vector now I want to show you the transpose of a matrix so let's define a matrix we'll do I'll set this to be random numbers so numpy dot random dot R and N and let's make a three by three matrix so let's see what this thing looks like so we have a three by three matrix of all random numbers so this is a function R and N and the N here means to create normally distributed numbers so these are numbers that have a average value and expected average value of zero and a standard deviation of one and basically that just means that you are highly likely to get random numbers that are close to zero and the further away from zero you gets like these numbers the less likely they are to be observed so we can see here is let's see here we don't even get a single number larger than one we run it again here we 2.8 so you can get these larger numbers relatively further away from 0 but they're just less likely to occur okay so that's a 3x3 random matrix but for you know doing these operations we want to be able to look at the numbers pretty easily and see whether the transpose really did what we expected it to do so I'm going to use a function called round which I believe you made of you might have already discovered earlier in the course when you had to have this assignment of creating our rounded numbers to the closest integer so now the issue is that these numbers are also close to 0 that when we round them we get basically just zeros and ones and minus one so we might get it to possibly even a three that could happen oh it's not super likely so I'm gonna do basically just a little trick I'm gonna say ten times these random numbers and then round them and all that's doing is just giving us a broader range it just spreads out the numbers a little bit more okay so that's our matrix of random integers and then here we get the matrix transposed and then again the main point is just to confirm that in the original matrix the first row corresponds to the first column of the transpose matrix and so on you could look at this many different ways so the first column of this matrix turns out to be the first row of this matrix alright so that is about the transpose operation now it's time for the assignments I'm sure you've been looking forward to this all video now the principle here is the same as with the assignment in the previous video so we are going to be you well we actually you you are going to be using Python as a tool to help you understand concepts in mathematics in particular you are going to use Python to discover a question that is about the transpose we want to know does the let's see the question is what happens when we transpose twice so we want to take matrix it can be one of these matrices or a vector any any vector or matrix you can create a new one transpose it and then transpose it again and what you should do is create two variables so you're gonna need let's see M is going to be the matrix and then maybe you have another variable called MT which will be the transpose and then you want to transpose this matrix so maybe you want to call that matrix M TT 4 matrix transpose transpose and the question is how does the transpose here the double transpose relate to this matrix and to this matrix so that's what you want to discover basically what happens when you transpose a matrix twice or a vector same thing okay and then the second thing you want to do here is confirm that the transpose operation works on non-square sometimes also called rectangular matrices now I already showed you an example of this in the slides but it's nice to see it in practice in code so basically apply the transpose operation but instead of using a square matrix apply this on a non square matrix at this point in the course you have learned that vector and matrix addition and subtraction is pretty straightforward it works the way you are expected to and multiplication by a scalar so scalar vector multiplication and scalar matrix multiplication is also pretty straightforward now we get to matrix multiplication so that's multiplying two matrices by each other now this turns out to be quite a bit more complicated and in fact it's beyond the scope of this short course to explain the mechanics of matrix multiplication so instead what I'm going to do in this video is teach you the rule for knowing when two matrices actually can be multiplied because it turns out that not all matrices can multiply each other so that's also unlike with normal numbers ok so before we even get to that rule we need to learn about a little bit of term and that's not really how this shows how special matrix multiplication really is so if a and B are two matrices and we try to multiply them like this then we describe this as saying a matrix a left multiplies matrix B or you can say matrix a pre multiplies matrix B conversely you can flip it around and say be right multiplies matrix a or be post multiplies matrix a now the reason why we need to make this distinction here with the terminology is because matrix a times B is not the same as matrix B times matrix a so if we would put B on the Left we are likely to get a different result so that's also it seems really weird coming from normal arithmetic because you know two times four is the same thing as 4 times 2 but with matrices it doesn't quite work like that ok so this is about the terminology and now let's get to the rule the rule of when matrix multiplication is even valid to determine whether two matrices even can be multiplied what you have to do is line them up like this so this is a visual representation and abstracted representation of a matrix this is a different matrix and then we write down their sizes so this would be an M by n matrix so M rows and n columns and this is an N by K matrix so now there's n rows and K columns now you look at these two I call these the inner dimensions they're the inner dimensions just the way that they're laid out here when you line up these four numbers here so rows columns rows columns now if these two inner dimensions match that means if the number of columns in the first matrix is the same as the number of rows in the second Matrix then these two matrices can be multiplied if these were not the same number let's say this was well I'll show you examples on the next slide but if these two sizes are not the same number then these two matrices cannot be multiplied okay so this tells you if the multiplication is valid and then the size of the product matrix which you already see here corresponds to the outer dimension so an M by K this is going to be an M by K matrix again make two matrices can multiply each other only if their inner dimensions match when you line them up when you line up all the sizes the four numbers of the sizes here so rows columns rows columns so these two numbers have to match and then if they match the product the size of the product matrix is given by the outer dimensions here okay let's see a few examples so here is a matrix a it is 5 by 2 so five rows and two columns and here's our matrix B so the question is can we multiply these two matrices is that a valid operation and the answer is yes because the inner sizes match and so the resulting product matrix is going to be 5 by 7 here's another example are these 2 so now imagine these are like the same matrices I just swapped the order and now all the sudden these matrices can no longer be multiplied because the inner dimensions do not match again that's kind of a weird thing to think about that matrix a times matrix B is valid but the exact same matrices just swapping their order is no longer a valid multiplication here's another example so here the matrix product is not defined is not a valid operation however one thing we could do here is actually transpose this first matrix now remember when we transpose a matrix the rows turn into columns and the columns turn into rows so that means that you know matrix C is 5 by 7 but matrix C transpose actually becomes a 7 by 5 matrix because we've swapped the rows in the columns so now this actually becomes a valid matrix multiplication so with this in mind I want to get back very quickly to the notation of the dot product so you remember that I introduced the vector dot product is having this notation and now we can briefly understand why that is the case so we imagine that we start off with 2 column vector and we transpose the first column vector and that gives us a row vector over here and a column vector over here so now the inner dimensions match and the outer dimensions gives us the size of the resulting product which is just going to be well one by one but it ends up just being a single number here by the way you can see that in the beginning of the video I'm restarting Python here online and that's because as I mentioned in the beginning of this course when you if you're using Jupiter org slash try this server will basically kick you off after I think it's 15 minutes so you know I'd I'd wait a few minutes between recording videos and then I do the slides so it ends up that I have to reboot the jupiter kernel here so that's why i'm always starting from scratch okay anyway so what I want to do now is just create a few matrices and illustrate one of the concepts I showed just a moment to go in the slide so I'm gonna create a random matrix o R and and let's make this four by five and now actually just gonna copy and paste here change that to two and yes so these are going to be two four by five matrices so now my question for you is can I multiply matrix 1 by matrix 2 the answer is no not not directly because when we line up their sizes their sizes end up being 4 by 5 and the second one is also 4 by 5 so the inner dimensions do not match however we could actually transpose one of these matrices and that would allow us to multiply them so that's what I want to do here now how do we actually multiply two matrices there's two ways we can do this there's a numpy function for it so it's numpy dot matt mole sort of matrix multiplication and then we input the first matrix that we want to multiply and then the second and now we can confirm that this actually doesn't work that we're getting an error here and we can see that python is giving us an error here the sizes and the size of the two matrices you see K and K have to match and the resulting product size so this says size four is different from five so we already know how to transpose a matrix here so we do dot T and you know whatever these numbers mean they're all random numbers so we can't exactly interpret it but we do see that we transpose the second matrix so this was four by five and then this matrix becomes five by four so now the resulting product matrix is going to be four by four and we can confirm that here okay and then I'm gonna put in a space in here and then I just want to show you the other way of multiplying matrices now it turns out that with an asterisks that that is multiplication for regular numbers but not for matrices so you can use either the function numpy dot map mul or you can use the @ symbol here so then we use the @ symbol I have to transpose this and from quick visual inspection it certainly seems that these two matrices are the same and what I want to do now is just show you one of several ways to illustrate that two expressions are the same in Python so I want to show that this produces the same output as this and so one way we can do that is by subtracting them and what would you expect the difference between these two matrices to be if they are the same you guessed it we expect the difference to be zero in particular we expect a matrix of all zeros which is what we get so we subtract this matrix from this matrix our actually we're subtracting yeah this matrix from this matrix and the result is all zeros and that tells us that both of these methods are the same they produce the same results all right so that is about matrix multiplication so now I want to give you your homework problem so the assignment this one's gonna be fun I mean they're all fun but you know this one is also fun so your assignment is to create a 3x3 matrix of integers and again it's convenient to make these matrices be integers just because they're a little bit easier to look at and quickly interpret you know as opposed to these where you really have to sort of takes a little bit more effort to go back and forth and compare these two matrices so you want to create a 3x3 matrix of integers and then you want to multiply that matrix by first the identity matrix identity and of course that has to be the 3 by 3 identity matrix and then the zeros matrix and then you want to multiply by that matrix is transpose transpose so this is gonna be pretty interesting um here you will discover an interesting feature of the identity matrix and what it means to multiply matrix by the identity matrix you can probably guess what's going to happen when you multiply this matrix by the zeros matrix but it's good to confirm and this will be interesting you are going to discover an interesting property and what I would like you to notice here is that the matrix product is going to be symmetric so keep your eyes out for symmetry even when this matrix is not symmetric here's a pretty simple equation how would you solve for x here well of course what you would do is divide both sides of the equation by 3 and that gets rid of the 3 over here because 3 divided by 3 equals 1 and 1 is the multiplicative identity because any number times 1 equals 1 so then we get x equals 1/3 you can also write that down this way instead of dividing by 3 we are multiplying by 3 to the power of -1 and when you have a number that's taken to a negative power that just means you take the reciprocal so 3 to the power of minus 1 is the same thing as 1/3 or 1/3 ok so that gives you this and then we see that X equals one-third or x equals 3 ^ -1 now we you know you want to be able to do something like this in linear algebra you want to be able to divide a matrix by another matrix but it turns out that this doesn't work there is no such thing as matrix division that operation does not exist in linear algebra so we basically leverage this that I mentioned in the previous slide so sum divided by 3 is the same thing as multiplying by 3 to the power of -1 so conceptually what you want to do with matrix division is actually implemented by something called the matrix inverse so this would be at matrix s times the inverse of matrix R that's what this is called the matrix inverse of R and we put a minus 1 in the numerator and then it's you know it's sort of the equivalent of this now why do we need something like a matrix inverse well let's imagine we have an equation like this where this is a matrix a is a matrix X is a vector of unknowns that's what we want to solve for and B is another vector of numbers that we already know so what we need to do to solve for X is isolate X on the left hand side of the equation now we cannot divide both sides of the equation by a because matrix division is not a real operation it doesn't actually exist but what we can do is left multiply or pre multiply both sides of this equation by the inverse of a so then we get something like this so now here we have a inverse times a and now it turns out that what is special about a inverse is that it's a matrix that multiplies the original matrix a to produce the identity matrix and you discovered in the previous video in the exercise that when you multiply any other matrix or any vector by the identity matrix you get exactly that same matrix or vector just like the number 1 it's very similar to the number 1 so we can just get rid of this identity matrix over here so a inverse times a equals the identity matrix and that allows us to solve for X now this equation right here is the basis of a lot of things in statistics like linear least squares general linear modeling regression analyses so this equation is really important in statistics and in machine learning and deep learning and artificial intelligence now the way to compute a inverse is actually a little bit complicated there's a few different algorithms for computing it and explaining those algorithms is beyond the scope of this short course however of course in practice we do it using a Python function so let's have a look at that so I'm going to start by creating a random numbers matrix I happen to like random numbers matrices I use them all the time so let's make this 4x4 random matrix and call it M maybe I'll call it a just for a little bit of variety so now I'm gonna create a in that's going to be the inverse of a and that will be numpy lyn alg so this is a different sub module inside the numpy library so num pile in algae dots in of a so now we have these two matrices I'm gonna print them out so print a and then I'll print a space just for visibility and then print a end now the thing is it's really hard to look at it inverse particularly for you know a matrix like this and and see how the inverse relates to the original matrix you know it's not clearly any relationship in fact the relationship is embedded across all the rows and all the columns so instead what I'm going to do to illustrate to you that a inverse really is the inverse of matrix a is multiply them so I'm going to say let's call this a in a and that is going to be equal to matrix a times matrix multiplication times a inverse and then I'm going to print out let's see prints another little space here and then prints a in a and now we see that this doesn't immediately look like the identity matrix but it actually is so you see the diagonal elements contain ones and all the off diagonal elements contain this looks like it says minus two but actually it's minus two and then there's this II - 17 this is the computer way of expressing 10 to the minus 17 so in fact this number is zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero two so this is basically zero at the computer precision this is computer rounding error here so to get a better sense of this what I'm gonna do is visualize these three matrices using the imshow function that I introduced you to in matplotlib towards the beginning of the course so and I'm gonna do this a slightly different way I want to create three matrices and I want them to be I want the three images to be all next to each other in a row so I'm gonna show you a slightly more advanced way of setting up multiple plots in matplotlib which I haven't yet introduced you to so I'm gonna write fig comma x equals PLT dot subplots that's what it's called when we have multiple plots in the same layout and I'm gonna make this a one by three geometry and then it's often useful to specify the figure size so let's see so we write x zero so this is going to be what we're plotting in the first axis I am show em and then X 1 is gonna be I am show oops not M hey I meant to say a and then here is gonna be a in and then X two is gonna be I am show a inverse times a and then we can make a plot this oops plot not to find hopes right I didn't import the matplotlib import matplotlib dot pi plot as PLT and computers are sticklers for spelling so you have to make sure you're spelling everything correctly here all right let's try this again brilliant here we see our matrix a here is a inverse and here is a inverse times a and you can see this looks like the identity matrix it's all ones on a diagonal and zeros on the off diagonal okay now I'm just gonna put in some titles here to make this a little bit cleaner so X 0 dot set title so this is matrix a and then we have this set title this is matrix a and I'm gonna do something interesting here this is a little bit of latex coating so let me just start it like this so you see this is not anything particularly special it doesn't even look very nice but watch what happens when I put a dollar sign surrounding this special code so this caret symbol and then in curly brackets the minus 1 all of a sudden it looks quite different and the caret symbol indicates super scripting and everything that's inside this curly brackets goes into the superscript above a this is called lay tech coding I can write this out for you latex it looks like this latex coding and it's just a special way of getting Python to interpret non-standard letters all right so and then the last one is going to be a inverse times a so oops there's a typo here I specified this to be 1 so it actually updated this title here that should be 2 now we get the third subplot which is index value 2 so what comes next of course it's the exercise for this video it's the assignment here is your assignment now up here I said it's really difficult to see the relationship between the matrix and its inverse you know you just look at the elements it's it's hard to see how they match and they don't really match element by element and you also see that in these images here so it you know it's not obvious that this matrix is the inverse of this one you really have to see them multiplied but there is actually an exception and that is for a diagonal matrix so there's an interesting property of a diagonal matrix in the inverse of a diagonal matrix and that is what you are going to discover in this assignment so what you want to do is let's see you start by creating a a vector create a vector let's make it three by three how about that three by three vector and then you want to create a diagonal matrix from that vector so you are just going to specify a vector oops this was eight I mean say three dimensional vector so you're gonna create a three dimensional vector and you know a really simple example is just one two three something like that and then you want to create a diagonal matrix where the diagonal elements of this matrix come from the vector that you specify now there's a function in Python numpy that will create a diagonal matrix when you input a vector I'm not going to tell you what that function is because I want you to figure it out on your own you have two ways to figure out what that function is one is you can try playing around with different terms until you think you find something that works or two you just look around on the Internet I think it will take you yeah no more than 30 seconds to figure out what the function is on from the internet okay once you've created that diagonal matrix then you compute its inverse and then you want to print out both of those matrices like this and you will discover the interesting property of the inverse of a diagonal matrix so you can pay attention to the elements of the diagonal of the inverse and the elements of the diagonal from the original diagonal matrix okay that's one thing and now there's a second part of this assignment which is that it turns out that the matrix inverse is defined only for square matrices so only for matrices that have the same number of rows as columns and so to illustrate this to yourself what you want to do is create a basically any non square matrix you can make it two by three create a two by three matrix or you know whatever size you want and try to compute its inverse and you will get an error message and you can see what that error message tells you
Info
Channel: Mike X Cohen
Views: 15,913
Rating: undefined out of 5
Keywords:
Id: JSjWltL9-7M
Channel Id: undefined
Length: 111min 33sec (6693 seconds)
Published: Sun Jun 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.