Learn NUMPY in 5 minutes - BEST Python Library!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys Giles here and today I want to do a tutorial and I wanted to do a tutorial about numpy if you want to do data science on machine learning you're gonna need to know numpy numerical python it's one of the most useful libraries in python especially if you're crunching numbers why is it so good because it's fast you can crunch numbers much much faster than say using a Python list or Python loops and it's got so many built-in functions we're gonna do numpy in five minutes let's get five minutes on the screen come over here let's look at the computer and let's get started we will be doing this tutorial in jupiter notebooks I would highly recommend Jupiter notebooks if you don't have it installed and you want to know how to do that I've made a video about that but otherwise let's just get on with it we're going to be doing Python 3 and to begin with we need to import numpy as MP I mean actually to begin with we need to install numpy if you haven't done that you can do that using a pip install and it comes with anaconda anyway which I would definitely recommend so we important numpy as NP it is always imported as NP and to begin with I'm gonna show you how to create numpy arrays and numpy arrays are really important so there are several ways and we're gonna look at a few of them the first way is using this MP zeros you can do that and you will end up with something like this so it creates an umpire array populated with zeros those zeros are actually floats so if we have a look at well first of all if we have a look at the type of what we get here we get a numpy array and if we have a look at the type of the integer or the rather the number inside the array we see that it is a float so just bear that in mind when you when you create an umpire array this way you get floats inside the array we need to do this faster so we can create different sizes of arrays and this is an example of creating a slightly bigger array populated with zeros we can look at the shape of that array but we might want to change that shape if we do we can change that shape to ten by one which may well be a more useful shape and that's something to remember when you're using numpy arrays you might need to change the shape in fact you often need to change the shape you can also create an umpire a populated with ones and you can see that we've got floats there as well and just to prove that if we look at the type of Z in there of the numbers inside the array we can see that we get a float a slightly less common way of creating an array is this NP dot empty and this creates an empty array for us to fill then there's the linspace way of creating an array and what that does it creates a range with a starting point and end point and then the number of elements that you want this is very good a lot of the time if you're making plots or you want to create an x-axis for a graph I mean there are lots of other uses as well but that's just one example that I think you might encounter and here we've now created an array as you can see it's got five elements which is what we've specified it starts at two and goes up to ten and again these numbers inside this array are floats and that's just something to bear in mind I'm taking too long and the final way I'm gonna show you of creating an array in numpy is this MP dot array and that will create one where you put a list inside these brackets in fact you can also put the name of a list inside these brackets and that will also create an array and just to check that we have a numpy array we look at the type they're at it's a numpy array here is a two dimensional array created from a two dimensional Python list and you can see how that works there we can look at the shape of this array and I want to show you a couple of tips on Jupiter notebooks if you're looking for a particular function that you can apply to this array if you type the name of the array and then a dot and then the tab key all of the available functions will come up for you another useful thing is in Jupiter notebooks if you type question mark and then the name of the array you will get the docstring about you know what it is that you've got here this is a numpy array we'll give you the attributes and the parameters that you can apply to it and that is really useful you can also narrow this down so for example if you were to do question mark Z dot and then shape you just get the bit that was relevant to the parameter shape so just bear that in mind that's quite useful faster now you can create random arrays using the random dot Rand int method in numpy and we've preceded it here so if I were to do that with this I would get a random array and I just want to show you how you can access elements of an array you've seen this one before z1 is the name of the array and then in square brackets 0 this will give us the first element of the array which is 5 we can get a range by doing 0 : 2 and that gives us an array of that range there and if we want to get the last element at the array we can use minus 1 and all that's really similar to Python lists so if you're familiar with Python lists this won't seem too odd we're getting there we're getting there The Times ticking I know that it's not going to be 5 minutes all right it's not going to be 5 minutes it's gonna be longer what I want to do now is use a photo to show you other ways that you can slice up a numpy array because I think that visual element is really helpful I've got this photo of York Minster and I am just reading that in now as an umpire array and just to show you here type of photo is a numpy array let's find out a little bit more about that photo what's its shape well it's 324 rose by 574 columns and then it's a color image so we've got three channels of RGB let's now have a look at this photo and there you can see it so let's do some slicing on this photo just to see how this works so so start stop step the step is minus one so we're going backwards so we've reversed the rows in this image what will this give us comma colon colon - one let's have a look well we've got all of the rows and we have reversed the columns so we've got a mirror image because the columns are in the reverse order what else can we do well we can just take a section of this photos if we take the rows from 50 to 150 and the columns from 152 to 80 which is from here to about here we should just cut out this tower and if we do that and have a look we can see that's exactly what we've done numpy though numpy it's numpy you've got to learn numpy just keep learning number I know it's taking longer but it's numpy and if we were to take every other row and every other column we've halved the size of the image if you have a look now this goes to about 300 where it was almost 600 on the x axis and on the y axis it goes up to just over a hundred and sixty whereas before it was over 300 so we've taken every other row and every other column so I hope that gives you some insight into slicing now I want to show you about applying mathematical functions to a numpy array we'll be there soon I promise I promise we'll be there soon and you can do that by applying it to every element of the array by using these numpy methods so we have this photo here we know what that is if we wanted to take the sine of every element that made up the numpy array of this photo we could just do it like this and we've taken the sine of every single number now obviously it's not much use doing that to a photo but if that's your data and you needed to take a sign the sign of every element in your data that's a very quick way of doing you don't have to worry about loops you can just broadcast the whole thing across the numpy array so that's really quick and then there are loads of other things that you can do so you can take the sum of the array or the product that mean the standard deviation the variance the minimum the maximum you can get the arc min and the arc max so let's just have a look at that so the sum of all the elements in photo I is this the product of all the elements is 0 the mean value is this not long to go now and then you'll know numpy and that that'll be a great thing all right it will I promise I promise I really do the standard deviation is this the variance is this figure here the minimum value is 0 the maximum is 255 or we would expect that because this is an image the odd min gives you the index value of the minimum which is that one and the arc max gives you the index value of the maximum so you can do some very quick statistics using numpy arrays and and these methods in numpy so now we're going to create another numpy array this is 1 2 3 4 5 should it be taking this long let's get back to it and there are some interesting things you can do as well you can you can test for where this is lower than 3 and you get an array that is true true false false false and you can do the same with let's say greater than 3 and you get the reverse here you want the array where the array is greater than 3 and you get 4 & 5 so you can apply these masks to numpy arrays which can be really useful when you're handling data you know if you wanted to find all of the if you had a data set of heights for example you wanted to find all of the heights greater than a certain value this would be very easy you could do it in a line of code and you know and again it's much much quicker than using a loop in python so we're gonna do something with this photo now to show you this in action so MP where photo is greater than 100 we are going to replace it with 2 5 5 and where it isn't greater than 100 we're gonna replace it with 0 let's do that and let's have a look at what we get and we get this image here so we've taken a threshold of 100 and we've replaced everything that's over 100 with the value 255 and everything that's under 100 or 100 and under with zero more numpy that's what we need more numpy the time's running down but we need more numpy you know and we could change that threshold if we wanted to and if we look at the output now you know we get a different output other things that are useful to know about numpy is now let's take these two arrays we've got a array and b array we can add those together and if we do we just get each element added together to give us seven nine eleven thirteen and fifteen if we add 30 to this array it will add 30 to each element if we multiply these arrays it will multiply each corresponding element together and give us a new array of those new elements if we multiply one by ten so if we multiply a array by ten each element is multiplied by ten do we have enough numpy yet are you numpy it out have you had enough of numpy no let's get back to it and then if we use this at symbol here we get 130 and that gives us the dot product of these two arrays so that's a nice way of getting the dot product and I just wanted to show you one other thing as well this dot T which gives us the transpose of an array so what that does is it interchanges rows and columns and just to visualize that a bit better you can see that if we apply it to this photo that's what we get so that is the transpose of an array so in in photo terms it turns a landscape into a portrait and and that's what it does with data as well and I find that a useful way to visualize this and finally you can sort an array again without having to use loops you can use the inbuilt methods here and if you do that we take this array that's unsorted and we can sort it using MP dot sort and then the array name okay I'm sorry that wasn't five minutes I know it was longer but I think that's given you the basics of numpy and you can go off and really practice with it I hope that was useful if it was like the video subscribe to the channel share it as far and wide as you possibly can and I'll see you next time bye bye
Info
Channel: Python Programmer
Views: 537,405
Rating: undefined out of 5
Keywords: learn numpy, learn python, python library, numpy, python, programming, numerical python, numpy array
Id: xECXZ3tyONo
Channel Id: undefined
Length: 13min 37sec (817 seconds)
Published: Thu Jan 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.