Transcript:

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. Were 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-axi’s 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 Time’s 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, lets. 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, lets. 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 lets? 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 times 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!