0% found this document useful (0 votes)
15 views10 pages

5.1 - Numpy Introduction - mp4

Numpy is a crucial Python package for numerical and scientific computations, widely used in data science, machine learning, and various scientific fields. It provides efficient representations of multi-dimensional arrays and is designed for fast mathematical operations, making it significantly faster than traditional Python lists. The document explains how to create and manipulate Numpy arrays, including one-dimensional, two-dimensional, and three-dimensional arrays, and highlights the efficiency of Numpy through performance comparisons.

Uploaded by

NAKKA PUNEETH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

5.1 - Numpy Introduction - mp4

Numpy is a crucial Python package for numerical and scientific computations, widely used in data science, machine learning, and various scientific fields. It provides efficient representations of multi-dimensional arrays and is designed for fast mathematical operations, making it significantly faster than traditional Python lists. The document explains how to create and manipulate Numpy arrays, including one-dimensional, two-dimensional, and three-dimensional arrays, and highlights the efficiency of Numpy through performance comparisons.

Uploaded by

NAKKA PUNEETH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Numpy is one of the most important packages in python for numerical computations and

scientific computation. We'll see and motivate why Numpy is required and introduce you to
some basics of numpy. Numpy is extensively used in data science, machine learning and AI.
And actually Numpy is used in lot of scientific areas, not just in machine learning, AI, for
example, for physics, in computer graphics, lots of areas. It's one of the most powerful
numerical processing libraries in whole of Python. Now, what does Numpy contain? Numpy
contains objects, or it contains some representations of integers and floating point numbers.
It also has containers like it has lists and dictionaries internally built so that we can do
mathematical computations very efficiently. So what Numpy provides us is basically an
extension to Python for multi dimensional arrays. See, I can create a multi dimensional
array in Python, right? Using existing data structures like lists, I can create that. But Numpy
provides us much more efficient. I'll prove that to you in a while, that numpy provides much
more efficient representations of multidimensional arrays and mathematics. What is a
multidimensional array? This is a one dimensional array, or it's also called a vector or an
array, right? So an array like this is a 2D, this is a 1D array, this is a 2D array, a matrix. A 2D
array is like a matrix. A 1d array is like a vector or an array or a simple array. What about a
dimensional array? Three dimensional array is like a cube. Three dimensional array can be
thought of as a cube, where you have cells in a cube, right? You have a cell in a cube, and
data is stored in each cell, okay? Similarly, you can have four dimensional arrays and so on
and so forth. You can have n dimensional arrays just the way you have one, two, three. Of
course I can't draw an n dimensional array like a four or a ten dimensional array on this
surface. Okay? But intuitively it makes sense. Now, what it does is its implementations.
Numpy's implementations of all of these multidimensional arrays is very close to hardware,
and hence it is super efficient, and it's designed for scientific computations by making it
extremely convenient for us. Wherever we need a multidimensional array, it's much more
efficient and much more convenient for us to use numpy arrays. And oftentimes this is
referred to as array oriented computing, just like because array is the central data structure
in whole of numpy. So numpy provides us an array oriented computation. And as soon as
you have n dimensional arrays. All of scientific computing, mathematics, most of
mathematical operations can be very, very efficiently done, can be modeled as n
dimensional arrays. And if you have a nice package which can help us do that efficiently and
conveniently, it's better to use it. Let me prove to you first that numpy is very, very efficient.
Okay, before we go ahead, let's see how to create a simple array in numpy. It's very, very
simple. There are two simple ways of creating an array. So first of all, you have to import
numpy. I'm importing numpy with an alias of NP. If I say NP array, and if I give a list here,
remember, this is a list here. What I'm giving to this is a list, right? What it creates is it takes
the list as input and it represents this list internally as a numpy array. And when I print a, I
get this, it looks like a list, but it's actually a numpy array. Similarly, if I want to create a
numpy array of all the numbers between zero to nine, I can just simply use the arrange
function here. NP arrange ten will create a numpy array for me with values from zero to
nine. These are two simple ways. You can explicitly give a list or you can say, I want
numbers between zero to nine or zero to n minus one. Those are two simple ways. Now,
since I've been claiming to you that numpy arrays are much more efficient, let me prove that
to you. Okay, so let's assume I use a list here. I'm creating a list right here with this. I'm
creating a list right within the range of zero to 999. So this creates a list of values between
zero to 999. Now what I'm doing here in this line, I'll come to what time it is in a second.
What I'm doing here is I am computing the square of each of these numbers, the square of
each of these numbers. See, I'm saying create a new, see what I'm doing with this here? I'm
saying create a new list. This is basically creating a new list. Create a new list such that the
elements of the list are squares of elements. So for I in l say l is my original list, right, for in L
compute, I square, this is I power two, right? And I'm creating a new list with all the squares.
That's what I'm doing here. So what am I doing? I'm basically from here, I'm creating a list of
0149, so on and so forth, 999 square. Okay, from this list, I'm trying to create this list using
this one line code. Now what this percentage time it does, this is a very, very interesting
operator or a function, if you want to think about it that way. In Python, where it runs this
operation multiple times, it repeats the same thing multiple times, and it measures how
much time it took per iteration. Okay? Very, very important, right? It's a very, very
important thing. It says how much time on an average it took for us to do this computation.
Now, when I run it using list, see this code? I am doing it using Python list, the built in data
structure, right? It took roughly about three, seven microseconds. This is the mean. And
those of you who know what standard deviation is, it took about 17.6. So since it has run
this multiple times, it got a mean of 3.37 microseconds. And by the way, for those of you
who don't know, a microsecond is 1,000,000th of a second. Is 1,000,000th, is 1,000,000th of
a second, okay? So a million microseconds will make 1 second, okay. While it sounds small,
let's look at this, right? It took about three, not seven microseconds, the average time it took
in our probability and statistics and exploratory data analysis, we understand about mean
and standard deviation. Forget about the standard deviation. It's not very important right
now. But the most important thing is the mean. Right? Now, let's try to do the same
operation. What did we do? We took thousand numbers, and we computed the square of the
thousand numbers, and it took us about three, seven microseconds. Right? Now, if I do the
same thing using numpy here, I'm creating a numpy array here. I'm doing the same thing
here. I'm computing here. I'm creating a numpy array of numbers between zero to 999,
right? And I'm saying a power two, which means I'm squaring each of the elements in this
numpy array. And I'm also using time it, percentage time it. Here it says that it took exactly,
roughly, the mean value is 1.35 microseconds. Now, the most important thing we have to do
is we have to compare these two numbers. Just look at these two numbers. If I use lists, if I
use lists, it took me about three, seven microseconds. If I use numpy, if I use numpy, it took
me roughly about 1.35 microseconds. This is almost, roughly like 200, 200 times, more than
200 times, actually. Right. This is way, see, even though microsecond might seem too small
compared to Numpy's implementation of computing the exponent, the list implementation
is almost 200 times slower. This is what I have been saying, that numpy is both memory
efficient and it's super fast computations. It's almost 200 times faster. Imagine doing this for
a very, very large computation. What we've done here is a very, very simple computation.
Very, very easy computation. But even for this simple and easy computation, Numpy was
200 times faster than lists. Right? So I hope with this, you're convinced, with this simple
experiment, you're convinced that Numpy is both memory efficient and it provides us
extremely fast numerical operations. Now let's look at, with that behind us, now let's
understand how to construct, how to create numpy arrays. It's very, very simple. Let's look
at how to create a one dimensional array. We just saw this, right? So I can create a numpy
array by giving a list of numbers. Once I give a list, it creates an array. If I just say a, it
doesn't say it's a list, it says it's an array, which means it's a numpy array. Now I can find the
number of dimensions. This is a one dimensional array, right? So if I say a dot endem, it
gives me one, which means it's a one dimensional array. I can also ask for the shape. So this
has what? This has four elements, right? So for a one dimensional array, it simply gives the
shape to be four elements with nothing on the other side. So it just says there are four
elements. Right. Now we'll see where shape is useful when we go little longer. I can also say
a length of a, which tells me how many elements are there. Now let's go to two dimensional
and three dimensional arrays. So if I want to create a two dimensional array, which is a
matrix, right? If I want to create a matrix like this, which is a two dimensional array, right?
All I have to do, all I have to do is I have to give a list of lists. Look at this. This is a list. This
outer thing is a list. This inner thing is also a list. So if we create a list of lists, numpy will
take each list as a row. This, it will take it as a row. This it will take it as a row, and it creates
this two dimensional array, as you can see here. Now, if I say number of dimensions, since
this is a two dimensional array, it will say it is a two dimensional array and shape here. How
many rows are there in this? There are two rows. There is row one and row two, right? So
there are two rows and how many columns are there? This is column one, this is row one,
and this is row two. This is column one, this is column two, and this is column three, right?
And it has three columns. So it'll say that the shape of this matrix or the shape of this numpy
array is two by three. Two, comma, three. Okay, now if you say length of B, now if you say
length of b, it returns the size of the first dimension, right? So look at it. Now if you say
length of B, that's a difference between shape and length. Shape tells you we have an array
like this, right? We have an array like this, which is two rows and three columns. This is how
we write it in matrix notation. In matrix notation, at the end of this matrix, we say this is the
number of rows and this is the number of columns. This is the standard mathematical
notation of doing it. You might have come across this in your 11th grade or 10th grade when
you're learning about matrices. Length of b basically gives me the size of the first dimension,
which is two here, or the number of rows. Okay, now how to create three dimensional
arrays. So we learned about two dimensional arrays, right? Now let's see how to compute
three dimensional arrays. So here I have, here I have my lists, right? These are two small
lists and this is a bigger list and this is the biggest list. So I have list within a list within a list,
right? If you think about it, because this is the innermost list. This is a second layer of list.
This is the third layer of list. So I have list within a list within a list. When I create this, what
happens actually is it creates a three dimensional array. It creates a three dimensional array
for me. And how does that array actually look like? It has 0123. I need to show it to you in
3D. It's a little tricky. Okay, let me try to do that. 4567. Okay, roughly, that's how it looks.
Okay, it's basically like a grid. It's basically like a two cross two grid. Let me try to draw this
better like this. Okay, here I have zero. Here I have one. Here I have two. Here I have three.
Here I have four, five, six and seven. Okay, this is like a three dimensional array, right?
Because I have values at the vertices of each of the vertices of my cuboid here. This is like a
three dimensional array. Now if you say what are the number of dimensions, it says three
up, obviously. And if you ask for the shape, it says it's a two cross two cross two, okay? And
when you say shape, remember what you get is a tuple. Get a tuple stating what is the size of
this? So because it has two rows, two columns and depth of two, right? It has length of two,
breadth of two, and depth of two. That's why it says it's of size two cross two cross two.
Now you can create instead of three dimension, you can create four dimensional or ten
dimensional arrays. And we'll come across these types of arrays in machine learning,
especially when we are learning about deep learning. Okay? So by the way, for those of you
who are interested, a 2D array is called a matrix. A 2D array is called a matrix, right? A 1d
array is called a vector. Now the question is, what about an n dimensional array? What is it
called where n is three, four, five, or any such number? It's called a tensor. These are called
tensors in mathematics. Okay? So for those of you who are wondering, the word tensorflow,
that Google is very famous software from Google for deep learning, this is where the word
comes from. A 1D array is called a vector. A 2D array is called a matrix. An n dimensional
array in mathematics is called a tensor, okay? So that's a new term if you didn't know about
the term tensor. Now, since we have learned about how to create numpy arrays, let's go and
understand some more ways of doing it. So arrange, we have seen this function, right? If I
say np, arrange, if I give a value of n here, it creates a numpy array of values between zero
and n minus one. Very simple way. The other way for us to create numpy arrays is this. This
is a very interesting way I can say that I want to create a numpy array. So this is called the
start, this is called the end, and this is called the step size. So with this, what happens is it
creates a numpy array with the start value being this and end value being just less than this,
not exactly this. Exclusive of this, we'll see. Step size equals to two, which means now,
instead of see in the, in the previous case, whenever I said NPR, range ten, I would get 0123,
so on and so forth, up to nine, right? So here I have a step size or difference of one between
every consecutive number. That's what is called as a step size. Here I have a step size of two,
which means my first value will be one, my second value and my first value will differ by
two because my step size is two. Okay? Then my next value will differ by two. My next value
will differ by two. My next value will differ by two till the time I reach ten. But I'll never put
ten in this array, okay? It's exclusive of this value. It's exclusive of this value with this step
size. Simple way, right? That's how you can create a numpy array. There is another way
called linear space, which you can use to create numpy arrays, which is also very, very
useful. Suppose if I want to create a numpy array again, start, end. And here I'm saying
number of points. So here I can say that I want to create a linear space. And what is a linear
space? Let me explain you. So it's saying between zero and one. So I take this as zero. I take
this as one. This is my start, and this is my end. And when you're using the linear space
function or lin space function, the third parameter is the number of points, not the step size.
Lot of people get confused. Okay, if I want six points here, what I'll try to do is I'll try to
divide this line into six equal points, okay? Now, if my first point is zero, my second point
will be zero two. My third point is zero four. My fourth point is zero six. Now look at, look at
it. This is my 123456. I have six points, and the distance between each of these points is zero
two. That's what a linear space means. It's breaking up this whole region into equal spaced
regions. And whatever are the values that you get here, it'll put it in an array, okay? This is
what linear space function does. You can give it a starting and ending point, and you can say
between these two numbers, but including these two numbers here, right? I want six points,
and it will do that for you. Okay? Now, if you want to create, there are other types of very
simple common arrays that you might want to create. So imagine if I say NP, ones, three,
comma, three. Remember, this is a tuple. Now, here, this is the function is the function
arguments, right? And the function argument is a tuple. When I give this, what it will do for
me is it will create a two dimensional array where all the values are equal to one, right? It's
creating three rows and three columns. It's creating three rows and three column array
where all the elements are equal to one, because you're saying NP ones. Similarly, if you say
NP zeros, it will create a three cross three array or three cross three matrix where all the
elements are zero, right? Now let's go and see something slightly different. So there is
something called an identity matrix in mathematics. An identity matrix is a matrix where
the diagonal elements are one. So these are called the diagonal elements because they're
diagonal to the matrix, right? While all the non diagonal elements are zero. Those of you
who studied basic matrices in your 10th or 11th grade would immediately recognize this.
Okay, so this is called a diagonal of a matrix. Anything which is, which goes from top left to
bottom right, that's called a diagonal. Now I can say NP I three. So this matrix is represented
mathematically as I I three cross three. Whenever you say I or identity matrix three cross
three, it means a matrix where the diagonal elements are one and everything else is zero.
That's what this exactly returns. Similarly, now I can say NP I. Now I can say NP I three,
comma, two. It will return a matrix like this for me. Okay, where this diagonal elements go
from top right to bottom left right. So these diagonal elements are one, everything else is
zero, right? Very, very simple. We use identity matrices a lot in mathematics, and you'll
come across this identity matrix when we do machine learning and data science extensively.
Now comes how to construct. So there's something called a diagonal matrix or a diagonal
array, right? What it means is whenever I say NP diagonal and here I'm giving a list, what it
does is it creates a two dimensional array for me, where the diagonal elements, where these
diagonal elements are the values that you have given here. Everything else is zero, all other
values are zero. If you notice, only the diagonal elements, only the diagonal elements are
whatever values you give here. If you give four values, it creates a four cross four matrix
with the diagonal elements being equal to that, right? Extremely simple. Now, if you want to
print the diagonal elements of a matrix, that's also very simple. Now here I have a matrix a
where the diagonal elements are 1234. If I say NP diagonal a, because for matrix a, I'm
asking it the question, what are diagonal elements? It'll return me a numpy array with all
the four values. Extremely simple. Again, excuse me, sorry. Now let's see how to create
random numbers. Because sometimes, actually, a lot of times in machine learning, we will
have to create random arrays and random matrices because there are some powerful
properties of random matrices and random vectors that we'll learn later. But what I can do
here is I can say NP random rand, okay, this is a function, and this is the package it is in. If I
say NP random, Np is your numpy package. Right? Random is your sub package, and rand is
your module. Sorry. Random is your module. Rand is your function. Now, if I say NP random
rand four, it returns me an array. Returns me an array of size four. This has four elements,
right? And these elements are random numbers. We will learn in exploratory data analysis
and in property and statistics that there are different types of random numbers. This
random number is called a uniform distributed random number. There is other type of
random number called a Gaussian distributed or a standard normal variate if you want
those values, because there are different types of random numbers. Okay, whenever we
think of random number in general, we think of it as uniform random variable. If you do not
understand what uniform random variable or what a Gaussian or a standard normal
variable, please check out our chapter called probability and statistics, where we explain
what these are. Right? And if you do not understand, it's okay. Just remember that you can
create random numbers using NP random, either the function rand or rand normal, which
stands for rand n. Okay? If you have already gone through a probability statistics chapter,
this should be easy. If you have not, you'll understand what these two types of random
numbers generators mean when we learn probability statistics. Okay? Having said that,
okay, let's understand some more details of numpy because as I told you, numpy is
extremely important. Okay? So look at like this. Whenever I say NP arrange ten, what it's
creating is a numpy array going from one to nine. Right? Now, I can use this attribute, which
is a dot data type. Dtype basically means data type. What it tells me is the data type of this
numpy array is integer 64 bit. But if I want float, I don't want integer values, I want a float.
Then what can I do? When I'm creating my array, I can just say dtype equals to float. And
whenever you have dtype equals to float, you have your numbers represented with a dot.
What this means is zero point basically means 0.0 or eight point basically means 8.0. Okay?
So here I'm saying my data type, I want it to be float instead of Int. So it's a 64 bit float here.
So your numbers will actually, when you print it, it actually puts a dot here. So whenever
you see this dot, it means it's a float. If you don't see it, it means it's an int when you print it.
Okay? Very simple. Now you can. So a couple of caveats here. So if you have NP zeros, three,
whenever you're using the zeros or the ones functions, it always creates float data type, not
int by default, right? Because ones and zeros are often used for mathematical computation,
where it's better for you to use float instead of Int. Now let's look at some very interesting
data type. So I can create a numpy array with complex data type here. These are complex
numbers. Those of you who come from areas like physics, or you might have learned about
complex numbers in physics or mathematics in your 11th and twelveth grade or electronics,
lot of these fields actually use complex numbers a lot. In machine learning. We don't use it a
lot, but it's good to know that there is a data type called complex. So if you say d data type, it
actually creates a complex data type, which is a special data type in python. Sorry, in numpy.
Now you can also create a boolean. You can also create a boolean numpy array just like this.
Or you can create a string numpy array, right? So when you say what is the type? It says s
six. Now what does s mean? You can just go and see here. B is for Boolean, I is for signed
integer. S is for string, right? So whenever it prints this, you can actually understand it using
the simple code. Of course you can go to the Python documentation to understand all the
basic data types. Here is the link to the Python documentation. Now, having learnt all of this,
how to access elements in a python, in a python array or an umpire array, right? Suppose,
imagine if I have an umpire like this. This creates an array from zero to nine. Now when I
say a five, it returns me the fifth value. So the indices always just like in cc plus plus, this is
my first element, my second element, my third element, fourth element, fifth element, so on
and so forth. My indices or my locations start from index zero, not from one. It's very, very
important. The first element is not at index one, it's at index zero or location zero. Right?
Now, similarly, if I create a diagonal element, if I create a diagonal matrix here, what I'm
creating, I'm creating a diagonal matrix. 12300. Right? Now if I say what is the value at a
two, comma two, which means second row, which means second row, this two basically
means the row. This basically means the column. So second row and second column, the
value is two. Oh, sorry, my bad, my bad. So second row, second column, this is zero ETH
row, first row, second row, this is zero ETH row, first row, and second row. My mistake,
sorry. Right, so two, comma, two means row two and column two. And the value will be
three as it's printed here. Sorry, my bad, my mistake. I stay corrected. Okay, having done
that, now let's go and see what is. Okay, let's look at this. This is very interesting. So we have
a diagonal matrix of one, two, three, which means we have 100-2003 and this index is zero.
This index is one, this index is two. This index is zero. This index is one. This index is two
rows. This is row zero, row, sorry, column zero, column one, column two. Now, if I can say
eight, two, comma, one is five. So what is second row? R two is this, right? And one column
one is this. So I'm saying this value, change it to five. I'm assigning a value here. I'm saying r
two and column one, change it from whatever value it is. Assign value five here, right? So I
basically put five here and this is what I get, right. Very simple. Now that's how you assign a
value to an umpire. Now let's look at related stuff called slicing. Slicing is a very useful thing.
So for example, if I have an umpire array like this with numbers going from zero to nine
sequentially, this is interesting. I can now get a subarray of this by doing something like this.
So I have three values here, right? The first value is called the start index. The last value is
called the end index. The third value is called the step. All of them are separated by a
column. Very, very important. All of them are separated by a column. So when I say this,
what happens is it takes the first element. So this is at index zero, index one, index two,
index 345-6789. These are the indices, right? So I'm saying start with the first index, which
means take this value. But instead of stepping here, I'm saying that my step size is two,
right? Which means now step two elements. Take the next element. Step two, again, step
two up to the 8th element, but without including the 8th element. Now, if I take step size
two, I go to nine, right? So don't take that, right? So this is my start, this is my end, and this is
my step size. When I do that, what is the subarray I get? I get a subarray of one, three, five
and seven, which is what you get when you print this. You get 1357 as a subarray, right?
Very, very simple. So this is a very, very interesting form of getting subarrays the way you
need. Right? Now, similarly, if I say, let's look at a slightly different operator right here.
When I say Np, arrange ten, I have basically 0123 so on and so forth, up to ten, sorry, up to
nine. Right? Now I can say a five column. If I leave this empty, it means up to the end, up to
the end of the array. So here, what I'm saying here is change from the fifth element to the
last element, make all the values equal to ten. So this is my first zero, th element, first
element, second element, third element, fourth element, fifth element, seven, eight, nine. So
what I'm doing here is from fifth element up to end, change all the values to ten. And that's
what it has exactly done, right? Very useful. Now, this is what my array a contains. Now I can
do some more interesting things here. I'm saying b equals to Np arrange five. So what does b
contain? 012345 elements. Right? Literally. Now if I do this type of indexing, this type of. So
when I say B column, column minus one. So what does column here mean? It means from
the start, because I left it empty, right? From the start or from zero here, I left it empty,
which means end, which means the length of the array. Here I'm saying minus one. What
minus one means is, see, when I said two or when I gave any other value, it is basically
stepping in this direction, right? If I gave it two, it would step two, two steps, right? Now
when I give it minus one, it will step in the reverse order. So minus one basically means the
reverse order here. Now what b, B column, column minus one is equal to basically take this
array and process it in reverse order. This will create an array which is 43210. This is what
it will create. Now I am saying a five column, which means from the fifth element onwards, I
want this to be equal to the reverse of B, right? So from fifth element onwards, I want it to
be so what it has done is it has taken this reverse value, it has copied it here, right? Here.
This is how you get reverse. And this is how we assign. We saw the assigning part, right?
This is slightly more complex line, but it's important to understand. Okay, now there's an
interesting method in which all these subarrays are stored using copies and views. Okay,
let's look at it. It's again not very, very complex. Imagine if I have a numpy array. I have a
numpy array here from size zero to nine, from value zero to nine. And when I say column
column two, it means from start to end with a step size of two. So it will create this subarray
for me. Now here is the fun part. And this subarray I'm storing it in B, right? So there is this
function called NP shares memory. Now a is one array, B is another array. Now what's
actually happening when you're doing this? When you're doing this slicing, what's actually
happening is in the memory. It is storing in the memory. In the memory. A is, this is in your
ram, right? This is in your ram. In your ram. A is pointing to this. Now what actually does is
for efficient space, B also points to the same location, but not to the same array. It points to a
subarray. So b now is this subarray, this subarray is basically this element, this element, the
fourth element, so on, so on and so forth. So internally, for memory optimization, Numpy
stores points to the same memory location, but using something called a view. So view
basically means I'm not reproducing, I'm not creating a copy of the array. Instead of creating
a copy and array copy of the array, I am using some other methodology. So that, the
methodology is called creating a view such that I am being space efficient so that I don't
copy the same thing and I eat up space. Right? So how can you check whether they are
taking the same space or not? You can just use the function NP shares memory. If you give
two arrays. So here it returns true, which means they both are sharing the memory.
Literally they're sharing the memory. Okay. So when two variables of different names share
memory, they're probably sharing it using the concept of views. How it is internally
implemented is beyond the scope of this class because for that, you'll have to see how
numpy's designers implemented views internally. There are multiple techniques on
implementing views. Okay, now let's do a slightly different thing now I have now because A
and B are pointing to the same memory location. If I say b zero equals to ten, okay, and if I
print B, my b used to be 02468. Right? Now I changed my b zero to ten. So my b looks like
this, but my A also will get modified. My a when I print now is no more. 0123. It's basically
ten, one, two, three, because A and B are pointing to the same location in memory. And
hence if you modify B, your A also will get modified. This is a very, very important thing
because you might imagine that only B will get modified and a will not get modified. No. If
you are slicing, a and b share the same common memory space, and hence you'll find this.
What is the workaround? If you want to force a copy, you can say that. Okay, I want this.
And if you use the copy function, then if you say shares memory, they don't share memory
anymore. So when you do the copy function, a will point to its own location. C will point to
its own location. If you want to force a copy. Right? Now, if you change C, a will not get
changed as you expect. Here I'm changing c zero to ten, but my a zero still remains zero,
right? So if you want to force copy, you can do it using copy function. Now here is something
called a fancy indexing. This is actually very, very interesting. Imagine, imagine if I have
some random numbers. Okay? Suppose if I have some random, again, this is another form of
creating random numbers, okay? So what this says is create random numbers in the range
of zero to 20. And I want 15 such numbers. So you have 15 random numbers here, and all
these numbers lie between zero to 20. Now, I can create something called a mask. Let me
explain what mask is. Now, here, look at it. This is a boolean expression. I'm saying if a
percentage two equals to zero. Now all of this is in an array called a, a percentage two
equals to zero. And I'm equating it to something called as a mask. Now, if I say a mask, what
happens is it'll evaluate this logical expression on each of the elements here. Okay, it'll
evaluate. So what does a percentage two equals to zero basically mean? It means that the
values have to be even numbers, because only for even numbers, the modulo two operation
returns zero. This is called the modulo operation or the reminder. Right? So when I say
amask, what it does is it applies this mask operation, this boolean. It evaluates the mask,
boolean operation on all of these values. And wherever it returns true, only those values
will be kept here. Rest of them will not be kept. For example, I'm saying extracted from a
equals to a mask. When I print extract from a, what I get is basically all even numbers. So
you can actually create a subarray. You can actually create a subarray using masks. And
what masks do is masks create copies and not views, which means it's like a deep copy, the
way we have dot copy function. Masks will not create views. Okay? That's important to
notice. Now, there is another way of creating a mask. Now remember, look at the use of
mask. Actually, now I can say a mask equals to minus one. What happens now is all the even
numbers will become equal to minus one. So this is one way for me if I want to say take all
even numbers and convert them to minus one. I can create this mask the way we have
shown earlier. And I can say a mask equals to minus one, right? Similarly, let's look at this.
There is another form of indexing that we can try. It's called indexing with an array of
integers. Suppose if I have an numpy array, right? I'm arranging zero to 100 with step size
ten, because it's an NP arrange. So I have value 00:10 2030, so on and so forth. Now I can
create an array. I can create a new array like this, right? It's an array in which here I'm
giving a list of indices. So I want the second index, third index and second index repeated.
Fourth index, and again second index repeated. When I do this, what I get here is this is my
index one, index 345-6789 so I'm saying index two, index two is 20. So it creates value 20
here, index three, it takes this 30 and creates here again index two. So again 20, index 440,
index two again, which means 20, right? You can repeat these indices. There will be
instances where you have to do this, right? Similarly, of course, I can also assign new values.
I can say a of. Remember here, I'm putting this in a list here. I want a nine and a seven, both
to be assigned a value of -200 right? Then I can put this in a list here, and I can say a seven
and a nine. I want them to be assigned a value of 200. This is another way to do it. So with
this, we have gone through lot of operations. I know this has been quite a long video, but I
wanted to go through all the operations that are available in numpy and these operations.
Numpy we will use extensively, I promise you. And as we have seen, Numpy is a very, very
efficient, both in terms of memory and in terms of speed for doing operations on multi
dimensional arrays.

You might also like