Why NumPy Arrays Matter for AI/ML
Before diving into code, understand the why: NumPy arrays are the foundation for organizing data in machine learning. Just as a library organizes books on shelves, NumPy structures raw data so you can:
- Access specific data points quickly
- Perform calculations efficiently across entire datasets
- Prepare data for AI/ML algorithms that process massive matrices
The roadmap ahead: organize → inspect → manipulate data, all powered by NumPy.
7 Essential NumPy Array Creation Functions
1. Zeros Array (np.zeros)
Creates arrays filled entirely with 0, perfect for initializing data structures.
import numpy as np
# 1D array: 4 elements
np_zeros = np.zeros(4) # Output: [0. 0. 0. 0.]
# 2D array: 2 rows × 4 columns
np_zeros_2d = np.zeros((2, 4)) # 2×4 matrix of zeros
# 3D array: 3 layers of 4×6 matrices
np_zeros_3d = np.zeros((3, 4, 6)) # 3×4×6 tensor
Key insight: np.zeros((rows, cols)) for 2D; add another dimension for 3D.
2. Ones Array (np.ones)
Identical to zeros but fills with 1.
np_ones = np.ones((3, 3)) # 3×3 matrix of 1s
3. Full Array (np.full)
Fills every element with your chosen constant value.
# 2×5 array, all values = 7
np_full = np.full((2, 5), 7)
Syntax: np.full(shape, fill_value)
4. Range Array (np.arange)
Generates evenly spaced values within an interval, similar to Python's range() but returns an array. For a deeper understanding of how arrays are structured in memory, see Understanding Arrays in Programming: Declaration, Initialization, and Memory Representation.
# Start=1, stop=10 (exclusive), step=2
np_range = np.arange(1, 10, 2) # Output: [1 3 5 7 9]
# Single argument: np.arange(4) → [0 1 2 3]
5. Identity Matrix (np.eye)
Creates a square matrix with 1s on the diagonal and 0s elsewhere, essential for linear algebra.
# 4×4 identity matrix
identity = np.eye(4)
# Output:
# [[1. 0. 0. 0.]
# [0. 1. 0. 0.]
# [0. 0. 1. 0.]
# [0. 0. 0. 1.]]
# Flip diagonal to opposite side
np.fliplr(identity)
6. Linear Spacing (np.linspace)
Creates evenly spaced values between a start and end point, specifying the number of points (not the step).
# 5 equally spaced values from 0 to 10
np_interval = np.linspace(0, 10, num=5) # [0. 2.5 5. 7.5 10.]
7. Random Integers (np.random.randint)
Generates arrays filled with random integers within a range, crucial for simulations and testing. For a more comprehensive overview of all these functions in a tutorial format, check out our Python NumPy Tutorial: Arrays, Operations, and Data Science Notebooks.
# Single random integer from 1 to 10
np_random = np.random.randint(1, 10)
# 5 random integers from 1 to 10
np_random_5 = np.random.randint(1, 10, size=5) # e.g., [6 2 3 2 1]
# 3×4 matrix of random integers 0-20
np_random_matrix = np.random.randint(0, 20, size=(3, 4))
Quick Reference Table
| Function | Purpose | Example |
|----------|---------|---------|
| np.zeros(shape) | All zeros | np.zeros((2,3)) |
| np.ones(shape) | All ones | np.ones(4) |
| np.full(shape, val) | All same value | np.full((3,3), 5) |
| np.arange(start, stop, step) | Range of values | np.arange(0, 10, 2) |
| np.eye(n) | Identity matrix | np.eye(4) |
| np.linspace(s, e, num=N) | Equal intervals | np.linspace(0,1,5) |
| np.random.randint(low, high, size) | Random ints | np.random.randint(1,6, size=10) |
Key Takeaways
- Organization first: Always structure your data into arrays before performing operations
- Choose the right tool: Use zeros for initialization, identity for linear algebra, random for testing
- Dimensions matter: 1D (vector), 2D (matrix), 3D+ (tensors), AI/ML often uses high-dimensional data. Learn the difference between array types in Understanding Static Arrays, Dynamic Arrays, and Strings in Python
- Check dimensions: Use
.ndimproperty to verify array shape - Data analysis: After mastering array creation, combine NumPy with pandas using Python Pandas Basics: A Comprehensive Guide for Data Analysis
- Deep learning: NumPy arrays are the building blocks for neural networks. See how in Neural Networks from Scratch: Build Deep Learning Fundamentals in Python
Hi everyone, your AI Wally Didi is back with lesson two of NumPy Python series. So, in previous lecture we understood a
basic overview of what is NumPy. We also understood that how to create our NumPy array. But, I want you guys to have more
clearer picture about why we are learning NumPy because it's very important whatever we are studying, we
should know why we are studying, right? So, in this video we'll cover like why we are creating NumPy arrays, okay? What
will be the future goal of our series and how actually NumPy will help us in AI/ML, okay? So, after this lesson you
will have many doubts cleared, okay guys? And you will have your base and your basic questions cleared, okay? So,
without wasting any more time, let's dive in. But, why NumPy array?
Why we are learning NumPy array? Why we are creating NumPy arrays? Why we are putting our data into NumPy data
structures? Like, why? Now, look at this picture. For example, we have this data table, okay? And it
has multiple information about students, right? For now, we just have four students over here. It could be like
hundreds of students, right? Now, if I want to perform any operation over here, then it's very important for
me to actually organize the data and then perform any information any calculation on this, right? So,
organization of data so much of data. For now, we have just four rows, right? Four rows, five rows, columns. But,
in real time we have a lot of data. Now, let's take an example. I have a heap of books, okay? I have a heap of books,
okay? We have multiple books in that like thousands of book. I want one book that is history book out of it, okay? A
history book out of it. So, now when I have so much different types of book, will it be easy for me to
get that specific history book out of that? No, right? Instead, when I walk into a library, guys,
I'll be directly going to that history shelf. I'll go through
the stack of the history books, and I'll be easily to find the book which I want, right?
So, organization of data is very important. When we have multiple things and we want to have
access to those thing, we want to do any calculation, any manipulation to that thing,
then very first step is organization of data. Understood?
So, what's NumPy array is doing for us? We are getting a whole amount of data, and then we are organizing that data
into a NumPy array. Understood? Here we have table, and then we
imported that table into NumPy array method, and then it looks something like that. Okay?
So, very first thing is that how to organize that data. And one of the method is NumPy array. There are
multiple methods. I'm not saying there is just NumPy array. There are multiple methods. Actually, we have multiple ways
of organizing that data. That we will be learning in today's lesson, okay, in today's lesson. Then, after one or two
lessons, we'll be learning how to inspect and understand that data, right? Once we are done with the organization,
we'll be organizing that data nicely, and then we'll understand that how to inspect, like how to go to that data.
Once we have our books stacked into the library, we have our books organized, then for me, it's important for me to
understand that in which row I have to go, like in which row I have to inspect that book, right? So, we have to learn
that also. That how we'll be inspecting our data. And then we'll be learning how to
manipulate that data, right? So, whenever we have so much of data, it is very important that we have a direction,
right? We have a method, we have ways, we have nice, simpler ways to access that data and do any action or
manipulation on that data. And in AI/ML, what we are doing is that we are putting so much of data out there
and then we are performing and something on that data and then we are getting the response, right? Something is happening
in back end, right? And then we get the data in response. So, I'm not saying that it is what actually NumPy is doing
into AI/ML. It's just a very small perspective of AI/ML, okay? A very small or basic
calculation of AI/ML, okay? You'll be able to understand the AI/ML calculation stuff in my future series, in my future
lectures. For now, we are just covering NumPy arrays, okay? So, now let's understand
what are different ways that we can organize our data in NumPy arrays. Types of NumPy array creation.
So, now we have some functions which NumPy has given us and those functions helps us to create some
fixed type or we can say default valued type NumPy arrays, okay? For example, we have a array function with zeros. So, in
this, we'll be creating a array which will have all values as zero, okay? Similarly, we have arrays with one. So,
the array which will be created will have all values as one, okay? We have more like these, okay? But now, you will
be having question that why such functions are given to us? Whenever we are organizing any kind of data, we need
to have a fixed type, right? For example, I want my data to be stored in 2D array. I want my data to be stored in
3D array. I want my data a set of data, I want it to get calculated with another set of data which will be having
all diagonal values as two. Something like that, okay? For example, I have some data. Okay? 1 2 3 4 5 6. And
my requirement is that whatever this data is, it should be calculated with an array which will be having diagonal
values as two. Okay? Or we can say take another thing that if I have a data, let's say 1 2 5 4
6 7 8. I have so much of data. Now, I want this data to be stored in what? In a 2D array. Okay? And that 2D array
should be having all values as five. Okay? So, let's say this is my 2D array. Okay? And
these all values should be stored in a 2D array, but it should be computed with the value of five. Like I want each
value to be multiplied by five and then get stored in a 2D array, right? So, this is one of the example. So, when we
have some scenarios which will be pre-decided and we have a pre-decided data, but we want it to be stored in a
fixed type. We want it to get calculated with some value. Then then we use NumPy array functions. Okay?
We'll be seeing the requirement. We'll be seeing the practical example when do we actually use it. But for now, just
understand that when we have a fixed requirement of storing a data into a type of array and then we create an
array using these NumPy array creation. Okay? And then we do whatever calculation or whatever
um permutation combination we put over here to calculate one matrix with the another
matrix like we used to do in 10th, right? We used to create a diagonal matrix, then used to create a matrix
with some 1 2 3 4 5 6 value and then we used to multiply the rows and columns and everything, right? So, these kinds
of calculations comes into the picture. Okay? So, I hope now you have a clear picture that why actually we are
creating NumPy uh NumPy array types. Okay? So, now we'll see everything in practical like
how to create arrays with zero, arrays with one, and everything. So, now let's come to the Jupyter Notebook. I have
already shared with you how to launch a Jupyter Notebook and how to run it, how to install Python in the first lesson.
So, if you haven't seen that, please go and check it out. And then we'll continue from here, okay? So, very first
thing, let's do a quick revision, okay? So, we what we'll do is we'll be import NumPy as np. We'll be creating the
alias. np will be using it again and again, okay? Then, how to create a NumPy array? We create it like np.array.
Okay, so this is the syntax, okay? Then we have the array value uh inside it, okay? The array value, the array data
which we needed to get converted into array. I will not say array value, I will say our data inside this and that
will be converted into NumPy array, okay? So, this is our 2D, okay? So, over here you can see that where I have
passed index and then it gets printed and I have also checked what is the type it over here is as the 2D, okay?
So, now we'll see how to create a NumPy array with all the values as zero, okay? So, I have already added a title over
here. Okay, so first thing what we'll have to do is that we'll create a variable with the name of np_zeros
equals to np.zeros and dot The function which we'll be using to create NumPy array with zeros is zeros, okay? It's so
simple. Then we'll pass the value we want it to be 1D, 2D, 3D, okay? Currently I have
passed four, that means it will create a 1D, okay? So, we'll print it now. Print np
_zeros, okay? And we'll run this. So, see, it has
created a 1D array with all the values as zero and we have one row and four columns, okay? Because we have passed it
four, that means it will create a 1D array, that means just one axis, okay? If you want it to be a 2D array, we'll
pass this over here. We'll try to run it again. Okay, it's showing us incorrect because we have to pass the
brackets again, okay? So, now you can see that we have what? We have two rows and four columns, okay? This first value
is what? The row, and the second value is the column, X and Y, remember. We have got a matrix, X and Y matrix, okay?
And all the values are zeros, okay? If we wanted to be like three, then it will print the three,
okay? So, this is how our matrix looks like, okay? So, we have printed what? We have printed the 2D array, okay? So,
let's check it's 2D only. For that, we'll print um np _0
dot. We'll mention ndim, it will help us to get the dimension, okay? So, this is 2D array,
okay? Now, what I want is that I want to create a 3D array. So, in this only I'll pass one
more value, that is six, and we'll print it. And look at this. This is how our 3D array looks like, right?
When we talk about 2D, we have X and Y, right? And when we have 3D, we have X, Y, and one more, that is the Z, right?
We have the Z axis as well. The same thing has happened over here. We have 1 2 3 4, four rows, and 1 2 3 4
5 6 columns. And these are getting dimensioned, like multi-dimensioned. This array is got at
like three times, right? So, we have this same thing three times. So, now the data set has increased so much,
understood? First, we were getting just normal one array. Now, the data set has
increased so much, right? It when we I'll type 10 over here,
it will give me what? It will give me this many data set, right? This much of space, right? So, this is called 3D. And
when we talk on multi-dimensional, that means oh my god, think about it, like how much of data.
And when we talk about AI/ML, this is what comes into picture. This is so much of of data, right? Understood? So, let
me remove this and um let's first check that Yeah, okay. So, we have 3D, okay? No, now let me remove
the value. Let us create it 2D only. Run it again. Okay, so now we have 2D. Okay. So, this is how we create any array with
zeros and I've showed you how 3D array looks. I missed it in the previous lecture, so I covered over here. Okay.
So, now we'll create the another type that will be the array which will be having all ones, okay? So, we'll declare
Okay, let's do one thing. Just copy and paste over here. I'll in place of zeros we will pass ones, okay? And when we
talk about the function over here, the name of the function is ones, okay? And we'll pass it as np_ones,
okay? Similarly, let's remove this and just run this,
okay? So, we have a array with all values as ones, okay? So, this will print the Okay, so this will add all the
values one into the NumPy array. Okay. So, we have covered NumPy with zeros. We have covered
NumPy array with ones. Now, we have to see full arrays. So, when we talk about full arrays, that means we will be
creating a array with all the values as same values. Like if I want a array which will be having all values as five,
okay? So, what you have to do is declare a variable for full. It's np_full equals to np. The function is full.
Remember, the function is full, guys, okay? Then we have to mention the range which we want. Let's say 3 by 5 we want.
Sorry, 2 by 5 we want. And we'll close this. We'll put a comma over here. It is very important. The comma is important.
And we'll mention what value we want. Let's say we want seven, okay? And we'll print it.
np_full, okay? See, we have a array with all the values
as seven, okay? So, this is why it's called full, okay? So, we have done with full arrays also. The function name was
full, okay? Understood? Now, we'll see array with range, okay? And for that, the function is a range, okay? So, let's
create for range also. np_ range, okay? Equals to np.arange, the function name. And we want our range
to start with like one, it should go till 10, and it should have a difference with,
let's say, two, okay? So, in our a range function, what we do is
like we write the starting, we write the ending, like the one is the included one in the range. It will not include 10,
but it will include before that, okay? And the differentiation of the range, which is two, okay? So, we want from one
to before 10 numbers with a differentiation of two, okay? So, it will get us a range, it will get us a
consecutive range, okay? So, we'll print it. Okay. So, we have 1 3 5 7 9, see? It
hasn't included the 10, right? So, whenever we declare something like this, always remember we have the starting
point, and this point, the second point is that we'll not include that, but before that, okay? So, till nine, it
will include till nine, okay? So, this is range, okay? So, when we talk about thing like this, to have a consecutive
numbers, we mention over here. For example, I mention just four, what it will do is it will get me 1 2 3, right?
So, it got me 0 1 2 3, not the four, okay? So, this is how our a range function works, okay, guys? Okay.
So, now our second thing is diagonals with I. So, this is also called as identity matrix over here, this one,
okay? So, this is also called identity matrix. And this used to be one of my favorite
question in exams. Like whenever we matrices and determines was into the picture, I used to be confident enough.
I hate trigos. Uh but uh matrix identity matrix and stuff like
calculation was simple, right? We have identity matrix. And when we talk about identity matrix, we have matrix
something like this, right? Uh the diagonally we have one, right? So, this is somehow our identity matrix looks,
right? So, how to create it with NumPy is very simple. You will just create one more variable. We'll write um
identity identity with the name equals to np.i, okay? Remember, it's i though i y e,
okay? And then we'll create like what type of identity matrix we want. Let's say we want 4 by 4, okay? And uh
we will do the print, okay? Print the np_ Okay, so we have a identity matrix with
one in diagonal. You can see over here, right? Now, because it was 4 is to 4, it looks very clean, right? We have
diagonally one. But if I have five, it will automatically arrange it. Like we have 1 2 3 4 ones like in diagonal and
then we have no ones in this column, okay? Now, if we want our this matrix to have
diagonally one, not like that but like this, what we will do is it's very simple, guys. You will come over here.
We'll type np. flip, okay? Flip lr. And in this you will add this.
Okay? And you will print this again. You can see the ones got flipped from there to like this, okay? So, this is
identity. Like we have to get ones in the diagonals, okay? So, this is also done.
Okay. So, identity done. Now, s lean space linear with intervals. Okay? So, the function that is linspace.
So now for this what it is, let me explain you. We'll have a range, okay, a start range and a ending range, okay?
Let's say we have a start range, okay? We have one and the ending range is 10. Then we'll tell the intervals, like how
many values we want. For example, from 1 to 10, I want five values, okay? So it will break the linear space into equal
five values, okay? Let's see in practical, okay? You will understand more with that. Okay.
So we'll create np under interval. Okay, equals to np. dot linspace, okay? The
what? Linspace is our function name, okay, guys? Then we'll
write the starting value, the ending, like it will not include 10, but before that, okay? And then we have
to mention the num value, okay? So we'll write num equals to how many intervals we want. We
want five, okay? So we'll print it. np underscore interval
See? It has printed five values, okay? 1 2 3 4 5 with the equal spaces, see?
The spaces is equal. Let me make it zero and then run it, okay? So see, it's 2.5 is the differentiation from zero to 2.5,
then five, then 7.5 and 10. Right? That's how the linspace work. It creates equal
intervals of data sets. Okay, so finally we have random numbers, guys, okay? And the function we use is rand int. So
random numbers, what is it? That it will generate a numpy array with random numbers and we'll pass the starting
numbers and the ending interval, okay? So, we'll pass like one or 10, okay? Let's see in practical how
it works. Okay, so we'll come over here. We'll write like random np.random, okay? Equals to
np.random, okay? Inside random, we will have a randint, okay? And in randint, we have
to mention the starting and the ending value, and it will print random numbers in this
number range, okay? So, we'll pass np_random, okay?
We'll run it. So, see. So, it printed a random value from one to 10. Now, let's say I want it to tell that print five
numbers. So, we'll print five over here. It will get me 62321, like five digits from one to 10, okay?
Similarly, if we pass 16, it will get us 16. Okay? So, that's how we generate a NumPy array with random numbers using
rand int function, okay, guys? So, in this video, we learned a lot, like we learned how to generate different NumPy
array with some default values, okay? So, that's it for today's lesson. I'll meet you in another lesson. If you have
any doubts, anything, you can just comment down below. I'll answer all you guys, okay? I'll meet you in another
video. Until then, guys, stop scrolling and start learning. Bye-bye.
NumPy arrays provide the structured foundation for organizing raw data in AI/ML workflows. They enable efficient data access, fast calculations across entire datasets, and prepare data for algorithms that process large matrices. Essentially, NumPy arrays act as organized shelves for data, making it ready for inspection and manipulation.
np.zeros(shape) creates an array filled with 0s, while np.ones(shape) fills it with 1s. For example, np.zeros((2,4)) generates a 2x4 matrix of zeros. Use these for initializing data structures, such as setting up weight matrices or placeholder values before filling in data.
np.arange(start, stop, step) generates values based on a defined step size, similar to Python's range(), and stops before the endpoint. In contrast, np.linspace(start, stop, num=N) creates a specified number of equally spaced values including both the start and stop points. For example, np.arange(1,10,2) yields [1,3,5,7,9], while np.linspace(0,10,5) yields [0., 2.5, 5., 7.5, 10.].
Use np.eye(n) to create an n×n identity matrix with 1s on the diagonal and 0s elsewhere, essential for linear algebra operations. For example, np.eye(4) produces a 4x4 matrix. You can flip the diagonal using np.fliplr(identity) to place 1s on the opposite diagonal.
Use np.random.randint(low, high, size) to create arrays of random integers from low (inclusive) to high (exclusive). For a single value, omit size; for a 3×4 matrix of random integers from 0 to 20, use np.random.randint(0, 20, size=(3,4)). This is ideal for simulations and testing datasets.
Dimensions are passed as a tuple to the shape parameter. For 1D arrays, use a single integer (e.g., np.zeros(4)). For 2D arrays, use (rows, cols) (e.g., np.zeros((2,4))). For 3D arrays, use (depth, rows, cols) (e.g., np.zeros((3,4,6))). Always remember that shapes are tuples; otherwise you may get unintended 1D arrays.
Keep this summary
Save it to LunaNotes and it becomes a real note in your library — editable, searchable, and ready to turn into flashcards or a diagram. Free to start.
Save to LunaNotesOr summarise for another video.
32K+ people have turned 65K+ videos into notes
“Very useful tool. I'm using it for the last couple weeks and I'm loving it so far. Makes learning on YouTube a great experience.”
This summary and transcript were automatically generated using AI with the Free YouTube transcript summary tool by LunaNotes.
Related summaries
Python NumPy Tutorial: Arrays, Operations, and Data Science Notebooks
इस वीडियो में हम Python की NumPy लाइब्रेरी के बारे में विस्तार से सीखेंगे। जानेंगे कि NumPy एरे कैसे बनते हैं, उनके ऑपरेशंस कैसे करते हैं, और नोटबुक्स जैसे Jupyter कैसे सेटअप और उपयोग करें। साथ ही Sudoku के माध्यम से प्रैक्टिकल प्रोजेक्ट के साथ डेटा साइंस की मजबूती बढ़ाने का अवसर मिलेगा।
Python Functions: Reusable Logic and Code Organization Basics
Learn how to create and use Python functions to package logic into reusable blocks. This lesson covers function definition, parameters, return values, and how functions combine with loops to build structured programs.
AI Fundamentals: Machine Learning, Deep Learning & Neural Networks Explained
This comprehensive lecture covers the core concepts of Artificial Intelligence, from everyday applications like Face ID and recommendations to the technical foundations of Machine Learning, Deep Learning, and Neural Networks. Learn the key differences between supervised, unsupervised, and reinforcement learning, and understand how models are trained and used for predictions.
Python Pandas Basics: A Comprehensive Guide for Data Analysis
Learn the essentials of using Pandas for data analysis in Python, including DataFrames, operations, and CSV handling.
Understanding Arrays in Programming: Declaration, Initialization, and Memory Representation
This video provides a comprehensive overview of arrays in programming, covering their declaration, initialization, and how they are represented in memory. It explains the need for arrays, the types of arrays, and the importance of data types in array declarations.
Most viewed summaries
A Comprehensive Guide to Using Stable Diffusion Forge UI
Explore the Stable Diffusion Forge UI, customizable settings, models, and more to enhance your image generation experience.
Kolonyalismo at Imperyalismo: Ang Kasaysayan ng Pagsakop sa Pilipinas
Tuklasin ang kasaysayan ng kolonyalismo at imperyalismo sa Pilipinas sa pamamagitan ni Ferdinand Magellan.
Mastering Inpainting with Stable Diffusion: Fix Mistakes and Enhance Your Images
Learn to fix mistakes and enhance images with Stable Diffusion's inpainting features effectively.
Pamamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakaran ng mga Espanyol sa Pilipinas, at ang epekto nito sa mga Pilipino.
How to Install and Configure Forge: A New Stable Diffusion Web UI
Learn to install and configure the new Forge web UI for Stable Diffusion, with tips on models and settings.
Found this summary useful?
Take it with you. One click puts it in your own LunaNotes library.
Save to LunaNotes