Transcript for:
Essential Numpy Library Guide

well hello internet and welcome to my numpy tutorial in this one video we're going to review the vast majority of the numpy library using numerous examples and if you don't know numpy is an absolutely amazing scientific computing library that's used by numerous other python data science libraries which i will review in upcoming videos and it contains many mathematical ray string functions and so many functions and they're very very useful to do things like work with linear algebra statistics simulation data science machine learning and so much more and i have a lot to do so let's get into it okay so i'm going to be using anaconda and i have a link in the description on how to install this if you want to use the same thing i'm using specifically i'm going to be using the jupiter notebook and open it up you just click on launch and i went and created a file inside here very simple and you can see all the different things that i'm going to be covering in this tutorial so a whole bunch of different things what i'm going to do first however is i'm going to import numpy as np so i can just keep that nice and short i'm also going to i'm not going to do a lot of plotting in this tutorial but i will do a little bit of it because there's so much to cover with numpy that i want to focus on it but i will play around with some plotting a little bit and that's what that library is for i'm also going to import a random function and you're going to be able to get all this code there's going to be a link in the description all right so the first thing i'm going to do is i'm going to create a regular python list so let's just call it list one and i'm just going to throw some random values inside of it here now if i wanted to create a numpy one dimensional meaning one axis array object i could come in and let's just call this np array just so you can tell the difference i could just come in and go np array and take that list that i created there's numerous different ways to do this i could also define the data type here if i would like so let's say that i wanted this to be a byte i could do so and this is going to accommodate the values one through five so that's perfectly fine i could then come in and if we wanted to display what i just put together here just type in our new array name and hit control and return and you can see there it is i could also create a multi-dimensional list and i'm going to do it in the same way even though this is not the way that i normally create multi-dimensional lists i'm going to do it this way just to show you so in this situation i'm going to create our multi-dimensional list and you can see that i'm just breaking everything apart into brackets like that see there's multiple different brackets and then we can say four five and six and seven eight and nine okay so there's a multi-dimensional list now i'm gonna turn it into a numpy array numpy and let's just call this m array one is equal to and again you're just gonna go call our array function and pass our list inside of it all right so let's start working with some of these guys give us a little bit more space you're also going to be able to create arrays by defining start values stop values as well as step so let's say that i wanted to come in here and create a range of values from 1 up to 10 and you can see i generated all those right there and i could also come in and do a step so let's just change that to two and there you can see it's stepped up through those and of course i can change those to whatever i'd like i'm also going to be able to go and generate floats again with a defined start and end and then a number of values so let's say i go np lin space and let's go from zero up to five seven values and there you can see those are generated for us and you can also see here if i change this from seven to five how they change each time that i work with them depending upon how many values i'm looking for and i'll show you more examples we're also going to be able to generate arrays that are just filled up with zeros so let's just go mp and zeros and four and you can see generated those guys for us i could also generate a multi-dimensional array that is filled up with zeros again in much the same way so zeros and then inside of it like if i wanted this to have two rows and three columns i would throw a tuple inside of here like this and you can see it generated that for us i'm also going to be able to fill arrays with one so let's just change this from zeros to ones and there you can see that changed if i wanted to get the number of items that i have inside of an array i just call size you see that works out to nine and of course i can go and generate arrays using default values so let's just call this np array2 it's equal to np and array and then you just create your multi-dimensional array inside here you can also get the data type for your arrays so let's get two just by calling d type on that array you can see these are integers 64-bit integers and you can see here is a list of numerous different types you can use booleans characters shorts integers longs floats doubles and you can see ranges of values depending upon which uh the data types that you choose to use so this is all big one and that's what we're using whenever we go and create an array with a default data type if you wanted to generate random arrays just go mp and random just make sure you import this guy right here that random library and i'm going to generate random ants so random int and let's say i go 10 to 50 and 5. you can see it generated all those again it's going to work in much the same way to generate random matrices so let's say i want to have a 2 by 3 array instead of 5 i can just come in and change this to size is equal to and assign a tuple so let's make it two by three and there you can see generated it and they are random another thing that's going to be useful is very often you'll want to know specifics about a function that you're working with you can just type in like let's say we wanted to find out more about the random function or randomint function just type in the function name with a question mark ctrl enter and you can see it's going to open up and give you a ton of information about what all the different parameters are for it and examples and so much more so there's a quick intro into arrays and now i want to talk about slicing and indexes now you're going to be able to change values at specific indexes so let's just go and get mp multi-dimensional array 1 and let's say we want to change the upper left hand corner value we can do it this way and let's say we want to change that to 2 you're also going to be able to do it in a slightly different way you could also come in here like that and go item set and let's change a different one just so you can see things are different so changes to 1 and assign a value of 1 to it can then come in and mp array and there you can see change the first value to 2 and change this one to 1 right there let's get rid of those changes though keep everything the way that it was you're also going to be able to get to the shape or overall size of arrays just by ending this with shape and there you can see and that's just a tuple that you can get individual values from it just like you do with any other tuple you're also going to be able to get values by index so let's say that i would like to get the top row and second column and you can see that's set for one you're also going to be able to get the same thing by going item and then throwing this throwing a tuple to this so instead change this from brackets to parentheses there i did actually execute it to different here i'll do this just to make sure okay there you go you could also get specific indices so let's say we go np and take and the array that we're working with and then pass inside of brackets zero three and six you can see that comes back it's two four and seven why don't i go and print this guy out here just so you can see what it looks like and you're gonna be able to use print inside of here just like you do anywhere else so go like this maybe do a new line and then there we are and now you'll be able to see all the different parts and how i'm pulling that data out we're also going to be able to replace provided index values with new values just by using put so we can go put throw in our array we're working with and get our index values here so we'll go 0 3 and 6 and our new values that we're going to throw inside of there let's throw 10 inside of there just so they really stand out and then show the new array and you can see how we changed those and if you're wondering how this works out it says this would be 0 1 2 this one right here three four five six so we're changing this column worth of data two four and seven as you can see right there you're also going to be able to slice up these arrays so let's say let's go and throw this inside of here mp array let's use the one-dimensional and let's say that i would want to get the starting of the array by leaving it blank that's what that's going to do so leaving this area right here with nothing that's going to start at the beginning beginning and i'm going to go through the fifth and then i'm going to take two steps and you can see how we were able to pull those values out you're also going to be able to come in with like a multi-dimensional array and let's say we want to get the second value from each row let's come in and do a colon with a comma and one and there you got it and you can also flip a raise so let's go and get this guy right here again and to flip it all you need to do is go colon colon and negative one and it flips it you can do things like let's say that we wanted to get even values let's go and define evens is equal to np m array1 and just define our condition here so we'll go like this and and we will do modulus 2 and see if that is equal to 0 just make sure you use square brackets here no parentheses okay so now that we've defined our conditions we can just say evens and you can see it grabbed only the even values out of there so pretty neat stuff you do this with so many conditions here i'll show you a couple more conditions let's say i wanted to get values from that array that are greater than five ikey command let's just go and get that array and just go get the array once again and say greater than five there we are we're also going to be able to use logical operators let's so let's say i want to get values that are greater than five and less than nine i just define the conditions inside of the brackets inside of parentheses whenever i'm using multiple so i'll say i want to define that array and i said greater than five and just a single and go get the array again and i said less than nine and there you go got those as well we can do also use an or like this and let's say i want to get values greater than 5 or values equal to 10 greater than 5 or values equal to 10. and i got them and the final thing here is i'm going to show i'm going to show you so much more but finally if you wanted to get only unique values from it throw that in there with unique and you get all the unique values all right so there you go whole bunch of stuff now i want to talk about reshaping arrays okay so let's say we go and we get our multi-dimensional list and we want to reshape it into a nine-item single axis array that's how you do it with reshape and then we can also come in and resize it to say two by five you're also going to be able to resize just by taking that array and then defining you want it to be a two by five array instead you're also going to be able to transpose the axes so let's take this and let's just say transpose and there we are going to be able to swap axes let's just keep this going just by saying swap axes and we'll say zero and one because that's all we got whoops forgot to put the dot inside of there there we are you can also flatten the array just by calling flatten there it is another thing we could do is we could flatten in different orders so let's say we wanted to flatten in column order just go like this with an f there we are we would also be able to sort rows so let's go in and sort them just by calling sort and we will sort with axis 1 and there they're sorted and also we would be able to come in and sort the columns just by changing this from axis 1 to axis two and there you can see the changes all right so good stuff and up next i'm going to talk about stacking and splitting arrays all right another thing i'd like to do here is i'm going to generate some random arrays so i'm going to call this ssar1 and i'm going to call our random randomint function and i will say 10 and i want my size to be two by two okay so we got that set up so just copy this and print this out so you can see and you can also see it's random and you can see the values will go from zero up to but not include ten that's what this guy's for let's go and create another one on top of that and let's just call this two because we're going to be stacking and splitting away and let's go and print out this information so that you'll be able to see the arrays and how they are changing so just throw this inside here and our array and then grab this and throw this down here and then change this to a two and this to a two there we are and now as we change things we'll be able to see how the arrays are being changed so what i want to do here first is i want to stack array two under array one to do that you just go and call v stack and then list whatever array you want on top so if you want your array one on top well that's what you put on top and then if you want to write two on the bottom no that's what you put on the bottom and there you can see how they would work that out could also stack horizontally so we can come in and just change this to h stack and leave everything else the same and there they are horizontally stacked let's say you'd like to delete the second row on each of your different arrays let's go and store this in a new array so i'll call that three you just call delete and then the row that you want to del or the array you want to work with and then if you want to get rid of the second row well that would be one because it's zero indexed and let's go and do the same thing for our second array here also uh let's go and store this in four and change this to two and run it and you can see how we deleted those we're also going to be able to combine arrays so let's get rid of this part right here and there's so many there's a bunch of different ways of doing things like there's a concatenate function i'm not going to bother showing you that but it's just you know there's so many functions so and they'll do the same thing so this is just combining the arrays so i'm going to call column stack and i'm going to use the third and the fourth one that we just created and you can see those stacked up you can also stack in a two-dimensional array just with row stack instead of column stack so change that to rows stack and there they are and also you could come in here and do even crazier stuff so let's go and create another array here call this five and it's just going to generate random one as well so let's just go and copy this as well so here's our random array and let's change this to 10 instead so we'll have two rows and 10 columns and the reason i have that set up and let's go and print this out also just so you can see how it changes all right and then let's go and split it into five arrays taking from both the arrays in our multi-dimensional array and this is done with horizontal split and then i just pass in the array that i want to work with and five and there you can see the original and how i went and broke those pieces up into five different arrays instead of just one big giant one and you can even do things like let's say i wanted to split after the seconds and the fourth column so let's keep this like this and then let's just change this to a tuple second and fourth and you can see how they broke those up so second fourth and then all the rest all right so a whole bunch of different ways that we can stack and split arrays and now i want to talk about a bunch of different ways we can copy okay so let's just call this copy array just to keep my naming conventions the same they sort of make sense for the tutorial they might not might not make sense for the real world let's go and create a random array here so there we are and two by two and values from zero to nine seems pretty good all right so what we wanna do here is if you would go and create another array like this and you made it equal to the first one that you created just know that both of these variables are going to be pointing at exactly the same array so if i would come in and go cp array 1 get the upper left hand corner and i would make that into two and i come in here and say that i want to change or output both of these just to prove it i can do that here and there you can see this is set for two and these are the same array it's pointing at the same a different name same array if i instead wanted to make changes that wouldn't affect the original what i can do is let's create another array here and call it three well i can go and copy from our first array again just by labeling this as a view and now what it can do is come in and let's say that i wanted to flatten it well i can go you know something really extreme well you could do that and there you see i did flatten it however if i come in and even though i copied it from this first array if i come in and print this you're going to see that nothing changed with it and if you really want to copy from an array and create a brand new array you can just go cp no you don't need to do that obviously so let's just create a new array here and to copy and create a new array you just end this with copy and there you go all right so a whole bunch of different ways you can copy arrays and work with them and just some information to know about what happens behind the scenes whenever you do so and now i want to show you a whole host of the basic math functions that are available to you and things you can do math wise and then i'll get into specific things like linear algebra and statistics all right so i'm going to create two arrays here to work with so one two and three and four and then let's go and create another one after that let's change this to four and to differentiate them let's have everything in here be a even value right like that well i can come in and add these together so we can go array three plus array four you can see our answers we're also going to be able to subtract them we're also going to be able to multiply them we're also going to be able to divide them and you can see all those changes let's go and create another random value inside of here a random array i mean call that array 5 going to call random again let's get a random integer and up to 100 so that would be zero up to but not including a hundred and let's go and define the size to be equal to two by three i can come in and show you what this looks like there we are there's our random array actually why don't i make this be six and let's go and generate another one and have it be array five and it's just going to be a one-dimensional array and let's give this a total size of four and if you just want to see what that is obviously it's random so it's going to change every single time you're also going to be able to create random floating point values or random floating point arrays that is so just go around like that let's say i want to create four of them there's the four and i could also go and get a random value from an array just by going random and choice and then the array you want to pull a value from so let's say i want to get it from three well just grabbed four for me okay and each time i do it it should yeah see it's different you're also going to be able to sum values inside of arrays so let's say that i have array 3 and i want to sum up everything inside of it can do so i could also come in and sum columns so let's go and print out array six so you can see what it looks like and then i can go and get array six and sum the columns for it by passing in axes is equal to zero well i forgot to put the sum part in there there we are and there you can see how that works out we would also be able to do a cumulative sum of rows by just changing this to cumulative sum and let's use axis 1 in this situation and that's how those work we would be able to get the minimum of each row and to do the rows you would do axis 0 or no to get the rows you would use axis one that is so there's that and then if you wanted to get the columns well you could use min why don't i show you how to use max instead and then you can go and play with it yourself and see how that this is going to work and there those go i want to print out the arrays again just so you can see them so array 3 and array 4 there they go you can also add individual numbers to arrays just by calling add and the array that you want to work with so let's say i wanted array three and i wanted to add five there's what would happen if i did that to each of those values we're also going to be able to add arrays together in much the same way so just change this to array 4 for example and there's what happens if you add them together there's also subtract so we'll just change this to subtract and we're also going to be able to multiply and divide so multiply and divide and there we are let's do some other cool stuff let's create a couple more multi-dimensional arrays let's redefine array five so let's get rid of this part right here and instead go np array and we'll have it be one and two and three and four and then do the same thing or a similar thing anyway with our array six just change this to two and four and six and nine so let's say we wanted to divide the elements in the first array by the second array and let's go and get a remainder from doing that just go array six and array five and there you are i mean there's there's never any supply of things you can do um let's say we wanted to return values in the first array to powers defined in the second array i mean it never ends so we'll say power array six array five there you can see getting big we could also do square roots so let's say we want to get square root of everything inside of array three like that like that we could also get cube root so just change that to cb there we are we can get absolute values let's change this to absolute and then we'll throw inside of here two negative values and two there we are we can get a exponential of all elements inside of an array just by going exp and which array do i want to do this with let's say that i'll just use array three again there you go you can see all those different arrays and what they did of course there's numerous different log functions so we have our regular log we have log 2 whoops there we are and log to the power 10 and so forth and so on another thing that's very useful is you can get the greatest common divisor just by passing in a array of values so let's get rid of that and to do so you just go greatest common divisor and reduce and 9 and 12 and 15 you can get the lowest common multiple as well um well let's run this there you are it's three and the lowest common multiple is just lcm run it you can see that comes out to 180. you can round up you can round down you can round all around and so let's go and get to round down you call floor and of course you have to pass inside of this floats so let's pass in 1.2 and let's pass in 2.5 and you're also going to be able to get the ceiling around up however you want to call it there that is another thing you can do is let's say you wanted to generate six values and square them let's just call this the square array just go a range define your range you want to work with and six and to the power of two and then let's say we wanted to get this done with the third or array three there we are and that works so really good stuff uh let's do a couple more here let's generate one more array let's go call it array seven and let's have it be a five by three array so we're going to use values from zero up to 99 except i want them to be five by three and let's go and print this out there it is now what i want to do here is i want to go and get the index for the max value per column so let's call this mc index and that would be array and arg max and that would be axis zero we can do mc index and you can see it got that max value and then we could also go and call to get the numbers that correspond to those indices so let's do max nums like this is going to be equal to and array 7 and then the indices and maximums and there we are so there's a whole bunch of different math functions that are built directly inside of numpy and before i continue covering even more i want to show you how to read information from files all right so i'm actually going to show you multiple different ways i'm going to show you a different library called pandas which you're going to see more of later on so we have pandas and i'll import this and it's going to be a very useful anytime you need to manipulate tabular data and it has a whole bunch of other different things okay so and i'm also going to go and get from numpy and show you the numpy way of reading files jen from text whoops make sure you spell that right jen from text okay so first off you're going to be pulling information from this guy right here it's ice cream sales it's a comma separated value list and that's what it looks like okay and i'm going to show you how to create them as well and read from them and just about do everything okay so let's say you wanted to read the table data from the comma separated value file that i just showed you and you want to convert it into a numpy array how would you do that sounds so confusing no it isn't so this is going to represent ice cream sales i'm going to use pandas here and csv and then you just pass in what you want to read from so it's called ice cream sales and comma separated values and then what you want to do is go and convert it into an uh numpy array to numpy and then we can just go ice cream sales whoops got an error because i didn't put an underscore there there you are so you can see that it imported all that information into a numpy array that we can work with and we will work with it a lot and now let me show you how to read data using numpy so i'm gonna call this ice cream sales and i'll show you how this differs in the next part of this specific tutorial so you go generate from text and ice cream sales and comma separated value just like you did before and then you just define the delimiter how that information is going to be how that information is going to be divided up and if you want to go and get rid of nan values inside of here we can go ice cream sales 2 is equal to and row and the nand values are going to be the part where it says temperature and sales at the top of the comma separated value list so we say np and we'll say is nan row and cycle through all of these rows inside of our ice cream sales and then we can say ice cream sales two and there you can see that we have those as well so and they're all divided up into separate arrays okay so that is how we were able to pull in information from multiple different files i'll show you how they differ here as we continue and up next i want to talk about some statistics functions so let's pass on linear algebra yeah that's going to be one of the biggest parts of this whole tutorial because there's so many things you can do with linear algebra so don't let me forget to cover it okay so let's go and create an array i'm going to call it s array 1 is equal to np and range one six and you can go and see what i did right there okay just generated values from one up two but not including six and let's say that i want to get the mean that's easy you just type in mean and then you pass in your array you have right here and there's your mean value you're also going to be able to get your median value there it is also you can also go and get your average value oops don't want this here and you're going to be able to calculate standard deviations let's go and do standard deviation and with this guy what i'm going to do is change this up a little bit throw an array inside of here let's maybe change this to four six three five and two if you watch my statistics tutorial you know what standard deviation is also going to be able to get variance let's use the same ones there we are another thing is you're going to be able to there's different versions there is nan median and nan mean and all that all they do is if they come across the nan value they just ignore it so nand standard deviation and nan variance that's what all those stand for do exactly the same things and just ignore nams and let's go and let's print out our ice cream sales so i'm going to say print ice cream sales okay so there's our ice cream sales and we're using the ones we got from pandas if you're wondering let's say i'd like to get the 50 percentile of that data i can go and get it and in some situations i'm going to cover exactly like the math a lot and sometimes i'm not whenever i get into the linear algebra i'm actually going to get a lot into the math and explaining what the functions actually do whenever we're dealing with statistics though i'm not going to do so you can see 62.5 394 and you can get that for any of the percentiles so if you really like math you will be very excited when i get the linear algebra because i'm going to be going and explaining literally what every function does piece by piece let's say we want to also get the first column and this like this and there it is our first column of data and of course you can go and get the second and there that is as well another thing let's do something a little bit more complicated something that's called the correlation coefficient and what that is is it's a measure of correlation between data so what i want to do is i want to correlate this ice cream sales data here this represents the temperature and this is the number of sales per month and in my statistics tutorial i did a lot of analysis to see if changes in temperatures increased or decreased ice cream sales okay so that's what that's about this is temperature average temperature this is the number of sales in a month so if i want to figure out if those two things are correlated or if they move in the same direction that's what it means meaning when one goes up the other goes up i just go correlation coefficient and i just throw in i see sales like that and then i can go zero and go and get the other array throw that in there change this to one run it and in the situations in which you get to one or get very close to one that means they're highly correlated so that means as temperatures increase yes more people buy ice cream as temperatures decrease yes less people buy ice cream and as an added benefit i'm going to show you how to go and calculate a regression line okay so this is how you calculate a regression line you get each value of x you subtract from it the mean and then you do that you or as you do that you do the same thing for the y you subtract the mean of y you sum these products up and then you divide by the sum of all the x's minus mean squared okay you want to know this in super detail watch my statistics tutorial but otherwise i'm going to show you exactly how we would calculate a regression line using all the stuff you just saw so let's say i want temperature mean i can go and get it so i'm going to say mean and i'm going to get my ice cream sales and comma zero so that's gonna give me my temperature mean now what i wanna do is get my sales mean and that is temperature mean would be x bar and sales mean is y bar that's what those things are and i just need to change this to 1. now what i want to do is go and get my numerator and this is sort of a little bit of an explanation of how extremely easy it is to create formulas using numpy so i'm going to go and sum these you can see this is the formula that i am recreating in numpy so i'm going to go numpy sum and i'm going to say i see sales and it's going to cycle through all of the x values and i'm going to subtract from that the temperature mean and then outside of here i'm going to multiply times the same guy right here well not the same one i'm going to throw that there i'm going to change this this is going to be the information on temperature or on sales i mean sorry about that so this is going to be not temp mean but sales mean all right so that's my numerator and my denominator is going to be the sum of and i'm going to call square i see sales o this guy right here to my temperature mean right there so just paste that inside of there there we are then i can calculate my slope this is a regression line and it's a linear equation so if you know how those work i'm guessing you probably do if you're watching a tutorial about about a mathematics library you probably are well aware of whatever a linear equation is so let's go numerator if you're not uh tell me because that would be interesting would actually i would look positively upon that all right so we'll say we want to calculate the y-intercept that's going to be the sales mean minus the slope times the temperature mean and we can go if we want to see what the y-intercept is there that is as long as i make a calculation error which i didn't that's 35.56 and then we can go and get this a regression line array just by saying ic sales colon whoops zero whoops this is a colon on a semicolon times the slope and you can do this on all the values so useful like that oops and then i need to actually show you the regression array like that and what i do wrong here oh y is not defined that's because it's supposed to be y underscore i like that and you can see it went and generated that regression line so just tons and tons and tons of awesome things whenever i start getting into the plot libraries and stuff they're going to be more example based with pretty pictures everywhere but i wanted to stay 100 focused on what numpy does really awesome in this tutorial and up next what i want to talk about is trig functions okay so what we're going to do here is i'm going to start off by generating an array that is going to be floats and what i want it to be is negative pi to positive pi and i'm going to do a plot here just so you can see what they look like okay so there's that array and now what i'm going to do is i want to plot this data out so i'm going to say plot and here is our array and i'm going to show you plots using different trig functions let's start off with sine so pass in our array and run it and you can see it plotted that out so pretty neat let's do the same thing with cosine and there it is and let's do the same thing with tangents okay and there that is and so pretty neat looking stuff and you're going to be able to change the axis and things like that but i'll get into that in a separate tutorial another thing we can do is we can get the arc sine which is going to provide of course the inverse so the arc sine is the inverse of the cosine so what i'm saying is if you have y equals cosine of x like that then x is going to be equal to the arc cosine of y all right just wanted to cover that and of course those are just going to be arc sine whoops make sure you put arc sine and i don't know i'll just throw one inside of there you're also going to be able to do r cosine and you're going to be able to do arc tangent and there is a ton of other ones for example here's an additional list of some other functions you can play with they work in exactly the same way and you probably want to know how to convert from radians to degrees you just go rad two degrees and let's go and get pi inside of there and you can see that works out to 180 and then likewise you're going to be able to go from degrees to radians and let's change this into 180 and you can see you got pi that way and then as a final example i'll show you how to get the hypotenuse if you know the width and height of a triangle let's just make it 10 and 10 just for the heck of it and there we are and of course this is the formula for generating hypotenuse okay so there we go and now we're going to get into a ton of examples as we talk about linear algebra functions so now i'm going to get into the biggest part of this tutorial which is going to be all the linear algebra functions and i'm going to be covering a lot about linear algebra in general i'll be making a linear algebra tutorial very soon to show you the power that it impedes or gives or provides to you we're going to need to get our linear algebra module i'll just put as and i'm just going to label it as la and we're going to be using these two well we'll go and use three different arrays first thing i'm going to cover however is how matrix multiplication works and i think you can see how everything lines up here with these different calculations using the dot product if you come down you're going to see the arrays that we're going to be working with so real easy to remember we're going to have 1 2 3 and 4 and then 2 4 6 and 9. and to go and actually create this matrix multiplication with a dot product all we're going to do is go np and dot and pass in your two arrays that you're working with so i'm going to have array 5 and array 6. if we execute that you can see that we are able to perform those calculations it's probably best i'm going to have a link to github with all this stuff in it in the description you can take a look at everything there if we want to be able to perform the inner product you can see exactly how that calculation is made well one other thing is you can actually perform the dot product using more than one array so i'm going to go la and it's called multi dot and then you can just go and pass in as many arrays as you'd like so ray 5 and array 6 and array 7 or array 8 i mean and you can see that that worked as well down here to perform the inner product we're going to say np inner and array 5 and array 6 and there is the calculation for that as well you're going to be able to calculate the tensor dot product i'm going to go and create another array here this is going to be called array 9 and like i said if you want me to explain exactly how these different formulas can be very useful make sure you select the or click on the notification bell because youtube has not been very good about informing my subscribers about my new videos that come out and i'll go three and four and break these into different arrays here five and six seven and eight and if you want to see what this looks like there's array 9 and that's how ray 9 is going to be structured and then i'm going to go and create array 10 as well and p array 1 and three and four and i'm going to define the data type for this to be object and now to go and calculate tensor dot pass in those two arrays and you can see the calculation that was made down there and of course this is the calculation up here for exactly how this is all processed einstein's summation is going to provide as it says there are many different ways to perform many different operations on multiple different arrays and what i'm going to do is i'm going to create some more arrays why not so np array and let's just keep this simple zero and one go and create another array call this array 12 and this is gonna be a little bit more complicated zero one two and three and four five six and seven now basically i'm gonna go and actually show you one of these so we'll go np einstein summation and then you have to use sort of characters in specific ways which i'll explain to you exactly how they're structured to go and actually perform numerous different calculations this is a function it can do a whole lot so array 11 and array 12. now basically this guy right here what you see i'm gonna put it outside of here so that you can see it and i can explain it piece by piece now basically the part that is on the left side of our arrow what this is saying is that we are going to be using one axis that's what that is for array 11 and two axes for array 12. and then after that what we are saying we want is a one dimensional array and what i j means is that we want to multiply array 11 and its individual single items by each column of array 12 and then you're going to sum those values and whenever you do that what you're going to get is 0 and then you're going to have 4 plus 5 plus 6 plus 7 as your answer if we go and make this calculation you can see that's exactly what we got here as our answer now there are so many more and i'm going to show you a whole bunch more let's say that you would want to come in and sum all the values in array 11. what would you do well i'm just going to go and do everything inside of here so i'm going to go we're going to be using that single axis and i can go and get rid of this part right here and just pass in array 11 run it and as you can see that is going to give us our sum of one because there's only one value inside of there okay you're also going to be able to go and perform the dot product let's go and actually print these arrays out again so i'm going to go and grab this i'm going to use array 3 and 4 and i'm going to show you what they look like let's get rid of that that can cause confusion so there's array 5 and 6. and you can see them down here one two three four two four six and nine oh whoops i went and did the wrong ones let's throw in um three and four so three and three and four and four okay so it's just one two three four and two four six and eight okay so those even values now if i want to calculate the dot product on those two again we're just gonna have i here and one axes again and then we pass in array three and four execute and you're going to see that the do you get our dot product here whoops i wouldn't made an error sorry about that i meant to have an i inside of here execute and you can see that we got our value of 60 and you can refer above to our dot product and exactly how that works up here kind of a whole bunch of things i'm trying to teach here all at one time you can also perform matrix multiplication let's use array five and six so ray five and six and again you're just going to type in inj and then j and k because we're working with multi-dimensional arrays here go a value of 114 and you can see those two arrays that we're working with there and refer to above where i talk about multiplication we're also going to be able to get a diagonal and to do that just change this to i i and let's just do it for ray 5 and if we come down inside of here you can see we got a value of 5 for array 5 for the diagonal that's right there we're also going to be able to easily transpose these so let's just flip this to j and i for array five and you can see we transpose those values from here to here meaning moving the row down into the column and then this row up here into the other column now there's a whole bunch of other things you could do but i'm just covering the best of what i use all the time here you're also going to be able to raise a matrix to the power of some value how we're going to do that again i'm going to use la and matrix power pass in our array i'm going to use array 5 and let's raise it to the power of 2 and you can see it did that for us and you can see exactly how those calculations are being made right here doing my best to move quickly but at the same time provide information here so you can look this up i'm just hoping this was going to work out as a really good cheat sheet you're also going to be able to calculate the kronecker product of two arrays and you can see the formula involved in doing exactly that just type in chrome and array 5 and array 6. there we are there's all those calculations for us you're also going to be able to compute eigenvalues which once again i'll get into in more detail in my linear algebra tutorial so i'll rate 5 and you can see our calculations down there and exactly what they're used for there's also another version this is going to this version is going to return eigenvectors and if you do not need them you can just type in eigenvalves like that and then you won't get them okay and what else let's also go and get to the vector norm which is just the square root of the sum of all the values to the power of two very easy just type in norm pass in array 5 and you can see that calculation as well you're also going to be able to get the multiplicative inverse of a matrix just by going l a and inverse and let's go and get the inverse of array five and you can see how that works out for us we can get a condition number of the matrix again i'm gonna get into what all of these do in more detail later this is basically just for people that already understand linear algebra and you will understand linear algebra much better later on we're going to be talking about determinants a lot because they're very useful you do things like compute volume area you can use them to solve systems of equations which i'm going to show you here in a second and like it says here for a matrix to have an inverse its determinant must not equal zero and you can see exactly how we calculate the determinant let's go and create array 12 here first and p array and multi-dimensional ray here and three and four and to get our determinant we'll go l a dot determinant 4 array 12 and what this works out to be is this would just be like 1 times 4 minus 2 times 3 and that'll be equal to negative 2 and you can see if we do this calculation that's exactly what we get down here you can also get the determinant of a 3 by 3 matrix so what i'm going to do is go and get array 12 inverse is equal to la inverse for array 12 and get array 12 like this you can see how that works out and then we can come in and say get our dot product for array 12 and array 12 inverse and there it works out okay and finally what i want to do here with linear algebra is show you how you can solve systems of linear equations now i'm guessing you know how to solve linear equations so for example if you had 3x plus 5 is equal to 9x what we're going to do is solve for x in that situation which you can see that we did right here and if you have x plus 4y is equal to 10 and 6x plus 18y is equal to 42 what we want to do is isolate our x which is going to be give us a value of x is equal to 10 minus 4y transpose the new value of x inside of our equation and multiply through until we get a value for y which is going to be equal to 3 and that's how we're able to solve both to find the final value of x and the final value of y well it's very easy inside of numpy to do exactly the same thing so i'm going to create an array this will be array 13 and if i want to solve this exact problem in numpy i'll go and put in the 1 and the 4 so this is actually what we're working with the 1 is going to be the value of x here and the 4 is going to be that value right there and then what we are going to do next is handle the other equation that we have and that's going to work out to be 6 and 18. well the 6 comes from here and the 18 comes from right there now what we can do is we'll create another array with our answer so mp array and put in our answer and what is our answer well it's going to be 10 so we can just literally copy it from there paste it inside of here and 42 which is right there so we'll just paste it inside whoops missed it all right 42 there we got it okay so we provided both of those equations and it's going to solve it for us so we just have to say la and solve pass in array 13 and pass in array 14. and if we do you can see negative 2 and 3 exactly the answer we were expecting negative 2 and 3. all right and finally what i want to do here is return an identity matrix with a defined number of rows and columns and to do that you just say np and i and two and two and we can also define the data type which is going to be an integer and you can see this is an identity matrix where we have diagonally ones and everything else is zero all right so there is a rough overview of a lot you can do in regards to linear algebra inside of numpy and up next i'm going to show you how to save and load numpy objects okay so let's come in here and array 15 and array and this is going to be a multi-dimensional array so let's make it one two and three and four just to keep it nice and simple now if you would like to save this you just say np save and then type in whatever you want its name to be here so i'm going to call this rand array and then you type in what array you want to save if you want to load that saved data then just i'm going to create a new array here and you just go mp and load but you have to put in the full name so it's going to be rand array dot numpy like that and we can go ray 16 and you can see that it was able to go and get that back you're also going to be able to save as a comma separated value file to do that say mp save text and i'm going to call this rand csv and it is a comma separated value file and what do i want to save i want to save array 15. if you then want to reload that you just say np and load text and pass in rand csv and we can go and verify that worked whoops what'd i do wrong oh i forgot to put the csv at the end here very important and there you can see it was able to go and get it back again all right so cool stuff and now what i need to do is i am going to show you some financial functions but i need to go and load in the financial library so i'm going to show you how to do that okay so whenever you start start up anaconda what you're going to do you're going to see a terminal window or a command prompt if you're in windows that also opens inside of there you can use that to load different modules and libraries and a whole bunch of other different things all you need to do is first make sure pip's installed so type in conda install pip and it's going to go and do all this stuff and it might ask you if you want to proceed just click on y and it will and then to install the numpy financial libraries you're just going to type in pip install numpy dash not underscore dash financial and it will go and load all those in for you and that is also how you would load any other libraries that are unavailable so cool stuff to know all right so now that we loaded those let's go and use them so i'm going to say import numpy and this is an underscore financial and i'm going to load it as npf and i'm going to show you a couple there's a couple more that i'm not gonna get into but this should be a good introduction so let's say that i would like to compute the future value of a four hundred dollar investment every single month with an annual rate of return of eight percent and see how much money i would have after 10 years so future value and i said that i expect to get eight percent after 12 months that's what that stands for and i want to get the calculation after 10 years so that's going to be 10 12 and i am going to invest 400 every month so i'm going to say well 400 initial investment 400 monthly investment and you can see that's how much money i would have all right so pretty cool stuff you can do this really really easy and do some neat things all right let's do something else let's calculate the interest portion of a payment basically what i want to structure here is that i am getting a loan from a bank and i want to output a table of information so i'm going to get a loan of 3 000 and they are loaning the money to me at 9.25 per year but that's going to be compounded monthly and i want to basically chart out that this is going to be one year period that i want to pay this 3000 back so what i'm going to do here first is i'm going to define my period and np a range and 1 times 12 but this is zero indexed so i'm gonna go plus one all right then i'm going to define my principal so how much money am i going to be taking from the bank well three thousand dollars whoops there we are three thousand and the if i want to calculate the amount of money that goes to interest i can go in and go np f and interest payments and the interest payment is going to be 0.0925 and 12 for the uh entire year i will pass in my period i'll pass in 1 times 12 and the principal amount all right so that handles the interest amount and now i want to calculate what the actual full payment will be so and to do this is going to be the principal payment the payment on the not on the interest but on the three thousand dollars so npf and payment and again 0.0925 and this is going to be 12 for 12 months in a year and period and 1 times 12 and the principal amount which is 3 000 now what i can do is actually generate a table for this so i'm going to say for payment in period and i'm going to go index is equal to the payment which is just going to be numbers 1 through 12. that's all payment is going to output so i'll say -1 and i will get the principal is going to be the principal plus the principal payment and throw index inside of there and then i can output everything as a chart so i'm going to say print and i'll have the payment number show up on there and i'm also going to let's go and use np and round there's actually a couple different ways to round and you say to yourself of course there is all right so we'll go and get our payment here i'm going to round this to two decimal places so index and then i'm going to say two decimal places and let's also go and round again and this is going to be the interest amount ipmt and index and two and let's go and finally say mp round and output our principal and again this time two decimal places and don't forget to close this off with some quotes make sure i have them there i do and everything else there looks good let's check it and there you can see i went and created a nice little chart explaining the payment back of the three thousand dollar loan all right so it just never ends there's just always something new to do here so what else can we calculate let's say i would like to calculate the number of payments to pay off three thousand dollars if i decide that i i can pay a 150 per month with an interest rate of again i'll just keep this the same i'll put it at 9.25 well i'm going to round this so i'm going to say np round n p f and np er so i don't know the number of payments i'm just putting down my terms 0.925 and paying every month and i'm going to pay 150 dollars and three thousand dollars is the initial amount i'm gonna round this to two decimal places and if i do that you can see it comes out to 21.8 and uh why not do one more here so let's say that i want to calculate the net present value of cash flows of in the future i expect to get four five six and seven thousand dollars after an initial fifteen thousand dollar investment with at an eight percent interest rate okay getting a little bit into the weeds here but i thought this would be kind of interesting just to show you all the different little hidden secrets we got here okay so we have an initial investment so we're getting fifteen thousand dollars at eight percent we went and took a loan of fifteen thousand and then we expect to earn four thousand five thousand six thousand and seven thousand from that loan what is the net present value and you can see it comes out to twenty eight hundred and ninety eight all right some neat stuff and finish off the tutorial here i just want to show you some different comparison functions that are available to us and then you can go and experiment with all the stuff you learned about numpy all right so let's come in and let's create another array and i'm going to call this mp array and throw 2 and 3 inside of there and let's go and create another one let's call this array 2 and let's throw three and two inside of here so three and two and what we can do is we can come in here this is what these different functions are going to do for us is they are going to return different booleans based off of whether array 1 values are greater than less than less than or equal to and so forth versus array 2 values so i'm going to go mp greater and throw in our first array and our second array and you can see it comes back as false and true and you're also going to be able to do greater than or equal so there's that you can see that comes back as false and true also you're also going to be able to do less than in comparison between those different values see that comes back as true and false we're going to be able to do less than equal true and false you're also going to be able to check for non-equality so not equal and you can also check for equal all right so there you go i did my best to give you a pretty thorough coverage of all the different things you can do with numpy and as i continue i'm going to cover all the different frameworks and libraries that are going to work with numpy to enhance and improve upon it so like always please leave your questions and comments down below otherwise till next time