NumPy for Beginners in 15 minutes | Python Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
want to get up and running with numpy super fast well you've come to the right place my name is nicholas renate and in this video i'm going to be taking you through a crash course on numpy you'll be able to take away these skills and use them to build deep learning models with tensorflow work with pandas and even get up and running with machine learning let's take a deeper look as to what we're going to be going through so if you've never worked with numpy before basically it's a python library that allows you to work with arrays for all different shapes and sizes it has support for n-dimensional arrays so you could have an array within an array within an array within an array and this is particularly useful when you're working with deep learning libraries like tensorflow or keras or pytorch and it also helps you a lot whenever you're performing data science with pandas or if you're building machine learning models so in terms of what we're going to be covering we'll take a look at how we can install numpy using the pip install command we'll then take a look at the crud framework for numpy so basically that's going to cover creating reading updating and deleting numpy arrays and then last but not least we're going to be taking a look at how we can export numpy arrays so that you can save your work down and work with it later on ready to get to it let's do it alrighty so in this video we're going to cover the four key things you need to know about numpy we're going to take a look at how you can create numpy arrays how you can read and work with them how you can make updates to them and perform mathematical functions and last but not least we're going to take a look at how we can delete and export those numpy arrays now the first thing that we need to do is actually install numpy and import it into our jupyter notebook so we're going to be working inside of jupyter and coding with python because numpy is a python library so let's go ahead and first up install it using the pip install command okay so in order to do that what we've done is we've used the exclamation mark and then typed in pip install numpy and you can see here that i've already got it installed but if you're installing it for the first time it might take a little bit longer but it will go through all the steps you need in order to install numpy now what we can do is import it into our notebook perfect so that's done so it's pretty common practice to import numpy as np whenever you're working with a jupyter notebook or doing data science that's just pretty common practice so in order to import numpy what we've done is we've imported numpy as np now what we can do is actually go through the process of creating some numpy arrays so the whole concept or principle around numpy is the ability to work with n-dimensional arrays so that might be an array stacked inside of another array stack inside of another array an array is really just a list that looks something like this so you might have a bunch of numbers if we want a two-dimensional array then we're just going to have two arrays within another array sort of something that looks a little bit like that now in this case we've got a number of numpy functions that help us make arrays so let's take a look at some of those functions so specifically we're going to take a look at the np.random.rand function which will allow us to create random arrays we'll look at np.0s mp.full as well as how to create a custom array okay so what we've done there is we've created four different types of numpy arrays so our first numpy array that we've created is a randomly generated array so specifically we've created an array with a random set of numbers in the shape of two three four but we'll cover the shape a little bit later so if we actually take a look at that array you can see that we've got a whole bunch of random numbers we've got three different arrays which each have four numbers in them and then we've got two larger arrays up here but you can see that it's all got random numbers the second array that we've created is done using the np.0s method and this basically allows us to create an array which is full of zeros so if we take a look at data we can see that that's got a whole bunch of random numbers but if we take a look at zeros you can see that that's just got zero values in there likewise if we take a look at full it's sort of similar to our np.zero's function except in this case we are replacing the zeros with the number seven so rather than having zeros we're going to have seven so if we take a look at the full array you can see that it's got a bunch of sevens in it and the last function that we used was np.ones so this basically works similar to zeros and similar to full except this time it's replacing the values with one so if we take a look at that you can see that we've just gone and created three different types of arrays so random gives us random values zeros gives us zeros full gives us a specific number that we passed in this case we did seven and ones fills our array with ones now what happens if we wanted to create an array with our own numbers well what we can actually do is use the numpy.array method so this is going to allow us to take a regular array or a list and convert it into a numpy array so let's try that up and there you go so now what we've done is we've used the np.array method to go and create our own custom array so you can see here that we've actually gone and generated our own array and if we take a look at the type you can see that it's now a numpy ray so that basically goes through all the steps to create an array so we can build a random array one filled with zeros one filled with our own number one filled with zeros and we can also generate our custom arrays now let's take a look at all the things that we need to do whenever we're reading our rates so there's a couple of key attributes within each of your numpy arrays that allow you to get information about what you've actually got on hand so let's take a look at those okay so what we've done there is we've grabbed three different attributes so first up we've grabbed the shape so if we take a look at that you can see that we're actually getting the shape of the array there now we've done this on our data array which we created up here so it was a random array with the shape 234 and you can see that we're accessing that shape back down here as well now we can also take a look at the size which in this case gives us the number of values that we've got within our array and we can also take a look at the types of data that we've got in there so in this case we've got everything as a float now say we actually wanted to work with the different values that we've got in our array what we can actually do is slice and traverse through our numpy ray all right so we've done a couple of different types of slicing there so the first method that we've just used there is going to get our first instance of data within our rate now keep in mind that our data object had the shape of two to four so oh sorry two three four so that basically means that it's going to grab our first array of three values so if we take a look at our arr variable you can see that we've got our first array and in that is three arrays of four values which is effectively this little part here so the three and the four now if we go one step further we can use a slicer to grab multiple values so in this case we've actually grabbed both the first and the second because we've gone and specified everything there but if we actually change that and grabbed our just our first two you can see that we've now gone into our first array and we've only grabbed the first two arrays within that so this allows us to slice within our different numpy arrays now we can also grab the last value by using a negative so in this case rather than grabbing our first array we're grabbing our last one and you can see that within our reverse line here and again that's grabbing our last array and again if we wanted to grab a single value well then we can traverse our rate so in this case we're grabbing the first value within our larger array the first value within that and then the first value within that so if we take a look we're grabbing a single value and if we take a look at our bigger data frame we're grabbing this value right there and that's really what you need to know in terms of reading so basically you can grab a bunch of attributes and you can also slice through your different numpy arrays to get the values that you need now there's going to be a whole heap of functions that we cover within our update section but first up let's create two arrays that we can use to actually use some of these functions on so again we're going to use the np.random.rand function to create these two lists okay so we've created two variables there so list1 and list2 and again we've used the np.random.rand functions to create arrays of 10 values so if we take a look at list1 you can see that it's got 10 values and again list2 that's got 10 values as well now there's a number of basic math functions built into numpy so that allows you to add subtract multiply divide and also perform a dot multiplication so let's take a look at those first so there's a fair few different math functions there but the basic premise is that we've got add subtract divide multiply and dot and the advantage of using these on a numpy array is that they are super fast and that they work on numpy arrays so if we take a look at our first one so again we've used np dot add to add list one and list two together so we can take a look at that and we've also used np dot subtract to subtract them we've used np divide and np multiply to divide and multiply respectively so if we take a look at div again we've divided one by the other and multiply we've multiplied one by the other and the last function that we've got there is np dot dot so this allows us to create a dot product so basically multiplies every value together and then aggregates the result so again that's add.product now there's also a whole bunch of statistical functions built into numpy so let's take a look at those now there's a really easy way to remember each one of these functions and that's to remember sap lem so whenever i try to remember these functions i try to imagine a tree and there's sort of sap dripping from the tree and that helps me remember sapling so let's take a look at these statistical functions okay so we've used quite a fair few different functions there so remember saplem so it's s-a-p-l-e-m-m oh we haven't actually applied a function there so this helps you remember each one of these functions here so first up we've got square root and again we're calculating the square root of 25 which is five we're also using np dot abs to basically get the absolute value so we've converted our negative two to two we're able to use power to calculate a power so in this case it's two to the power of seven we can calculate the logarithm so again we're taking 25 and applying a log function we can also use the np.exp to create an exponential and again we're now calculating our exponential on two and three and we can also grab our minimum and our maximum using the npe.min and np.max functions so that covers applying some functions to our different arrays now what happens if we actually wanted to update a value within an array so if we cast our minds back to our data array which we created right at the start say we wanted to change this value here to 700 for example well what we can actually do is access that value using the same method that we had right up here and we can actually reset that value so right now we're getting 0.48 which is that but what we can do is assign a new value to it and now again if we take a look at our data we've now gone and updated that value now say we wanted to sort our data we can also sort our data while we're at it so again we can just use the sort function on our array and you can see that we've now gone and sorted that now sometimes when you're working with tensorflow or when you're working with keras you might need to reshape your array well in order to do that you can use the reshape method to go and do that so let's take a look at that so if we take a look at our initial data shape you can see that we've got an array with the shape of 2 3 4. now assume that we wanted a new array with the shape of 2 2 and then we didn't care about the last value well what we can do is use the reshape method there so let's do that and there you go so what we basically did is we grabbed our array we then used the reshape method and passed through the shape that we wanted by specifying negative one we're basically telling numpy that we don't care what the shape of the last value is and in this case it's automatically calculated that it needs to be six now the last thing that you might want to do whenever you're working with numpy arrays is append to them or insert new values so let's take a look at how you might do that so what we've done there is we've initially created an array so using the np.0s function that we set up right at the start within our create section and we specified that we wanted eight different values in there so in this case what we've got is we've got eight different zeros now say we wanted to add some values to it so we wanted to add a three and a four well what we can do is use the np dot append method pass through our initial array and then pass through what we want to append to it and you can see that by doing that we've then gone and appended a three and a four to our initial zeros array now that appends to the end say for example we wanted to insert some values in a really specific place well rather than using np dot append we can use np dot insert to insert a value at a specific area so what we've done is we've used the np.insert method we've passed through our initial array we've then specified the position that we want to insert our values so in this case we've specified position 2 so 0 1 2 and then we've included the value that we actually wanted to insert so in this case we inserted a number one and then we printed it out so basically you can see there that by using the append method we're going to add a value to the end and by using the np.insert method we're going to insert a value where we want it to be and that about wraps up our update section so what we did is a whole bunch of stuff so we went through our basic math function so add subtract divide multiply and dot we then also took a look at our statistical functions or slightly more advanced mathematical functions and to remember that you just need to remember saplem so imagine sap dripping from a tree and this covers square root absolute power log exp min and max we then also updated a value within an array we sorted we reshaped and we also appended values and inserted new values now the last thing that we want to take a look at is our delete section so here what we're going to do is we're going to delete a segment of an array and we're also going to export our array out so that we can use it later on so let's initially take a look at our data and say for example we wanted to delete the first array within each one of these arrays so in this case you can see that each one of our arrays contains two arrays say we wanted to delete the first one well what we can do is use the mp.delete function to do that and there you go so what we've actually gone and done there is we've deleted this and we've also deleted this so the np.delete function allows you to delete different segments of arrays so in order to do that we pass through our array or our numpy array we then specified which level of array we want to delete and we pass through access equals one because we're specifying that we want to delete a row not a column now the next thing that we actually want to do is save our arrays well we can do that using the np.save function so we've now gone and saved away our array and you can see that it's gone and saved as newarray.npy and again this is going to save in the same directory that your jupyter notebook is in unless you specify otherwise now if we wanted to reload that array well what we can do is do that using the np.load function and you can see that we've now gone and reloaded it from memory so within our delete section we've gone through how to delete different segments of arrays how to save them down as well as how to load them up and that about wraps up this video so what we went through is quite a fair bit so we took a look at how to install numpy how to import it how to create a random array create an array field with zeros fours and ones we also took a look at the different attributes that we can grab from our arrays as well as how to slice then within our update section we covered our basic math and stat functions as well as how to update values sort insert and append and last but not least we took a look at how to delete values out of our arrays and save them down to memory thanks so much for tuning in guys hopefully you found this video useful if you did be sure to give it a thumbs up hit subscribe and tick that bell so you get notified of any future videos and let me know in the comments below what you're using numpy for thanks again for tuning in peace
Info
Channel: Nicholas Renotte
Views: 4,550
Rating: 4.9597988 out of 5
Keywords: numpy tutorial, python programming, data science tutorial, data science tutorial for beginners
Id: uRsE5WGiKWo
Channel Id: undefined
Length: 17min 23sec (1043 seconds)
Published: Wed Sep 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.