Learn NumPy in 40 Minutes - Python NumPy Tutorial
Skills:
ML Maths Basics90%
Key Takeaways
This video tutorial covers the basics of NumPy, a library for efficient numerical computation in Python, and demonstrates its application in data science, machine learning, and scientific computing. The tutorial provides a comprehensive introduction to NumPy arrays, their creation, manipulation, and operation, as well as various functions and methods for performing statistical and linear algebra operations.
Full Transcript
In this video, you'll learn how to use the NumPy library in Python. Now, if you're interested at all in data science, AI, machine learning, or scientific computing, the NumPy is a must learn. And fortunately, in just a short video like this, I can teach you all of the fundamentals that will get you quite far. So, with that said, let's get onto the computer and let me teach you NumPy in Python. So, on my screen, I have the NumPy website open. Now, most everything you'll see in this video comes straight from the documentation. I've just condensed it and put it into a nicer format to learn. So, if you want to reference anything, feel free to go here, numpy.org. Now, with that said, I'm going to go into my code editor here. For this video, I'm using cursor, but you can use anything you like. PyCharm, VS Code, doesn't matter. And the first thing we need to do if we want to start working with NumPy is we need to install it. So go ahead and open up your terminal and simply type the command pip install numpy. Now that's going to install that directly into your Python installation and then allow you to import it and start using it inside of your project. So if you're on Windows, you can simply run this command. If you're on Mac or Linux, you can run the pip 3 command. Now once you have NumPy installed, you'll be able to use it by importing it just like this. Now what you see me working inside of right now is something called a Jupyter notebook. And I'm going to recommend that you follow along in this video using a Jupyter notebook because it's the most common when dealing with libraries like NumPy and doing things like scientific computing. Now, if you're wondering how to open a Jupyter notebook, it's quite simple. If you're working inside of VS Code cursor or any VS Code fork, so something like Windsurf, you can hit on your keyboard control shiftp or commandshiftp if you're on Mac. You can type Jupiter. So, JUP, and then it should start showing the autocomplete here. and you can go create new Jupyter notebook, the one I have selected right here. From here, it should open up a new notebook which ends in this ipinb extension. From here, you're able to directly write markdown code. So, something like hello world or you can write a code cell where you can directly import and run code. So, for example, if I do something like import numpy, then I can actually just run that cell and we'll see I select a kernel. So, let's select this one right here. They will have an output block if anything is outputed directly below that. and we can create other code blocks or markdown blocks throughout the file. So, I've already created a notebook that has all of the code that I'm going to be showing you in this video, but I recommend you make your own notebook again ending innb and you mess around and kind of play with some of the examples that I'm showing you in this video. And last thing, if you want the exact notebook that you see on screen to follow along with, you can download it from the link in the description, simply sign up for my email list and I'll give it to you for free. So, with all that said, let's start with an introduction to NumPy. Well, NumPy stands for numerical Python, and it provides support for large multi-dimensional arrays and matrices. It includes a collection of a ton of mathematical functions to operate on these arrays, and it's essential for any type of data science, machine learning, and scientific computing task in Python. NumPy itself is extremely efficient. The underlying operations are written in C and C++, and it's used in all of the major frameworks that you've probably seen in Python before. Think something like TensorFlow or PyTorch. All of these use NumPy behind the scenes. So it's essential that you understand how NumPy works so that when you're working with those types of operations or frameworks, you know what's actually going on and the type of data you're working with. And again, just to stress, NumPy typically deals with large arrays. So you're going to see in this video that pretty much everything we do has to do with numbers and arrays. Okay. So if you want to get started with NumPy, the first thing you need to do is import it. So you import numpy as np. Now this is just the convention. You're almost always going to see anytime numpy is used directly that it's imported with the alias np. Now let's just quickly go over why you would use numpy. I already talked about some of the reasons and I'm going to give you a few practical examples in some of these code cells right here. So like I said, numpy arrays are more efficient than python lists for numerical operations. In fact, they're significantly more efficient and they make the code run significantly faster. So you can see that I have a Python list right here where I have some elements like 1 2 3 4 5. Then I have the exact same thing represented as a numpy array. So this is your first introduction to how to create a numpy array. I can take a Python list and I can wrap nparray around that. This now represents the data differently in my computer's memory and it uses numpy which again is written in C and C++ as opposed to pure Python. So now when I run the cell here, we can just go through the output of some of the examples here. So when we print the python list looks like this right 1 2 3 4 5 when we print the numpy array looks very similar just notice that we don't get the commas here so we get 1 2 3 4 5 then we can look at the type of these different objects and you'll notice that when we look at the type of the python list it is class list whereas when we look at the type of the numpy array it is numpy nd array now nd array stands for nd dimensional array just meaning this can be in any number of dimensions which we're going to get into later then I just do a quick operation where I add 10 to all of the values inside of the list. Now, in order to do that with a Python list, notice that I actually need to use a for loop or some kind of mapping operation. So, I have x + 10 for x in Python list. So, I get all of the elements and add 10 to them. Whereas, when I do it with numpy, I'm actually able to perform a vectorzed operation, which means that I can directly add 10 to all of the elements inside of this list without actually having to loop over them. Now, again, this has to do with how NumPy represents the data and the way that it's actually written. It's a lot more efficient and I'm going to show you a quick time comparison right now so you can see how much faster working with a numpy array actually is. So I have a quick example on my screen here where what I do is create two lists. One which is a Python list which has 1 million elements and another which is a numpy array. So same thing but just represent as a numpy array again with a million elements. Now, what I'm going to do here is perform a quick performance analysis where I see how long it takes to add one to all of the elements inside of the normal Python list versus how long it takes if we use a numpy array. So notice in this example I'm using a for loop, right? And in this example, I'm just using the scalar addition. We'll talk about this later where I'm adding one directly to the numpy array. So I'm just going to run the code here and I just want you to see that what happens is numpy is 35.6 times faster where with the Python list it took 0.03 035 seconds. And with the NumPy array, it took 0.001 seconds. Significantly faster. And this is just for a million elements. So hopefully at this point you have a decent understanding of why you would use NumPy and what it is. And we're going to get into all of the syntax and features of NumPy after a quick word from today's sponsor, boot.dev. It's an online learning platform designed specifically for back-end development. and it approaches learning in a way that's far more interactive than the usual video-based courses. Rather than having you sit through hours of lectures, boot.dev puts you straight into hands-on coding. You'll work directly in your browser building real projects while learning back-end fundamentals like APIs, databases, and serverside logic using Python and Go. Now, what makes it stand out is the way that it borrows from game design. You'll progress through levels, unlock new content, and keep your momentum up as you go. The platform is filled with exercises and practical challenges, so you'll end up writing a lot of code, which is exactly what helps you improve. Now, all of the core content is free to access, and if you decide to commit to the annual plan, you can use the code tech with Tim to get 25% off your first year. I've been going through it myself lately, and honestly, it's surprisingly addictive. Thanks to boot.dev. Now, let's get back into it. So, let's get back into it and let's start talking about creating numpy arrays. Again, when we're working with numpy, we're almost always using some form of arrays or lists, whatever you want to refer to them as. Now, here I just have a kind of block of code that shows you a ton of different ways that you can make numpy arrays. Oftent times when you're working in frameworks, you'll already have a numpy array provided to you or it'll be used behind the scenes. But if you want to make one by yourself, here are many of the different ways that you can do that. So the first way is from a Python list, right? We can simply just create array where we say np dot array and then we put the elements inside of a Python list that we want to be in the array. Now it's important to note that when you create a numpy array, all of the elements need to be the same. So unlike in Python where I could do like true or hello or something like that, that doesn't work in numpy, they all need to be the same type. So either floats or ins. Next, we can use np.arrange. Now, this is similar to using range in Python where what we can do is create a range of values. So, in this case, we're starting at zero, stopping at but not including 10, and stepping by two. So, we're going to get an array that looks like 0 2 4 6 8. Next, we have np.space. Now, this is going to give us evenly spaced numbers. So, in this case, we can have a start, a stop, and then the number of elements that we want. I'll show you what that looks like in a second, but it would be 0, 0.2, 0.4, etc. Next, we can just create a full array of zeros. In this case, it's going to give us a one-dimensional array with five zeros inside of it. We also could create an array of ones. So, it says MP ones, right? And then we have five ones inside of there. Continuing, we can do np.full. Now, when we do np.full, that's going to populate this number of elements with this value. So, we're going to have five elements that are all sevens. We could have an identity matrix. I'll show you what that looks like in one second. We also could have a random array. So, we're just doing random values. So, five random values or we can do random integers in some kind of range. So, if I run this cell here, you'll see what all of these values look like. Again, you can play with this on your own as well. And you see 1 2 3 4 5 0246. This one I got wrong. Sorry, because it only went up to four, not five. So, 0 0.25, etc. We have full the identity matrix, which looks like this. Random values, random integers. You get the idea. So, that's how you can create numpy arrays. Now, like I was saying, when you use numpy arrays, all of the values inside of those arrays need to be the same type. And that's referred to as the dtype or the data type of a numpy array. Just have a look at this code example right here. I'm actually just going to run this so we can refer to some of the values. So, if we scroll down here, you'll see the first array, right? It's just a bunch of whole numbers. So, 1 2 3 integers in Python. And you'll see that the type of this array when I print it out is int32. And to access the type of a numpy array, you can use this dt type value. Then you'll notice that I had some floats. So 1.0, 2.0, 3.0. And when I print out the type of this numpy array, it's float 64. And then if we have mix types, so if we mix integers and floats together, which is allowed, it's going to give us float 64. The reason why this is allowed is because one can be represented as a float. However, 2.5 could not be represented as an int. So, we need to promote the type to be a float, which does take up more space in our computer to be able to represent both of the types that we have inside of here. So, just keep that in mind. You're always going to have one type. And if you're going to have these mixed integers and floats, it's always going to be a float array because that's what's needed to represent those values. Now, continuing from here, if you wanted to specify the type that you're going to use manually, you can do that. For example, you can use a float 32, which is going to use 32 bits as opposed to 64 bits by default. You also could specify that this is an int for example, right? Up to you. You also can change the type of a numpy array. So if you want to represent an array as a different type, you can say the array as type and then specify the type directly from numpy that you want to convert it into. When you do that, it's going to give you a new numpy array with that new type. Okay? And then you can see the amount of memory that's used for these different types. Four bytes and then eight bytes. Right? So if you want to have lower amount of memory, you want to use in32 or float 32. If that doesn't mean anything to you, don't worry. Not super important right now. Okay. So, let's continue and talk about multi-dimensional arrays, which is where numpy becomes really powerful, but also a little bit confusing. So, when we talk about dimensions, we're talking about essentially the number of nested arrays that are inside of one another. So, let me go into this cell here. If we talk about a onedimensional array, it's just something like this, one that you've seen before, just like a standard Python list. 1 2 3 4 5. we have one dimension because the array or the list is flat. If we talk about two-dimensional arrays, that's where we have something like a matrix or rows and columns. So notice here we have arrays inside of this array. Now numpy is capable of representing multi-dimensional arrays typically 2 3 4 5 multiple dimensions, right? And that's when you have multiple lists or arrays nested inside of one another. So I just want to show you a few examples here. If we're looking at a 2D matrix here and we print it out. So let's actually just run this right here. You can see it looks something like this when printed with numpy, we're then able to have a look at some of the properties of this matrix or of this numpy array. So I can look at the shape. For example, this is a very important thing to understand in numpy. And when I look at the shape, it gives me 33. Now the way that the shape works is that the number of elements inside of this tpple here is the number of dimensions that is inside of my array. So in this case we have two dimensions because we have a matrix or a 2D array. The first dimension which is the number of rows is three. So we have three rows and then we have three columns or three elements inside of every row. That's how the shape works. We start with the outermost dimension and then move inwards and give the stats or essentially the number of elements in each dimension. Then if we want to look at the number of dimensions we can look at n dim number of dimensions which is two. We can look at the total size which is the total number of elements. In this case we have nine and then we can look at the data type which in this case defaults to int 32. Now let's have a look here at an example of a 3D array. Okay. So we can print the 3D array out and it looks something like this, right? Where imagine we kind of have like pages of 2D arrays. So we have this first page which is this one, this second page which is this one and then within there we have rows and we have columns. And notice the shape of this is 2 two two because we have two pages. We have two rows within each page and then two columns within each row. Now if we were to change that. So let's just make this a little bit different for example sake. So three five. Okay. Like so. And let's rerun this. So rerun the cell. You can see now the shape is 2 2 3 because again two pages two rows and then three values inside of each row. That's how the shape works. Make sure you understand that because that's pretty important. And then lastly, I'll just show you that if you want to create your own multi-dimensional arrays, you can do that by specifying the shape when you're using some of those methods that I showed you above. So when we did np.zeros before, notice that before I just did like three, right? That just means add three elements, right? And make a one-dimensional array. However, if I want to make a multi-dimensional array, I just specify the shape of the array. In this case, I want a two-dimensional array which has three rows and four columns. I specify that and then again if I run the cell you can see it will create that for us. So we have three rows and four columns and then same thing we can create one like this. I just didn't print it out. So let's print. Okay's 3D and then run this. And you can see now it looks like this. It's just kind of not showing all of it because it's giving us a preview when it prints it. And you see the shape is 2 3 4. And just as a quick cheat sheet here, I just put a cell in this uh Jupyter notebook that shows you all of the different major properties that you want to know about numpy arrays, right? So the shape, the size, the number of dimensions, the data type, you can also look at the item size, so the size of each element. You can look at the number of bytes that's used by this array. And then if you're familiar with a transposition of an array, you can use T. And this will transpose the array automatically for you. So you can use it, you know, as is needed when you do kind of those linear algebra type operations. Again, if you're not familiar with the transposition of an array, don't worry. It's probably not important for you. Okay, so let's move next to indexing and slicing. Now, just like standard Python list, you can access all of the elements inside of numpy arrays by using a standard index or a Python slice. However, it gets a little bit more complex with numpy arrays because you're working with multiple dimensions and you can create kind of multi-dimensional slices. So, let's have a look at this simple numpy array up here. This is a one-dimensional array. Okay. So, when we do 1D indexing, this is the exact same as a standard Python list. So, if I want to access the first element, array at index zero. I want to access the last element, I can use a negative index, negative 1. I want to do a slice where I get all of the elements between a range, I can do that. I want to get every other element. I can do that using a standard Python slice, which works like start, stop, step. When you don't have an element, it means start at the beginning or stop at the end based on what kind of position you're in in this slice. Now, when you get into 2D indexing, it gets a little bit more complicated. So, let's run this so we can see the results. Notice now I have my numpy array, two dimensions, right? Rows and columns. Can print it out. And if I want to access the element at row one, column one, rather than having two sets of indices like I would with a standard Python list, I'm using them inside of one set of square brackets. So I'm saying matrix and then 1, 2. I'm referencing I want to look in row one, column 2. Okay? And I'm separating those values with commas. So every comma I put here is referencing a dimension that I want to access. So dimension one, I want to access index one. dimension two I want to access index two. Now if we wanted to get all of the columns in the first row for example what we can do is do matrix zero comma and then we can put a colon. When we put the colon this is equivalent to having a slice where we're essentially saying we want to start at the very beginning and then end at the very end. So we're just putting a uh what is it a colon here without putting anything on the left or right side. Now let's say I want to access the second column. I can say okay matrix same thing colon so reference everything and then just get the second column right so I'm putting a one like that and then if I wanted to get a submatrix for example so the first two rows and the first two columns then I would say okay I want to start at zero and go to two and then start at zero and go to two and if we have a look here you can see this is my 2D array the element at row one column 2 is six so this one right here the first row 1 2 3 the second column column is 258 and then the submatrix you can see it's kind of this small square right here 1 2 4 5 okay cool so that's a little bit complicated can be a bit confusing so I recommend playing around with it trying it with some 2D and 3D matrices and when you get into the 3D 4D 5D matrices the exact same thing applies as what you're seeing here we just have more dimensions so if we add another dimension here right this just means I now add another row or another kind of commaepparated value inside of my index when I'm trying to access the values. And this is where numpy can get confusing. It's very difficult to visualize anything beyond three dimensions. So when you start getting into four dimensions, five dimensions, six dimensions, it's easier just to go back to the fundamentals, understand how it works with two or three dimensions and then apply that at a higher dimension value rather than you trying to visualize it. because again it's very difficult to visualize data that's organized more than three-dimensional because just as humans work we only look in two three and one dimension. Okay, so let's continue here. Now we have boolean indexing as it says here. Pretty powerful feature. So I have just a 1D array here and I'll show you that what I can actually do is have kind of this conditional expression inside of my index where it's only going to grab for me values that specify or that are true essentially in this expression. So I said okay all the elements greater than five. So I have array and I did array greater than five. When I do that it's going to go through here and it's going to apply this boolean and only give me the elements greater than five. So if we have a look down here you can see 6 7 8 9 10. Same thing with the even numbers. I can do array mod 2 equal to 0 and then it's only giving me those values. Now you can use these when you're dealing with more than one dimensions as well. But again you now need to have multiple values in your index. So now let's continue to the next section which is array operations. So of course when you're working with numpy you can perform all kinds of operations with numpy arrays. And again these operations that you perform are significantly more efficient and also just easier to write out and perform than if you were dealing with a standard Python list. So you can see that I have two numpy arrays here a and b. Now if I print these out so let's actually just run them. You can see what they look like right here. Now we can perform multiple types of operations. We have element-wise operations which means each element is going to be applied to the other element. So like one is going to match with five, two is going to match with six, three is going to match with seven. It's going to go element by element. We also have scalar operations. When we perform a scalar operation, this means that we're going to take some individual value and add that to all of the values inside of the array. So when I add 10, that's adding 10 to every individual element inside of the array. Same thing when I multiply by two, multiplying every element by two. When I do power of three, same thing, every element by three. And then we have comparison operators. I'll show you what those look like in one second. Okay, so we have the ability to perform addition. So if I want to add, you know, array A to array B, I can do that by adding all of the individual elements. Same thing with subtraction, multiplication, right? This is not a dot product if you're familiar with that. So it's different. We have division a / b. We can raise a to the power of two. Although I think I already did that down here. So let's delete that one for right now. We can do the square root for example. So np.square root. And let's run this. And you can see here are some of the examples. Right? So we add a + b. We take the element 1 add it to five. We get six. Take 2 6 get 8. Take 3 + 7 get 10. Take 4 + 8 get 12. Subtraction. Same thing right? multiplication. You get the idea. You can look at those examples. Now, here if we add the values, you can see a + 10 11 12 13 14, right? A * 2 2 4 6 8. You get the idea. And then comparison operators. Here's where it looks a little bit interesting and might be surprising to you. When we perform a comparison like this, it will perform it on every single element. So, it gives us a new array that contains the boolean which represents whether or not this expression was true. So, a greater than two. Well, is that true for these elements? Yes, it is. But it's false for these ones. And you get the idea. And you can perform kind of some advanced arithmetic and comparisons using this type of syntax. So, let's continue and talk about array manipulation, specifically reshaping and resizing arrays. Now, it's very common that when you're dealing with numpy arrays, you need to reshape them or change kind of their format. For example, imagine you have a two-dimensional array and you need to flatten that into a one-dimensional array. Happens all the time. I'm going to show you some examples of how you can do this. Okay, so I've just created a 1D array here, right? We can run this. And if we have a look, this is what the 1D array looks like. Now, I might want to convert this 1D array into something of a size of 34, where I create a matrix that has the same values as this. Now, in order to do that, what I can do is use this reshape method. Now, when I reshape the method, I need to ensure that the new size matches the number of elements that I have in the original array. So in this case, because I have three rows of four elements, I have a total of 12, which matches the number of elements in this array. So that's totally fine. I can reshape this and it works properly. And if I scroll down here, you'll see that it's now reshaped this and I now have three rows with four elements each. However, if I added an additional element, so I made this like 13 for example. So let's save that and rerun. And you'll see the error here. Cannot reshape arrays of size 13 into shape 34. Okay, so the shape needs to match the number of elements if you're going to do this resizing. Let's change that back. And now it should be good. Okay, now same thing. We can do a flatten. So like I said, if we have something that's like a two-dimensional array or three-dimensional array, we can flatten that into a one-dimensional array. To do that, you simply just write flatten. And now if we run this, you'll see that we took this and we flattened it back into the original array. Now let's continue. We also have the ability to reshape using an automatic calculation. So sometimes you don't know the exact shape or the exact size that you want to change the array into. And if that's the case, you can use this negative one, which means calculate automatically. So what I'm saying here is I want to reshape my original array. So this one right here. And I want to reshape it into something that has two dimensions because I'm specifying two values here. The first dimension, I know I want to have two rows, but I don't know the number of columns that I want to have. So I'm going to put a negative one here where it will now automatically calculate how many values I need in the columns for it to uh reshape that for me automatically. So now what it will do is it'll auto reshape with two rows and then automatically find the number of columns. So again when I do that you can see that it's reshaped this and we now have six elements in each column because that's what's required to create this shape. Now if we want to do this for more than one dimension I could do for example maybe 2 to1 this works as well. So I can run this and you'll see that now we've got three elements in every single row because that was what was required with the automatic calculation. Okay. So we had two pages essentially two rows and then three columns. Okay let's go back and just change that back to what we originally had. Then we have transpose. So, like I was saying, you can transpose a matrix. If you transpose a matrix, you're essentially just writing it not in reverse, but where every single row is where every single column used to be. So, you're kind of just flipping the rows and the columns. So, if we look at the transposition, you can see this is the original matrix. And then if we transpose it, we now have kind of a longer matrix where all of our columns are now our rows, right? And all of our rows are now our columns. And then we have concatenation. So if you want to essentially add multiple matrices together, you can do that. But you need to specify how you're going to add them. So you can see here that we have two two-dimensional matrices. Right now, if that's the case, if I want to add them or concatenate them, I can vertically stack them, I can horizontally stack them, or I can concatenate them and use a specific axis. Now, when I specify an axis, this is the same thing as me using a vertical stack or a horizontal stack. I'm just doing it more manually by saying what axis I want to combine these matrices on and we're going to talk about axises in one second so it makes a little bit more sense. Okay. So sorry with the array concatenation you can see we have array A array B. When we do the vertical concatenation and we stack them along the vertical axis you can see we have 1 2 3 4 5 6 7 8. So we just took these added them to the end there. Whereas when we do a horizontal concatenation it adds it to the rows. So you can see that this row got added to this one and then this row got added to this one. Okay. Now let's move on to understanding dimensions and axis because this is an important concept and something that's commonly misunderstood in numpy. Okay. So when we talk about numpy arrays, you're going to hear dimension, axis, and shape very commonly. I just want to go over these again and show you a few examples so you really kind of know how to wrap your head around them because that's in my opinion the most confusing part about working with these types of data structures. Now the dimensions is the number of axes. Okay, essentially the number of kind of nested arrays inside of one of another. So if we have one dimension, right, we have one axis. If we have two dimensions, we have two axis. Now at two axis, most people can understand, right? Because it's just a two-dimensional array. Kind of looks like a grid, right? You have a row and a column. When we start going beyond three, as I mentioned, it's very hard to visualize, but you still can have that type of data represented in NumPy. It's very common to see that type of data, but it's just hard for us to wrap our head around. Now, when we talk about the axis specifically, this is the dimension along which an operation is performed. So, if I'm working in a two-dimensional array, for example, then I have two axes, right? Axis 0 is the row, axis one is the columns. And then, of course, we have the shape. So, this is the size of each dimension. Like we talked about before, we're going to have multiple values for the shape. The number of values we have actually specifies the number of dimensions or the number of axes. And in this case, if we had a size of three, four, that would be three rows, four columns. Now, let's skip this first cell and just move into this second one where I'm going to show you a few examples of performing operations and specifying the axis that you want to perform these operations on. So, let's say that I want to get a sum of a matrix. Well, in order for me to get that sum, I need to specify what axis I want to perform the sum on or no axis at all where it's going to sum all of the values. So, when I specify axis zero, this means that I want to collapse all of the rows and essentially operate down the columns. So, I want to get one value per column and I want to get three sums or multiple sums based on the number of values that are inside of my matrix or the number of rows that I have. So when I say okay I want to do npsum matrix and then axis zero. Effectively what I'm saying is I want to get the sum of all of the values in the columns. So when I say axis0 what I'm really saying is I want to sum all of the columns. So I'm going to run the cell so you can see what I mean in a second. But the same thing applies down here when I specify axis one. So let's run this and have a look. So I have my matrix, right? Three rows, three columns, shape 33. Now, when I take the sum and I specify axis 0, this is the sum. This is what it gave me. It gave me a new array that now has a shape of three. That's it. The shape is just three because it's a one-dimensional array. Now, the values here are equivalent to the sums of all of the columns in my matrix. Now, we can reference that because you can see that it's added 1, 4, and 7, which gave me 12. It added 2, 5, and 8, which gave me 15. added 3 6 and 9 which gave me 18. And just a quick example you can see if we're getting the column 0 sum right 1 + 4 + 7 as I showed you which equals 12 which is in index zero. If we're getting the column 1 sum 2 5 8 which is 15 which then is in index 1. Now if I reference axis 1 this is going across the columns or giving me each row sum. So you can see I have 1 2 3 which is six. I have four 56 which is 15. I have 7 8 9 which is 24. So when I reference axis 0 that's specifying the rows, right? So it's kind of the outermost dimension that I'm looking at. When I specify axis one that's talking about the columns or the values inside of the row. So it gave me the sum of each row. I know that's a little bit confusing but I just wanted to show you quick example there. Now let's have a look at a quick example of a three-dimensional array. So we've created an array, right? And you can see that we kind of have like page one, page two. This is the way to think about it. And then within each page, we have rows and we have columns. So as I said, you know, think two pages each with a 2x3 matrix on them. So let's just print this out. Let's start looking at some of the results. You can see the shape of this is 2 23. So a depth of two or two pages, two rows, and three columns. So this time when I reference axis zero, this is referencing each individual page because it's the outermost dimension that you're looking at first when you look at axis at index zero. So let's have a look what happens when I do the sum now on axis 0 in a three-dimensional array. So let's scroll down here. This is the one that we're looking at. Now notice that the shape that I get is 2 three. So I'm getting a shape that has one dimension less than the dimension of the array that I'm applying this operation on. And what I've done is I've gotten this kind of weird matrix where this eight actually corresponds to the sum of 1 and 7. So I'm looking at elements that are in the same position on a different page effectively and adding them together. When I look at 10, that's 8 + 2. When I look at 12, that's 3 + 9. When I look at 14, that's 10, sorry, 10 + 4. Okay, so you get the idea. That's how it's being added together because I'm doing this on axis 0. Now, if we look at axis 1, we're now going down the rows. So, I'm now going into each page, right? So, I'm looking at each page and I'm going down the rows like I did before. So, this five is 1 + 4. This 7 is 2, sorry, 2 + 5. This 9 is 3 + 6. 17 + 10. And then if I go across the columns, same thing. It's just happening in the pages, right? this time. So, what's happening with my 6 is I've added 1 + 2 + 3. What happens with my 15 is I've added 4 + 5 + 6. With my 24, 7 + 8 + 9, and you get the idea. So, a little bit confusing, but that's how the axes work when you're performing operations. So, let's continue and have a look at some statistical operations in NumPy. I'm not going to go over all of them, but I just wanted to show you kind of a list of some different things that you can do here. So, you can get statistics like the sum, the mean, the median, right? at the standard deviation, variable deviation, min, max, arg min, argmax, all of that kind of stuff. And then you can even do this with 2D arrays as well. So, same thing as before, you can perform these exact same operations, but this time you specify the axis that you want to perform them on. And that's kind of a cool thing about numpy is that no matter how many dimensions you have, you can always perform these operations and you just get a different sized result, right, based on the axis that you're performing them along, which is why I showed you that example previously. Okay, now let's have a look at some linear algebra operations because numpy is also very good at working with linear algebra. In fact, it's designed for that. Like I was showing before, we can have two matrices. We can multiply those together element wise, right, with just an a * b. But we can also perform a dotproduct of various vectors. So if you want to have a dotproduct, you can do np dot and that's going to give you a scalar value. So I'll show you this right now and you can see if we do the dotproduct, we get 32. I'm not going to explain dotproduct right now, but if you've taken a linear algebra course, then you're familiar with what that is. Okay. Now, same thing with matrix properties. So, for example, you can look at the determinant of a matrix. You can look at the inverse of a matrix. You can transpose it. And there's some other more advanced stuff here that I don't even really understand that well, like these vectors, which has been way too long since I've done that. You can do this inside of numpy. I just wanted to show you that those operations exist, right? And you can see kind of the results right here. All right. So continuing, let's have a look at a few useful array methods. Again, just a quick list so that you're familiar with some of the stuff that you can do here. Okay, so sorting. If you want to create a sorted copy of an array, you can use mp.sort. If you want to sort it in place, you can do array.sort. If you have an array with a bunch of duplicates, for example, and you want to convert this into something like a set, so something that has unique values, you can use np. And that's going to give you all of the unique values from the array. If you want to search for something, you can actually use this method, which is pretty cool, where it's going to give you all of the indices where a a specific condition is true. So if I say np.wear array greater than 5. Let's run this. And let's go down here. You can see that it gives us this result. So it says array 5 6 7 8 9. And these are all of the indices, not values, indices where the value in the array is greater than five. And then you can see these are the individual values where that condition is true. Okay, so pretty cool that you can use this where and you can kind of perform the condition directly here. All right, and then if I wanted to actually get all of those different values, you can see that I can go array and then I put inside of here a list of indices. So this is something else you can do inside of numpy. Like I can go 1 2 3 4 like that. If I have a numpy array inside of the numpy array and that then gives me all of the values associated with all of the indices inside of that numpy array, which is kind of what you're seeing right here. Okay, then we have stacking. Like I showed you before, we can do the vertical stacking, horizontal stacking, the column stacking, and you can see a few examples of what that looks like. Okay, now let's get into a few quick practical examples of when you would use numpy. So you can see how these operations are actually useful. So the first example I have is image processing. So for example, let's simulate a grayscale image or kind of like a 2D array with the value 0 to 255 which represents RGB values. So you can see that I've created a simple 10x 10 image even though that's incredibly small. And I've said I want to have some random integers between 0 and 256. So I get valid RGB values. So if I run the code here, you can see this is an example of what the image might look like. This would just be a bunch of kind of random colored pixels. And if we have a look at the statistics of the image, you can see we have the mean, standard deviation, min, max, and I'm just rounding these off to two decimal places where appropriate. Now what I might want to do using dumpy is actually adjust the brightness of the image or the contrast. Now in order to do that I can simply add to the RGB value for every single pixel to make it well brighter. So a function that I could use in numpy to do that is clip. Now the way the clip works is it allows me to take a array and to clip all of the values in that array between some range. So what I'm saying is okay I want to add 50 to this array right? So to every single value here, I want to add 50. But I want to make sure that all my values stay within 0 and 255. So when I do that, it's going to clip the image and make it brighter, but not necessarily by 50 because some pixels may already be at their maximum brightness when I add 50 to them. So if we look down here, you can see it says brightness increased, right? And shows me how much I increase the brightness by. Okay, then let's continue. Here we have another example, right? Where same thing, we clip the image. This time I'm multiplying the image. So I'm actually adding contrast rather than brightness. And when I do that same thing, I can clip it between 0 to 55. And you can see that it increases the contrast by a standard deviation of 77. So there's a quick example of some image manipulation using numpy when you're changing brightness and all of that kind of stuff. Really behind the scenes, you're doing something by manipulating the pixels, which now you kind of understand if you were going to write it yourself in NumPy. Let's have another quick look at an example with some data analysis. Let's imagine we have some student test scores. So the values here are the test scores and each row represents one student, right? If I want to print out all of the scores, can do that. Have a look at it. And then maybe I want to get the mean, right? The average, the maximum score, the uh the student with the best score, right? All of that stuff. So I can start printing out some of those values. So the average score per student. So this goes to each student and says, okay, student one 87, student two 90, student 3, student 4 96, etc. The best student average is 96, right? Right here. And then the student with the best average the index right is three. So if you use this arg max function this gives you the index whereas if you use the max function it gives you the value. Both are particularly useful. Let's look at test statistics overall. So the average score per test 87.6 88.4 89. So there was three tests gave me three averages. The hardest test zero easiest test two. And the way that I did that if you have a look here is I was doing this on the axis right? So, axis zero getting the test averages and then getting some results based on that. And then maybe I want to find all of the students who have scores that are greater than 90. Okay, I can do that. np.all scores greater than 90 axis equal to one. And this is going to give me a boolean mask that has all of the students that essentially have this condition true. So, if I scroll down here, you can see false, false, false, true, false, indices three, and then the scores of excellent students 95, 98, and 96. So I was able to there by using numpy really quickly do some data data analysis sorry this is significantly easier than if I had to write it manually in Python. And then lastly I just created this quick kind of cheat sheet for you that you can reference in this document in case you want to access it from the link in the description which has a bunch of different functions in numpy that you can use. Feel free to have a look at it. Of course there are a lot more but this video is just meant to cover the fundamentals. So with that said I'm going to wrap it up here. I hope that you found this video useful. If you did, make sure to leave a like, subscribe, and I will see you in the next one. [Music]
Original Description
Click this link https://boot.dev/?promo=TECHWITHTIM and use my code TECHWITHTIM to get 25% off your first payment for boot.dev
In this video, you'll learn how to use the NumPy library in Python. If you're interested at all in data science, AI, machine learning, or scientific computing, then NumPy is a must learn. And fortunately, in just a short video like this, I can teach you all of the fundamentals that will get you quite far.
DevLaunch is my mentorship program where I personally help developers go beyond tutorials, build real-world projects, and actually land jobs. No fluff. Just real accountability, proven strategies, and hands-on guidance. Learn more here - https://training.devlaunch.us/tim
🎞 Video Resources 🎞
Juypter Notebook Link: newsletter.techwithtim.net/numpy
⏳ Timestamps ⏳
00:00 | Overview
00:23 | Setup & Install
02:40 | What is NumPy
03:55 | Why Use NumPy?
07:40 | Creating NumPy Arrays
09:56 | NumPy Data Types
11:48 | Multi-Dimensional Arrays
15:23 | Array Attributes
16:06 | Indexing & Slicing
20:22 | Array Operations
22:48 | Array Manipulation
27:23 | Dimensions and Axis
33:24 Statistical Operations
34:08 | Linear Algebra
35:00 | Useful Array Methods
36:41 | Practical Examples
Hashtags
#NumPy #Python #SoftwareEngineer
UAE Media License Number: 3635141
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: ML Maths Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Mastering TypeScript — Understanding the TypeScript Compiler (tsc) from Scratch — Lesson 2
Medium · JavaScript
Stop Overfitting With Basically One Line of Code
Medium · AI
Stop Overfitting With Basically One Line of Code
Medium · Machine Learning
Stop Overfitting With Basically One Line of Code
Medium · Data Science
Chapters (16)
| Overview
0:23
| Setup & Install
2:40
| What is NumPy
3:55
| Why Use NumPy?
7:40
| Creating NumPy Arrays
9:56
| NumPy Data Types
11:48
| Multi-Dimensional Arrays
15:23
| Array Attributes
16:06
| Indexing & Slicing
20:22
| Array Operations
22:48
| Array Manipulation
27:23
| Dimensions and Axis
33:24
Statistical Operations
34:08
| Linear Algebra
35:00
| Useful Array Methods
36:41
| Practical Examples
🎓
Tutor Explanation
DeepCamp AI