Python NumPy Tutorial for Beginners

freeCodeCamp.org · Beginner ·🔢 Mathematical Foundations ·6y ago
Skills: Maths for ML80%

Key Takeaways

Introduces the basics of the NumPy library for Python developers

Full Transcript

How's it going everyone and welcome back to another video. Today we're going to be going through the NumPy library which is considered to be kind of the fundamental package for all scientific computing in Python. So it's a super super important library. It's kind of the base for a lot of the other like major data science libraries in Python. So like Pandas that I've done a video on before builds pretty much like entirely off of the NumPy library. So it's super important and kind of because it's important and because it's this like base the the way we're going to do this video is I'm going to start out with kind of a background information on how NumPy works and I think really having that intuition is helpful for when you actually start writing code with it. So we'll do the background information and then after that we'll jump into all sorts of useful um methods and ways you can utilize this library as far as actual code goes. As always make sure to smash that subscribe button, throw this video a thumbs up, uh follow my Twitters Twittergram, Instagram, Twitter, GitHub too. Uh hit the bell for the notifications. Throw this another thumbs up. Yeah. To begin NumPy is a multi-dimensional array library. So what that means is you can use NumPy to store all sorts of data in one-dimensional arrays, two-dimensional arrays, three-dimensional arrays, four-dimensional arrays, etc. Um and so the common question you kind of ask or I'm commonly asked when you know you first bring up NumPy is why do you use NumPy over lists? So the main difference comes from the speed. So lists they are very slow. Meanwhile NumPy is very fast. And so why are lists slow and NumPy fast? Well, one reason is because NumPy uses fixed types. So what that means is imagine we have this 3 by 4 matrix, three rows, four columns. And it's all integer values, and we're going to kind of look at how those integer values differ between NumPy and lists. So, let's just zoom in on that five that's there in that matrix. So, our computers, they don't see five, they see binary that represents five. And so, this is the number five in binary, and it's eight bits, which makes up a byte. So, our computers read information in bytes. So, when we use NumPy, this this one bit five is actually, by default, going to be casted to this int32 type, which consists of four bytes. Uh and so, it represents five in a total memory space of four bytes. So, int32, and you also you can even specify. So, by default, it's int32, but you could even specify that you didn't need all four bytes to represent this value. So, you could specify within NumPy that you want to maybe an int16, which is 16 bits or two bytes. Or even if you had really small values, int8, which is just a single byte. On the other hand, uh with lists, there's a lot more information you need to store as an integer. So, in in lists, lists use a built-in int type for Python. And so, that built-in int type consists of four different things. It consists of the object value, which, you know, has its own bits associated with it, object type, the reference count, how many times has that integer has been specifically like pointed at, uh and the size of that integer value. And so, if we break this up into the actual binary that it represents, we can take the object value and that's represented as a long, which is like eight bytes. The object type, same deal. Reference count, same deal. And then the size I believe is a little bit smaller. Um I think it's only four bytes. But as you can see, that's a single integer within lists using the built-in int type, it requires a lot more space than NumPy. So basically the takeaway from this is that because NumPy uses less bytes of memory, um the computer can read less bytes of memory quicker, obviously. So it's faster in that regard. Uh another reason that I didn't specifically say is that um when we're iterating through each item in a NumPy array, we don't have to do type checking each time. So in Python built-in lists, you could have a list of like an integer, then a float, then a string, then a boolean. Uh and you'd have to check each element you're looking at what type it is. But NumPy we don't have to do that. So another reason it's faster is that there's no type checking when iterating through objects. Moving on, another reason that NumPy is faster than lists is because NumPy utilizes contiguous memory. So what that means is imagine that this kind of array-like structure is our computer's memory. So we could store information in any one of these memory blocks. So if we had a list, the way that that would look in a list's memory is that our list would be kind of scattered around. So maybe we have an a list that takes up eight memory blocks. The thing is that these memory blocks aren't necessarily next to each other. So you have some information here, you have some information here, you have a good amount of information in here. Uh then you skip a block here, here, and skip two blocks. So you have some information here. So it's all kind of scattered around. Um so, kind of if you have an eight item array, what that looks like is that that array is actually just it or that list is just um containing pointers to the actual information that's scattered around our computer's memory. Um And so, it's just the all the information is not right next to each other. It kind of you have to bounce around your computer's memory a bit, and it's not super super fast to like rapidly go through and uh kind of potentially perform functions on all items at the time or subsets of the items. Uh NumPy array, however, uses contiguous memory. So, all eight blocks in this case would be right next to each other. And this has all sorts of advantages. Um and also just to mention real quick, you'd also I kind of have to have to store somehow where the start of that memory is, and then like the total size and the type of memory block. Uh but, it's a lot easier than this kind of pointer structure that's up here. And so, the benefits of NumPy using this contiguous memory are a couple of different uh things. So, the first ben- benefit is that our CPUs or our computers have these SIMD vector processing units. And so, when this memory is all like right next to each other, we can utilize this unit. And basically, what SIMD stands for is single instruction multiple data. So, we can like if we have to do an addition of like a lot of values, instead of just doing one addition at a time, we can use this SIMD vector unit and basically perform computations on all of these values at one time. Uh so, it's quicker in that regard. Another reason it's quicker is that we more effectively utilize our cache, so our kind of our quicker memory in our computer. Basically, if we load in all these values, uh we can keep them close to where we need to access them. Um and like perform all sorts of operations, while as in the list's case, you'd maybe load in like half of this, but then this other half, because it's scattered around in different places, you'd have to like go back and like reload that in uh to your cache, like you know, just be overall slower, because you'd have to do more like longer memory lookups within your computer. Okay, so we're kind of going over some of the performance benefits, but how are lists different from NumPy? Well, lists we can do insertion, deletion, appending, concatenation, etc. And we can also do those same exact things in NumPy. I guess the big difference though is that within NumPy, we can do all of that, and we can do lots lots more, and we'll see the lots lots more uh throughout the video, but as a simple example, imagine we have these two arrays. One thing that we can do that's really nice in NumPy is that if we try to multiply these one item at a time, uh we could do that in in lists. You couldn't multiply one and one, three and two, uh five and three, um etc. But when we do the exact same computation within NumPy, it allows us to do these, you know, single value like item-wise computations, which is pretty neat and pretty useful. So that's one example, and we'll see a lot more throughout the video. Uh so applications of NumPy, there's all sorts of applications. I think the first one, the kind of the first one that comes to my mind, is that is a kind of a a MATLAB replacement. You can do all sorts of mathematics with NumPy. And I think I should say that I think the SciPy library has even more mathematics like functions and whatnot. So, if NumPy isn't cutting it for you, try to look through the SciPy documentation. You might be able to find even more. But, yeah, it's pretty powerful the math that NumPy can do. Um it's useful in plotting. It is uh the back end of many different applications. So, Pandas, uh which I've done a video on before, it is just like the core component of Pandas library. It really allows Pandas to work. Uh if you've seen my Connect 4 uh how to program that video, I used NumPy to store the board. And then, in future videos that I'm going to do, uh you can you can actually store images uh through NumPy. So, like PNG images, you can use NumPy to store all the information and like do all sorts of cool stuff that I'll uh post future videos on. Uh let's see. Also, another, I think, useful reason to know NumPy is that it's kind of like pretty important for machine learning applications, um both directly and then also kind of indirectly because uh one of the key libraries or key kind of concepts you learn with machine learning is the idea of like tensors. And tensors are pretty connected to kind of like the tensor libraries are pretty similar to like the NumPy library. It's just a way to store all sorts of values. So, knowing NumPy will help you uh kind of be able to do some stuff with machine learning. All right, to get started with the code, the first thing you're going to want to do is import the NumPy library. And just so we're on the same page, I'm using a Jupyter notebook to use uh to code this up, but you can use whatever editor you prefer. Also, all of this code that I'll be going through will be on my GitHub, and the link to that will be in the description. Okay, so import NumPy as np. If that works for you, great. If it didn't work, you'll have to do a pip install. So, you can go ahead into your terminal and type in pip install numpy. And so, uh it's already installed for me. So, and if pip doesn't work for you, try pip3 install numpy. That should work. So, the first thing that's important to know is how to initialize an array. So, we'll just say that A equals np.array. And then within this, we just basically pass in a list. So, 1 2 3. This would be a one-dimensional array containing the values 1 2 3, as you see. Uh and you can go ahead, if you're not using Jupyter notebooks, and print A. Okay, cool. So, we could also initialize a little bit more complex arrays. So, we could do like a 2D array of floats, and I could do that following way. Uh we're going to have a list within a list. Uh so, here's some floating values, and then uh we're going to make this two-dimensional. So, here's some more float values. And let's go ahead and print B. Cool. So, now that we know how to initialize arrays, uh and you can keep doing this. Like, I can nest list within a list within a list to create a three-dimensional array, etc. Um some other useful things to know about this is um how do how do you get the dimension of your numpy arrays. So, if I did A. dot number dimensions, uh so, this tells me that it's one-dimensional for A. And if I did B. ndim, it would be two. Shape is another important function, so get shape. If we do the first one, a.shape, this is all we have to do. It's a vector, so it's only going to tell me the one dimension because it only has one dimension, so it's size three. If I do b.shape, it's going to tell me the rows and the columns, so this is two rows and three columns, so this should print out two by three as it does. Okay, other things we want to know how much memory our NumPy arrays take up, so we can get the type and you also get the size, so if we want to get the type, we do just a.type. Sorry, a.datatype. int32 by default. So, even though these are small values by default, it specifies that it should take up four bytes or be an int32. If we wanted to specify what type we wanted to store as, so maybe we knew that we didn't have many like big values, so we could do like an int16, and so that would take up less size, and you can see the difference in size in a sec. So, right now it's int16, and if I want to see the size, there's a couple different I guess important functions with this. We can do a.itemsize, so this should tell me two bytes. As it does. If we left this as an int32, it will tell me four bytes down here. As it does. You can also do, I think, um the total size. I guess a.size is the total number of elements, so the total size would be a.size * a.itemsize. Another way to do that is I think just number of bytes. As you see, that's the same thing. And um you can also do this with b. So like b.itemsize. B's are floats, and I believe that this is an 8-byte type. So if I do b.itemsize, as you see, yeah, it's eight. So floats are going to be bigger than um floats are bigger than integers usually, unless you define this as like an int 64. And so yeah, you really I usually don't even worry about the data type too much. I don't specify it, but if you really want to be efficient, try to specify this so that it fits all your data, but um if yeah, I guess it fits all your data as tightly as possible. All right. So now that we've gone through some of the basics, let's actually show how we access {slash} change specific elements, rows, columns, etc. So imagine we have the array It's going to be a two-dimensional array, so I'm going to make this kind of a long, and you'll see why in a second. Okay, so this is a 2x7 array. If I print that out, okay, and I can prove that it's a 2x7 by doing a.shape. That's just a reminder. Um so what if we wanted to get this a specific element? Well, to do that, we can use this notation of row, {comma} column. So this is the row index, this is the column index. So I could just do something just like a um let's say I wanted to get this 13 right here. Well, that would be in the second row, but because we start Python indexing at zero, it'd be the first row, and then the 0 1 2 3 4 5 fifth column. So, yeah, that gives us the 13 as you see down here. And one thing that's kind of cool is you can also use the negative notation, similar to lists. So, I could also say the negative second element would be 13 as well because this would be -1 and then -2. So, there's a couple different ways to do this, but we'll stick with the first one. Okay, let's say we wanted to get a specific I can't spell. Uh row. Um that's pretty straightforward as well. So, in this case, if we wanted the first row, we would do zero, and then because we want all columns, we use the basic slice syntax, similar to lists. And I can just do a single column, and that will get me everything in the row. That's nice. What if we want a specific column? Well, if you know how to do rows, you probably know how to do columns. A Let's say we wanted this row right or this column right here, three and 10. That would be all of the rows and then the 0 1 2 column. That gives me the 3 10. And from here, we can do even some more like tricky stuff. So, um uh I want to just say getting a little more fancy. Uh and we have the start index. This is just a reminder. Start index, end index, and then finally the step size. So, if I wanted to let's say get between the numbers two and six, every other element, so two, four, and six. Well, to specify that, I would do Well, we want the first row, and then I want to start at the first element the two I actually screwed that up. It should be one. So, I start at the two, then I want to end here at the six which is the uh it's exclusive, so that would be uh I want to actually go to the sixth element, and then I want to step by two because I wanted 2 4 6, so I do 1 6 2. And that gives me 2 4 6. And I can also use the negative here and do like negative two. Uh what happened there? Oh, shoot. That was going backwards. I didn't want to change it there. I wanted to change the six to be negative two. Uh okay, it's exclusive, so I wanted this to actually be negative one. A little bit more of a fancy way to do that. Okay, so that's how you access elements. And then if we wanted to change something, it's pretty straightforward, too. Let's say I wanted to change that 13 that I originally accessed. Well, I can just do like 20. If I print out A now that original element that was 13 is now 20. And you can do the same thing for um series of numbers. So, like for an entire column, let's say we wanted to replace this 3 10 column I would do something like A um colon two equals let's say I wanted it to be all fives. I could start like this. And as you see, it's all fives, 5 5. And then if I wanted it to be two different numbers, you just kind of specify the same shape as what you've subsequence, so it'd be like 1 2. So, now you see that we have a 1 2 in that's position. Really quickly, let me just show a 3D example. If I had a 3D, so we'll say B equals NumPy array of all this. And if I print B, um So, if we want to get a specific element here, uh the recommendation I have is work outside in. So, work outside in. So, let's say I wanted this four right here. Well, the farthest outside would be which one of these do I want? And I want the first set, so I want this area right here. So, if I wanted that, I would do B zero. And then, now that I'm in here, I want the second row. So, I want the three four. So, that would be one. And now that I'm within this, I want the first uh or the second, yeah, the second element, but the first index, like that. So, that gives me the four. And you can do similar type stuff with like the colons in here. So, each one of these dimensions that you're indexing, you can be all fancy with uh how you access elements. So, I can do something like this. And, you know, get three four seven eight. And you can kind of play around with this and see how changing different things changes what you get. And if you wanted to replace in this case, um basically just have to create a subsequence that's the same dimension. Um so, if I did B one, this that gives me three four seven eight. Let's say I wanted to change that to nine nine eight eight. As long as it's the same dimension, it's going to work. So, nine nine eight eight. But I try to like do something like nine nine nine eight eight, it's going to have an error. All right, so that's the basics of indexing. I think at by the end at the end of the video, I'll do a little like um challenge problem on like some advanced indexing. So, look at the end of the video for that. All right, next let's go through how to initialize all sorts of different types of arrays. So, to start off, let's uh initialize an all zeros matrix. Um and to do that, there's a nice built-in function called np.zeros. And we can first I guess actually all you really need to do is specify a shape. So, I did like np.zeros 5, it's going to just give me a vector of five length five. But, I also can pass in a more complex shape. So, if I wanted it to be like a 2x2 or 2x3, let's say. As you see there, I could do three-dimensional 2x3x3. Could even do four-dimensional if I wanted to. 2x3x3x2. Yeah, it gets pretty crazy. But, yeah, uh you can do all sorts of zeros with that. Um next, let's do an all ones matrix, pretty similar to the last one. np.ones of let's say 4x2x2. And there you go. And you can also specify the data type here. So, if you wanted like all ones but in 32, uh you can go ahead and do that. So, all ones, uh all zeros. However, you might want to initialize a matrix that's not uh ones or zeros, any other number. So, for that, you can do np.full. And this one takes in two parameters. So, the first is the shape. So, 2x2, and then the next is the value. So, if I wanted all 99s, then it's a 2 by 2 with 99. Another useful And you can you know, that has a data type, too. So, if I wanted that to be float 32, there you go. And I'll put a link in the description to a list full of these like array creation routines. Uh useful to know is there's also this full like um there's this full like method. And basically that just allows us to take a shape that's already built. So, let's imagine we wanted to reuse that um this array that we I guess had in in the last section, A. I think it's still loaded in. Let me just make sure. Well, I can pass in and make a array that's the same size size uh of fours, let's say, by doing full like. Or actually, I think I don't even have to pass in a.shape. I just have to pass in A. There we go. If I didn't use full like, I would have to do full of a.shape. I don't know if that's that useful for you, but I guess it's potentially good to know. Okay, next one. Let's say we wanted to initialize a array or a matrix of random numbers. So, random decimal numbers to start. To do that, we do np.random.rand um and we specify the shape. So, let's say 4 by 2. Uh no, what did I do wrong? Uh Huh. I'm actually confused. Tuple state Oh. Okay, yeah, this one's a little bit different. So, instead of passing in a tuple, you can pass in directly the integers you want to the integers of the shape. So, it's a kind of a weird thing to remember. Uh, so if I did the 4 by 2, this way I would actually pass it in like that. And when you get errors like this, often times you can just do a quick Google search and realize that that's what you need to do. So, I can even keep going. So, I can do a 4 by 2 by 3 of random numbers between 0 and 1. Uh, I could also pass in something like a.shape. I don't know if this would work. Let's try. Yeah, so if you wanted to pass in like a shape, you can do random sample. a.shape And that, now you see gives us the same shape as our A from up here. So, yeah, rand, and then there's random. sample, which is another method. We'll keep it as rand of 4 by 2. Okay, what if you didn't want just decimal numbers, but you wanted random like integer values? Well, to do that, we can do random.rand I see. I'm getting np.random.randint And this one we're going to pass in the start value, or if you don't specify a start value, it's going to just start at zero. Um And so, if you don't specify a shape, then it's just going to do one number. So, let's say we wanted a 3 by 3. Uh, what did I do wrong? And this is not shapes, actually size. And yeah, all the documentation has these like, you know, you're not expected to memorize all of these things. What I think it is helpful to see is that you see that you can do these types of things. So, like when you're thinking about a problem, you can like kind of point back like, "Oh, I remember that that's possible." Uh, maybe do a Google search on how to get it. But, yeah. random.randint 0 to 7 uh, with size 3 by 3 is here. Could also specify like a different parameter. So, let's say I wanted 4 to 7. And I think And if I keep running this, too, it's kind of cool. You can see it changing. And so, it looks like that 7 is exclusive. So, if I wanted to include 7, I would stop a little bit later. You could also throw in like negative numbers here. Cool. All right. Uh, what else other than the random integers? Maybe you wanted to do like the identity matrix. You can do identity of three. This one only needs one parameter because the identity matrix uh, by its nature is going to be a square matrix. Um, what else is useful? Maybe it's useful to repeat a array a few times. So, to do that, you could do say we had the array um, 1 2 3. Let's say I wanted to repeat that three times. Pass in the array you want to repeat, and then let's print R1. See what happens. Okay. And then if I specify the axis equals zero, oh, no, know if do anything. Uh, what I can do is make this a two-dimensional array. I think because it was a vector, it didn't do what I wanted to. What I wanted to do is 1 2 3 or 1 2 3 1 2 3 1 2 3. So if I wanted to do that now I made this a two-dimensional array and it will repeat the inner part on the zeroth axis so that will be basically making it rows. There you go. So if I made this um equal to one, that's going to be what we saw before. Cool. Okay, so next here's a picture of an array I want you to try to initialize using everything that we kind of just went through. So all these different methods. So look at this picture and then try to put it together without just manually typing out the all the numbers cuz you could imagine like this isn't too too big, but if you got into a matrix that was massive, you'd want to know how to build it up using these kind of like fundamental concepts. Okay, so here's the solution to that. So I can do output equals I'm going to start with making everything ones. So ones and it's going to be a 5 by 5 of ones. Um print output. So this is what I have now. Okay, and now basically what we're going to do is fill in this middle part with zeros. So Z I'm going to just say equals np.zeros and that's going to be a 3 by 3. And if I print Z, now we have this. Now what I can do is fill in the middle element, so that's 1 1 with a 9. And now if I print Z, we get this. And then finally, we need to replace the middle part of the ones matrix, so output the middle part, so that's going to be the first row to the third row. So I want the first row to the third row, and then I want the same thing with columns cuz it's the middle. First column to the third column. And actually, this is exclusive value, so it needs to go to four. And that's going to equal um Z. And now what happens when I print output is yay, we got what we're looking for. And actually, one thing that I think is nice is instead of using four, I could also do -1. So basically, the from the first element to the last element. Uh do that. And as you see, it didn't change. This last initialization I want to go through, I guess, is a little bit different. It's uh over on the concept of copying, but something you got to be really careful about. So I'm just going to quickly mention it. I want to do exclamation points. There we go. Um okay, so imagine we have two arrays or we have one array. Uh let's call it A. And so, now A is just a normal array as you can see. And let's say we want to make B a direct copy of A. So now I'm going to just do B = A and then print out B. And as you can see, it's still 1 2 3. And so I'm like, okay, I have this copy, like things are cool, it's fine. I I want to change the first element in B. So, I'm going to do B0 equals 100. Here's the issue. I print out B. Looks good. The issue lies in if I print out A, look what happens. I just printed out A and A now has a 100 instead of the 1 2 3 that I initially set it as. And that's because when we did B equals A, we just set that the the variable name B points to the same thing as A does. We didn't tell like NumPy to make a copy of what is the contents of A. So, that's the that's why though because we're just pointing at the same exact thing that A is pointing, when we change the value, it also changes the value of A. So, if we want to prevent that, we can use this dot copy function. Oh, sorry. I shouldn't do it yet. Um B equals A.copy. And then when we run this cell, as you can see, 1 2 3 still there because now we're just copying the contents of what's in A. And if I print B, it has the 100 200 100 2 3. Okay, so one of the big uses of NumPy is all the math capabilities it offers. Um so, just to kind of show some of that, um one thing that it can do is element-wise um I'll just make this four values. Element-wise addition, subtraction, element-wise I guess arithmetic. So, here we have A. Print out A. Um and if I wanted to do something like A plus 2, um adds two to each element. You can do A minus 2. Subtracts two from each element. A times 2. As you can see, uh A divided by two. Um divides everything by two. Uh one thing to note with And you can also do stuff like uh A plus equals two. So, now if I printed out A in this column, uh it's going to be two plus everything. It's kind of cool. You can do like the same type of math that you can do in uh Python. You could also create another array np. array and that's like, let's say uh 1 0 1 0. And I could do something like A plus B. That should be 2 2 4 4. Oh, and because I added If I rerun this, okay, 2 2 4 4, like we expect. Uh so, all sorts of useful things. You could even do like A to the second power. 1 4 9 16. And that might have made it a bigger data type, I'm not sure. Um cool. We can do stuff like take the sign of all the values. So, let's say we had A, we do np.sign, pass in A. Gives us all the the sine wave of all those values, which you know, and you have like cosine of all those values, all sorts of useful things that you can perform on an entire array or entire matrix all at once. And if you want all of the different things that you can do, um I'll paste in a link here. This will all be part of the Also, as I mentioned before, I have this on my GitHub. So, if you look in the description, you can find this exact notebook. Um so, yeah, look up the routines right here for math, all sorts of cool stuff. All right, moving on. We're going to still be in math, but let's jump into linear algebra type stuff. So, here we are doing linear algebra. Okay, so this is kind of like the basic all sorts of functions you can do on elements. Uh linear algebra, so this is like really I feel like when I'm using MATLAB, it would be doing these linear algebra type stuff. So, let's say we have two matrices. And the big difference with linear algebra is like we're not doing element-wise. So, like in this case this B we're doing element-wise computation. So, like if you like A * B uh in you know, linear algebra you're trying to multiply matrices and that's a different process. So, let's say we have um two matrices. We'll have A and I'm going to use the syntax we learned about earlier. I'm going to say this is a 2 by 3 matrix of all twos. Um actually, let's make this 2 by 3 matrix of ones. So, we have A as you can see and then we'll have um B which is equal to np.full it's going to be a 3 by 2 and it's going to be a value 2. So, if I print out B now we have this. And if you remember linear algebra, you have to have the columns of the first matrix be the equal to the uh rows of the second one. So, as you can see this has three columns and this has three rows, so we're good there. So, we would multiply this row by this column um and you know, you do the the process of matrix multiplication. We're not going to walk through the whole thing, but we should end up with a 2 by 2 matrix at the end if we want to do um matrix multiplication. And it doesn't just automatically have for if you try to do A * B, it's not going to work because these are different sizes. So, what we can do is NP has a matrix multiply function. And if I pass in A, then pass in B, we get 6 6 6 6. Did I say enough sixes? I don't know, but uh, yeah, it uh, multiplied those two matrices. Um, you know, and if I try to switch up this dimension in the middle, uh, it's not going to work because it's now incompatible. Uh, yeah, that's matrix multiplication. You could also want to do maybe some other stuff with matrices. So, let's imagine I wanted to create the or to find the um, let's say determinant of a matrix. So, we could, as a sanity check, you know, make C equal the identity matrix. And if you are familiar with linear algebra, you know that the identity matrix has a determinant of one. Um, so, if I do linear algebra {dot} determinant of C, we should get one. 1.0 as we get. So, find the determinant. You know, there's all sorts of other good things like eigenvalues, uh, you know, the inverse of a matrix. So, what what do you multiply by a matrix to get the identity matrix? Um, and so, yeah, all sorts of good stuff on that. Like, I guess I'll do, hm, and if you want to have all this information on the other types of linear algebra stuff, uh, here is some useful information. Uh, definitely go to this link. And as I I've said a couple times in this video, uh, this notebook is on my GitHub page, so you can find all of this there. But, yeah, there's so many different things that you can do with matrices and linear algebra using the NumPy library. Okay, continuing onwards, let's look at um some statistics with uh NumPy. So, kind of the easiest things we might think about when we think about Sorry. Um statistics, there's like min, mean, max, etc. So, let's say we have this array. Um so, let's say we want to take the min of it. You can just do np.min of stats. That's going to give us the one that you see there. You can do np.max of stats. Um six. You could also do it on like a row basis. So, if I said axis equals one, that's going to give me the min of the first row and the min of the second row. All right, maybe this is a better way to see it. If I said axis equals zero, well, it's going to give me all the values that are up top here because those are all the the mins. Um so, yeah, you can do all sorts of cool stuff with min and max with this. Um same thing with max. Let's say axis equals zero. Um axis equals one. Three and six is the biggest value. Three is the biggest value and the six is the biggest value. You can also do np.sum of stats. If I do it just as is, it's going to sum up all of the elements in the matrix. And then, same thing, I can do row or column. So, axis equals zero is going to add up all these terms going downwards. Next, let's talk a little bit about reorganizing arrays. So, kind of the I would say the key method within reorganizing arrays, so if I have the array, I'm going to call it before. And let's say that that is equal to this value right here. So, we have before. I'll print before out. Looks like that. So, let's say we wanted to instead of this shape that it currently has, which is a 2 by 4. Um let's say we wanted to make it a I don't know, a 8 by 1 or something. Or maybe a 4 by 2. Um or a Yeah, all sorts of different things we could do. I'll start with 8 by 1. So, we have before. Uh and if we wanted to make it something else, we can do after equals uh before.reshape. And then we pass in the new size we want it to have. So, if we wanted it to be an 8 by 1, you can pass it in like that and you can print out after. As you can see, it's an 8 by 1 now. I could also say maybe I wanted it to be a 4 by 2. So, now you got that. You could even pass it in as a 2 by 2 by 2. As long as it has the same amount of values, like it's fair game. So, as you see, 2 by 2 by 2 still works with the reshape. Uh what doesn't work is like if I wanted it to be a 2 by 3, um the values don't fit in. So, when you get errors with using the reshape, it's usually because there's a mismatch between the shape you're trying to resize it to versus the original shape. Uh moving onwards, let's look at vertical stacks. So, vertically stacking uh vectors or matrices. And you know, dimensions are important in vertical stack as well. So, vertical stacking matrices. Let's say we had these two arrays. If wanted to stack um you know, 1 2 3 4 on top of 5 6 7 8, I can do np.vstack and I can pass in V1 V2. And as you see now, they're part of the same matrix and 1 2 3 4 is on top of 5 6 7 8. What I can even do is keep passing these in. So, let's say I wanted like three copies of this 5 6 7 8 and only one copy of this or I could inter twee- weave them. That's a vertical stack. Horizontal stacks are pretty similar and also note here like I can't do that. The size is mismatch Mitch- miss- match. So, yep. Horizontal stack very similar. Um let's say we had um we'll use a sub notation we've learned before. We had these two matrices. Um so, if I printed out H1, you got like that and then H2 is this. Well, I want H2 to be on the back of H1, I can just do an np.hstack horizontal stack and that will be H1 and H2. And that did not work because I did not surround this in parentheses. Either parentheses or brackets, I think they both work. Yep. There you go. So, now we've horizontally stacked the zeros on top of the or to the right of the ones. All right, let's get into some miscellaneous things. Uh so, first off, let imagine you have uh you know, some sort of text file with all sorts of data and for whatever reason you choose, you don't want to use pandas, but you want to load all of that data from that file into a NumPy array. Well, we can do that without too much trouble. So, I have this um text file that I created as you can see here. This is on my GitHub page. Uh you can download it there. This is just really simple data, but it shows kind of what you can do with it. All delimited by commas, called data.txt. Um what I can do is I can do MP and I can use this function called genfromtext. And I pass in the name of the file, which is data.txt, and then I pass in a delimiter, which is the separator, and that's a comma. And if I do that, uh you see that I get that data that I just showed you. Um You get that. I can increase the zoom here. I get that um as an array. So, that's pretty nice. So, uh I'll just call this file data equals and file data Yep. Uh one thing you notice though is it automatically cast it to a um float type. And what if I wanted it to be an integer? Well, I can do another function as type, which basically copies all the data into a whatever format you specify. So, I'll say int32. And as you can see, now all of this stuff is here. And if I go ahead and print file data now, it is back to what what we had originally. And the reason it's back is that this actually makes a copy because the float type and the int32 type are different sizes. It can't just like in place um copy everything. It doesn't really make sense to. So, if I did file data equals file data uh {dot} astype int32 and then printed out file data, as you can see, now it's all floats. So, that's how you load data from a file, and you can change up this delimiter based on how your data is split, but I think that this genfromtext will handle your uh new line breaks properly if that's how it's formatted. Um write in the comments if you have any questions about this. Okay, the second thing I want to go through is um what happened there? I didn't want that to be markdown. Uh Um the second thing I want to go through with this miscellaneous section is uh some advanced uh indexing. So, there's some really cool stuff you can do with NumPy. Uh I'm going to say Boolean masking and advanced indexing. So, what can we do here? So, let's say I wanted to learn where in file data the value is greater than 50. So, if I just type in file data greater than 50, uh it's pretty cool that you get uh false or true based on whether that specific location was greater than 50. So, as you can see, there's four falses and then a true. If we go to our data, four falses and then 196 is in fact greater than 50. So, that's like one way, and you can do all sorts of cool stuff with this. Like, you could do greater than or equal to, you know, all sorts of different combinations. Uh one thing that's pretty neat is you can do file data and then you can index based on where it is greater than 50. Uh and by doing this, you grab only the values that actually have a value greater than 50. So, that is pretty cool. And kind of the reason that this right here works is that one thing I did not mention until now um is that you can can index with a list in NumPy, which is pretty cool. So, if you have the array uh, 1 2 3 4 5 6 7 8 9 and I wanted, let's say, the zeroth spot, the second spot, and then the last spot I could do np, or let's say that this is A. I could do A of 0 1 and, or I wanted 2 3 and 9, so I would do 1 2 and then 8. And as you see, that gives me 2 3 and 9. I passed in a list and it indexed those spots. So, basically, it also works if you like had trues and falses, it like basically, if it is true, then it knows to take it. If it's false, doesn't. So, that's why this up here works. Um We could do all sorts of other things. So, let's say um, I wanted to figure out if any value in any of these columns was greater than 50. So, I can do a np.any file data greater than 50 and the axis of 0. So, that should tell me like, if we looked downwards on all of these, are any of the values greater than 50? Let's see what happens. So, false, false, false, false, true. That's correct. True. These two values are greater than 50, this even though this one isn't. Um false, true. Yeah, true, true, false, true. Cool. So, this is telling us, yeah, where what columns have a value greater than 50. And I can also do np.all. And as you can see, there's less trues in this case. I think the only time that all of the values are greater than 50 are right here. Yeah, you see there's one true in the fifth spot, which corresponds to this right here. Uh what else can we do with this? Um if you did axis equals one, it's going to be the rows. You can also do multiple conditions, so I could do like uh I want file data to be greater than 50 and let's say file data is less than 100. And this syntax is very similar to pandas here. And as I said before, NumPy builds is what's the base of pandas, so it makes sense. Uh no. The fire truth value of an array with more than one element is ambiguous. How do I do this? Think if I do something like this, it will work. Not positive. Let's see. Yeah, whatever. No, I need to end it. Yeah, cool. So, this is all the values that are greater than 50 but less than 100. So, like the first true should happen at the sixth spot. 1 2 3 4 5 6. As you see. Uh and I could do something like all the spots, if I wanted to make all of this not, so this means not greater than 50 and less than 100. These are going to be the reverse of what we just did. So, yeah, now the um sixth spot is the first false. So, this meant not. So, yeah, all sorts of cool stuff you can do with this Boolean masking and advanced indexing. I mean, yeah, any sort of like condition. I'll put a link and some more information about this. All right, quick little quiz on uh indexing. This is kind of using all sorts of advanced stuff that you just learned in that last section and include and then also like some of the original stuff. So, first question, uh basically pause the video after I ask it and then try to figure out what the command would be. So, we have this matrix, and how would you index this part of the matrix? So, this is the um second and third row and the first and second column or zeroth and first column, so it looks something like this. Rows, columns. Next question, how would you index this? This is something we haven't done before, but you potentially with that last section might have an idea. If not, no worries. So, to do this one, um you need to use two different lists within your uh indexing. So, it's going to look something like this. We need the zeroth, first, second, and third row. And then the first, second, third, and fourth columns. That's what that is. And then final question, how would you index this? This is like also something we haven't immediately looked at, but you might be able to get, especially with that last one. Take a second. All right, that would look something like this. Where you get the zeroth, fourth, and fifth rows zero. Fourth and fifth rows, and then you want columns three onwards. So, this would like three onwards works. You could also do like three to five. You could also do three or like a list of three, four. But, yeah, that's one way to do it. It's a fun little quiz. I don't know. It's I guess good to revisit this type of thing and like think critically about it. All right, thank you guys very much for watching. I think this is all I have for this video. Peace out.

Original Description

Learn the basics of the NumPy library in this tutorial for beginners. It provides background information on how NumPy works and how it compares to Python's Built-in lists. This video goes through how to write code with NumPy. It starts with the basics of creating arrays and then gets into more advanced stuff. The video covers creating arrays, indexing, math, statistics, reshaping, and more. 💻 Code: https://github.com/KeithGalli/NumPy 🎥 Tutorial from Keith Galli. Check out his YouTube channel: https://www.youtube.com/channel/UCq6XkhO5SZ66N04IcPbqNcw ❤️ Try interactive Python courses we love, right in your browser: https://scrimba.com/freeCodeCamp-Python (Made possible by a grant from our friends at Scrimba) ⭐️ Course Contents ⭐️ ⌨️ (01:15) What is NumPy ⌨️ (01:35) NumPy vs Lists (speed, functionality) ⌨️ (09:17) Applications of NumPy ⌨️ (11:08) The Basics (creating arrays, shape, size, data type) ⌨️ (16:08) Accessing/Changing Specific Elements, Rows, Columns, etc (slicing) ⌨️ (23:14) Initializing Different Arrays (1s, 0s, full, random, etc...) ⌨️ (31:34) Problem #1 (How do you initialize this array?) ⌨️ (33:42) Be careful when copying variables! ⌨️ (35:45) Basic Mathematics (arithmetic, trigonometry, etc.) ⌨️ (38:20) Linear Algebra ⌨️ (42:19) Statistics ⌨️ (43:57) Reorganizing Arrays (reshape, vstack, hstack) ⌨️ (47:29) Load data in from a file ⌨️ (50:20) Advanced Indexing and Boolean Masking ⌨️ (55:59) Problem #2 (How do you index these values?) ⭐️ Links with more info ⭐️ 🔗 NumPy vs Lists: https://www.youtube.com/channel/UC_mmB9WkzXQAQmwj6EPmXQw 🔗 Indexing: https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html 🔗 Array Creation Routines: https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html 🔗 Math Routines Docs: https://docs.scipy.org/doc/numpy/reference/routines.math.html 🔗 Linear Algebra Docs: https://docs.scipy.org/doc/numpy/reference/routines.linalg.html -- Learn to code for free and get a developer job: https://www.fre
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60

← Previous Next →
1 React: Production Server Setup Part 2 - Live Coding with Jesse
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
2 cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
3 Browser history tutorial - Beau teaches JavaScript
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
4 Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
5 React: Parameterized Routing with Next.js - Live Coding with Jesse
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
6 React: Dealing with jQuery Issues - Live Coding with Jesse
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
7 setInterval and setTimeout: timing events - Beau teaches JavaScript
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
8 Browser and Device Testing - Live Coding with Jesse
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
9 Last Minute Updates - Live Coding with Jesse
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
10 Post Launch Updates - Live Coding with Jesse
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
11 React: Setting Up Google Analytics - Live Coding with Jesse
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
12 React: Masonry Layout - Live Coding with Jesse
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
13 Load Balancing Digital Ocean Droplets - Live Coding with Jesse
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
14 try, catch, finally, throw - error handling in JavaScript
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
15 Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
16 Graphs: breadth-first search - Beau teaches JavaScript
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
17 React: Masonry Layout Part 2 - Live Coding with Jesse
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
18 React: WordPress API Live Search - Live Coding with Jesse
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
19 Creating WordPress Custom Post Types - Live Coding With Jesse
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
20 Dates - Beau teaches JavaScript
Dates - Beau teaches JavaScript
freeCodeCamp.org
21 Miscellaneous Front End Updates - Live Coding with Jesse
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
22 Merging a Pull Request from GitHub - Live Coding with Jesse
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
23 React + Prettier + Standard JS - Live Coding with Jesse
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
24 React: Sortable Responsive Table - Live Coding with Jesse
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
25 Geolocation Sorting by Distance - Live Coding with Jesse
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
26 Tradeoff Matrix - Agile Software Development
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
27 The Definition of Ready - Agile Software Development
The Definition of Ready - Agile Software Development
freeCodeCamp.org
28 Getting first React job without experience - Ask Preethi
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
29 React: Google Analytics Click Tracking - Live Coding with Jesse
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
30 Submitting a PR to an Open Source Project - Live Coding with Jesse
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
31 Should I go back to school to get CS degree? - Ask Preethi
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
32 Hero Section CSS Changes - Live Coding with Jesse
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
33 Working Agreement - Agile Software Development
Working Agreement - Agile Software Development
freeCodeCamp.org
34 A day at Pennybox with Co-Founder Reji Eapen
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
35 React: Sorting and Filtering Data - Live Coding with Jesse
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
36 React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
37 React: Building a New UI - Live Coding with Jesse
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
38 Definition of Done - Agile Software Development
Definition of Done - Agile Software Development
freeCodeCamp.org
39 Getting started with jQuery (tutorial) - Beau teaches JavaScript
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
40 Making a React Blog with WordPress Content - Live Coding with Jesse
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
41 React, NextJS, CSS - Live Coding with Jesse
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
42 jQuery events - Beau teaches JavaScript
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
43 React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
44 React: Working with API Data - Live Coding with Jesse
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
45 React: Refactoring Components - Live Streaming with Jesse
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
46 jQuery effects - Beau teaches JavaScript
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
47 More React Refactoring - Live Coding with Jesse
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
48 animate in jQuery - Beau teaches JavaScript
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
49 "Finishing" My React Site - Live Coding with Jesse
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
50 Starting a New React Project (P2D1) - Live Coding with Jesse
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
51 React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
52 The Agile Manifesto - Agile Software Development
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
53 jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
54 React Project 2 Day 3 - Live Coding with Jesse
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
55 The INVEST approach to product backlog items
The INVEST approach to product backlog items
freeCodeCamp.org
56 React Project 2 Day 4 - Live Coding with Jesse
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
57 Chickens and Pigs - Agile Software Development
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
58 React Project 2 Day 5 - Live Coding with Jesse
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
59 jQuery: add and remove DOM elements - Beau teaches JavaScript
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
60 React Project 2 Day 6 - Live Coding with Jesse
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org

Related Reads

Up next
Google Meridian | Intro to Priors
Google Analytics
Watch →