#3 NumPy for Machine Learning: Complete Beginner Tutorial (2026)

Akansha Yadav walks through NumPy basics for machine learning: creating and reshaping arrays, indexing, boolean masking, and handling missing values. NumPy arrays are faster and more powerful than Python lists for data preprocessing, feature scaling, and matrix operations. The tutorial uses live Google Colab examples to show element-wise arithmetic, filtering, and clipping.
Chapters
So hello everyone, welcome to another video of this machine learning series. So in this particular video, we are going to discuss about a very important concept which is NumPy. Now this is one of the library which is required for each and every project of machine learning, artificial intelligence, data analytics, or if you are trying to perform something into, you know, data science, then you need this library. Yes, that's correct. Now what is NumPy? Why do we use NumPy? And where exactly we have to utilize this NumPy? So let us begin with the what is NumPy, why do we need it. So very first thing to know about NumPy is it is for numerical Python. It means when you are trying to perform something numerically, when you are trying to perform something mathematically, then this is the powerful library that you need. Now this can support large multi-dimensional arrays and metrics. Yes, I told you, right? So when we are going to work with artificial intelligence and machine learning, we have to basically work with arrays and metrics, and this is an efficient way to do so. Now when it comes to NumPy, NumPy can work with small to large datasets easily. If you have one-dimensional data, you can work. If you have two-dimensional data, you can work with it. If you have three-dimensional data, there also you can work with the NumPy. Now it is the foundation for further other libraries that we are going to discuss in this series. So this is the very first primary library which you require for your machine learning journey, and you have to understand these two libraries very efficiently— NumPy and Pandas. NumPy is for numerical computation and Pandas is for data management. We'll talk about Pandas in the next video, but in this, our core focus is going to be on NumPy. Now what is NumPy? I already told you. Now why exactly do we need it? So as I told you, when we are going to work with data, we have to work on arrays, and if you want to perform something on array, NumPy is the fast efficient way to do so. Second is it supports vectorization. So instead of working within loops, you can perform the vectorization and easily pick up or choose the data whichever you want. Third thing is it is essential for matrix and linear algebra operation. I told you, right, in linear regression type of model, which is a very basic foundational model in machine learning, if you are going to work on it, you have to do some matrix transformations and so on. So this NumPy will help you at that point of time. Now when you are trying to, you know, form the base for many AI and ML libraries, then you need NumPy. You want to handle large dataset within the less memory, then you need NumPy. And main thing about NumPy is NumPy stores the data in nd_array. Now what is nd_array? n is for number of dimensional, like n-dimensional array. This is the core object, just like the data type. So in Python, what we have learned so far is when you're trying to store the value, you are going to store it in the string format, integer format, or float format, right? So whenever we work with NumPy, we work within arrays. An array— that array can be n-dimensional array, 1D, 2D, 3D, any dimensional, and same data type, indexed, mutable, and supports mathematical operation. These are the features of NDArray. Whenever any array is created within NumPy, these features comes along with it. Now the next thing is, I told you, right, feature matrix, data processing, vectorization, everywhere this is required, right? So everywhere this is required. Now let us see why do exactly— where exactly we are going to use it. So very first thing is data processing. We need it in data processing. When it comes to data processing, you have to clean the data to make it perfect. You have to work with the missing values, so you have to pick up those values from the data. Then it is feature scaling. We also have to scale some values whenever the values are really, really slow, we have to scale them. So for this reason also we need NumPy. And when we have to do encoding— we will talk about encoding and feature scaling in the later videos, but for now understand feature scaling means whenever some values are too big, we have to reduce them. Whenever the values are so like too small, we have to increase them sometimes to match the level of other data points. Encoding means converting one data point to another data point so that we have one base for the model, right? Now next is when you are working— when I say mathematical computation, what exactly mathematical computation I'm talking about? Let's say I want to perform matrix multiplication. So I can easily go and use NumPy for mathematical matrix multiplication. I want to perform dot matrix. This we have learned already in mathematics, right? So in university we have heard about matrix, matrix multiplication, dot product, cross product, and so on, right? So we can perform these operations using NumPy. Now when I say I am going to work with images, I do not have to work with, let's say, text data. So when you are doing image processing also Images are also a part of array. Images are part of array. So again, we need NumPy for this reason also. Now, as I said, in the linear regression, which is a very basic model of machine learning, right, here also what you are going to do is you are going to perform inverse of the matrix, you are going to perform transpose of the matrix. So everywhere basically we are going to use NumPy. And NumPy will help us perform these things. Now NumPy also helps us in token processing, token preprocessing. Like, this is a concept of LLM, okay? Token preprocessing. When you are trying to perform embedding, then also you need this. Even in the attention score calculation also, we need the NumPy. Although we have a built-in function, but those built-in functions are built on top of NumPy function, right? So that is why you should have the knowledge about these things. Now let us go and see how to code in it, right? So what I am going to do is I am going to use Google Colab for entire videos, and these Google Colab links either you will get it in the description, or else I will download the IPYNB file and upload it on GitHub for ease, and you can go and, you know, download it and work at the same time in your laptop as well. First thing is we have to import the NumPy. To import NumPy, what is the command that you have to write? It is import numpy. You can give it any nick name as well. Let's say I want to say as np. So I can just write import numpy as np, and then you run the cell. You can use Shift and Enter to run the cell as well. So this will import the NumPy library for you in this particular notebook. Now, next is, whenever we install any library, we might have to check what is the version that we are working. So every time, regularly, the developers are working on certain things, right? So NumPy developers might have, you know, updated the library, giving you more features. So to check that, you have to write down numpy.version.fullversion. Okay, so for now I'm just removing this, and now I can see numpy.version.fullversion. I want to know the version of this library. So what we will do, we will just say what is the version. Full version is 2.0.2. Next is when we are importing it, as I told you, you can import it with the nickname as well, and then you can create an array. Now I'm trying to create one-dimensional array. Which array? One-dimensional array. To create one-dimensional array, np.array. Array is what? Array is a function. Array is what? Array is a function, and that function will take the values. Now you can see, if I will run this, it stores all the values that I have given. Now I want to check what is the dimension of this array which I have created. It is one-dimensional. You can see only one square bracket I am using. It means it is one-dimensional array. I want to check the shape. What is the shape? Shape is obviously 6 comma. This is what data type? This is 6 comma, right? 6 comma means this is tuple. This is tuple, single value tuple. This is single value tuple. And remember, when we create single value tuple, what do we do? We use commas so that it, it considered it as a tuple. Now see, when we are trying to reshape it, let's say the same values I want to reshape. Okay, how do we reshape? We use the function reshape. So a is my data, reshape is the function, and this function takes two value. Rows and columns. This function takes two values, rows and columns. Now 3 comma 2, and then run it. It takes 3 comma 2 and it already divides the data into two columns, two columns and three rows. This is our B. From use— like basically using A variable in which we have one-dimensional array, we are changing it to two-dimensional array with the help of reshape function. And reshape function takes two values. Which all value does it take? It takes the value like we have to enter x value and y value. So both value we have to mention so that the number of rows and number of columns can automatically be created. Now next is, next is ndimensional. And using the ndimensional, what do we do? We check the dimension of the array. Now b is a 2-dimensional array. When you have rows and columns, it is 2-dimensional. When you have only list format, only single data structure structure, that is the basic one. It means one-dimensional dataset. Then again, we can check the shape. When you will check the shape, this time you will get two values: rows plus column. But in one-dimensional, we were just having one single row. Now if I want to change something, now I want to change something. Now what are we changing? First, let's see. Now what I'm trying to do is, you remember the concept of indexing? Same concept we are utilizing. Okay, so 0, 1— sorry, 0, 1, 2, 3, 4, 5. This is our, this is our row number 0, 1, 2, and this is 0 again and this is 1 again. Now I want to change something within first row and first value. Now if I will run this, what will happen? This particular statement, if I will run and then I will print B, what will happen? You can see the value that I indicated, like which is 2, 2 got updated with the new value, which is 77. So you can see, you can— this is how you can update the value. So there has to be square bracket. What do you have to do? You have to use square bracket. First square bracket is for row, second time when you are writing the square bracket is referring to the column. That's how you update the two-dimensional array, okay? This is how you can update the value. We are not immediately seeing the change, but what we have to do is we have to print it to see, right? So if I will print a You can see this is the updated value. What happens in this case? We made changes in b, but b is already referring to a. Remember, b got— we created b. How do we create b? We created b variable with the help of a, with the reference of a, with the data that presents in a. But now when we are making changes in b, the a— the changes that we are making in b will automatically be updated in a. It means a will also reflect those changes. So same change will be there in the both the variables. A, you can see, this is instead of 2, we have 77. It automatically updates, right? So you have to keep in mind, when we are creating array, it means only one container we have created and we are just making different forms of it. That's it. The data remains the same, only different forms are given. Now what I am doing next is I am doing C is equal to A.reshape, doing the reshape again but making copy. Now see, there's a difference between making copy and giving it a reference. Reference means that latest change will be updated in the previous data also, but when it comes to copy, it doesn't reflect. Let me show you. So let's say I'm making copy. So first I am reshaping it into rows and columns, then I am making a copy. Now one, once I've made a copy, this is my actual data which is there. Now I am trying to change the first value which is 0 to -99. 0 to -99. So now let's see whether our a variable is updated. No. This time we created a duplicate. A variable and B variable were same, using the same data, working on same data but different forms. But now what we did is we created A key copy. A copy means duplicacy we are working with, right? So now this will only reflect in the variable C because we have duplicated it, right? Now A and C are totally different independent copies, therefore it worked like this. Now within one-dimensional array, if you want to perform some mathematical operation, like I want to multiply the values by 2, so I can write it like this, just variable a, and what do you want to multiply each value with? So 2 is the value. Now each and every value in the a variable, a array, will be multiplied with 2. Now if I want to calculate, let's say, to the power of 2, right? So I can to the power of 2 also, I can do it. So that's how it works. If you look at the basic If you remember the concept of basic list in Python, what does list say? List says that you can define the list in the square bracket and use the operations, whatever operation you want to do, and you can do it. Now see, there is a change between— there is a difference between array and there is a difference between list. What is the difference between list and array? See, in the case of list, when you are writing this and you are trying to perform some mathematical operation— oh sorry, mathematical— yeah, arithmetic also we can call it. If you are trying to do the mathematical operation like multiply 2, it will repeat it 2 times. It will consider it as a string format. You remember the string format? If I'm writing 101 and saying in the codes and I say multiply by 2, so it means this particular value 101, 101 will be written like this. That's how multiplication happens on string. Same thing on the list. If you have written something and you are trying to multiply 2, it means same set of values it will be written 2 times. Same set of values will be written 2 times. That's how list works. But when it comes to array, it doesn't work like that. Now you cannot perform to the power because list and integers are 2 different values, and to the power of something, there is no substitution related to alphanumerical stuff, right? So in alphabets, when you want to repeat something, we have to perform 2 times, 3 times, 5 times, right? So that comes times. But when it comes to to the power, we cannot do anything on the alphabets, right? Same thing happens on the list. Therefore, arrays are more efficient when working with datasets. So we use arrays for the same purpose. Now you understood what is the difference between list and array? Array are something which we can change, can have different forms, when we can make copies, we can perform mathematical operations directly. But if you are trying to store one-dimensional array, considering it as a list also we can do it, but in list you cannot do that. In list you can only and only use the methods which are defined for the list. You cannot perform anything extra on top of it, right? Now, what is indexing? In case of indexing, what is indexing? In addition to the normal, uh, list indexing that we have, we have the array and we can use array themselves as indexes. For example, if I'm writing this, okay, np.array, and this array says 2, 3, 4. np.array . Now what is 2, 3, 4? If you go back to a over here, this is array 2, 3, 4, we said, right? 2, 3, 4. And we are getting the value also for the 2, 3, 4 only. 77, 3, and 4. So basically we have the indexes allotted within the array. Okay, let's say I want to check whether a is greater than 4. So each and every value within the variable a will be checked. Each and every value within the array will be checked. You can see it is more convenient now when you want to perform any filtering, so you can easily write it. Got it? We can see true, false, true, false everywhere, and then a is greater than this. Now a square bracket a greater than 4 What we are doing now is whichever value are greater than 4, put it in A. I repeat, whichever value are greater than 4, put it into A. A is an array. So this is filtering, this is filtering. So this is— this becomes more convenient. You can trim the outliers. Let's say I have a range that my sales generally goes from 1 lakh to 2 lakh. If there is any value beyond that, it means that that particular client is high paying client.. So to identify those things, those things comes under outliers. We will discuss about outliers, but for now understand outlier means something which is out of range, something which is out of range, and you wanted to trim them. You don't want them in the data, so you perform these type of operations where you filter them out based on some condition. Now see, so now what we do is we say a, a greater than 4 is equal to 4. If there is something greater than 4, do this. So there is something, there is a clip function also. There's a special clip function when you wanted to clip the values. You can use this at both end. This function will work. Now what is the range that we are giving? We are giving starting from A, 0 to 4, starting from 0 to 4. Now at this point of time, what we did? At this point of time, what we did? At this point of time, we were filtering. We were saying whichever value greater than 4, put it into A, and then whichever value is greater than 4, update it with the value 4. It means whichever value were less than 4, keep them as it is. If it is greater than 4, then change it to 4. So this becomes our updated one. Now whichever value we want, like whatever range you want to consider, you can clip the data into this using the clip function. Got it? Now if you are handling non-existing values, let's say the value which are None or NaN— NaN means not a number, NaN means not a number, right? So if you wanted to work on those values which is non-existing, then you have the function called isNone. First we can check whether is there is any value which is missing or not, then isNone function will help you do that. And then c dot, we can filter them as well, and then we can consider replacing it with the mean. I hope you have understood a lot about NumPy library, its functions, its functionality. And later on maybe we will use it in the projects, we will understand more functions, more stuff from NumPy. So I hope you have learned so many things. Check out the links in the description, show some love on the video so that I also get motivated and create more stuff and get more learning resources for you guys. So happy learning, keep growing, and keep it










