We are going to discuss the NumPy.
So NumPy is a linear algebra library for Python. The
reason why it is so important for data science is that it covers almost all of
the libraries in the PyData ecosystem, which relies on NumPy as one of their
main building blocks.
So one more thing, one
more advantage we have, NumPy is also incredibly fast as it has bindings to the
C libraries.
Okay, so first we are
going to talk about the installation
process. So to do that,
if you are using a Jupyter
Notebook, you just need to do
conda install NumPy. By using conda install NumPy, your NumPy will be
installed.
And if you are not using Jupyter Notebooks, so simply go to your PowerShell and
just install NumPy. As your NumPy is installed, now you need to do to import it
in your program. So to do that, you just need to do import NumPy as NP.
So NumPy has built-in
functions and capabilities, and we won't cover them all. Still, instead, we
will focus on some of the most important aspects of NumPy like vectors,
using arrays, matrices and number generations.
So NumPy array zation surely come in two flavors like what is our
matrices and vectors are strictly 1-D arrays, and matrices are 2-D arrays.
So how to create the
NumPy arrays? This is the variable list name off my list, and here I
have three elements, okay, one, two and three. And if I just do 'my list' right
over here and do Shift +Enter, it will take the output one,
two, three.
Okay, so now the same
thing I want to do that I want to create an array and I want to convert my list
into my array. To do that, you just need to do NP dot array. Right, so this is the feature which we are
going to do, NP dot array. Why NP? Because we have imported NumPy as NP.
So NP is the short form
for NumPy. So that's why we are using NP dot array. And inside the parenthesis, we will pass the
parameter of our list name, o which is our minus.
Now you can see the
changes. First, when we execute the list, it shows 1,2, 3 with the square
brackets, but now, it shows within array written over here. The array is also
there and the parentheses and inside it, we have the list.
So if you do that with
your matrix by taking it one, two and three, four, five and six, seven, eight
and nine, so these are the three lists which I have included, nested list. This is a listed list, and if you run that, you
will see that, one, two, three, four, five, six, seven, eight, nine.
So this is the execution
of our nested list. Right, if I do the same thing same function like
NP dot array and write inside the parenthesis my nested list name. So my list and you will see that it has been
converted into an array just like this one, one, two three, then four, five,
six, seven eight nine.
So basically, it has
three rows, right. And it has only a single column, so this is my entire
column. So it has been converted into my matrix form, right.
Let's talk about the built-in methods. There are lots of integrated ways to generate
arrays. The first one which we are going to discuss is a range.
What does it do? It returns evenly-spaced values with a given interval.
So I have done that NP
dot a range from zero to ten. So I want to include elements starting from zero
and will end at ten. So just keep this thing in your mind that whenever you are
giving a stop parameter, it will terminate as soon as it finds the ten. It
has started from zero, and it has ended in nine.
So similarly, now we
have another thing, just NP dot a range. And now I am adding the third
parameter, which is our step
parameter. This parameter
helps you to jump to the step you want to do. In my previous example, you can
see that it has been growing up with the increment of one, but now you have
given a step parameter, and it has been located with the value of two. Now, it
will be growing with a two-step, right.
So we execute this code,
then you will find that zero, two, four, six, eight, ten. And see you have
terminated at 11. So it has been terminated with one previous value. So your
array has been executed, starting from zero and ending to ten.
Okay, now want to create
zeros and ones, you want to generate arrays of zeros or ones. So to do this,
you need to do simply just NP
dot zero and number of
zeros you want in your array, just give the value. So now, you can find that
your array has become with the three elements of zero.
Okay, if you want to
build a 2-D array or two-dimensional array, we have only one thing, one
parameter. So it is defining only your rows, you don't have any columns. But in
the second example, we can see that NP dot zeros, five comma five, right. So,
the first one is for my rows, and the second one is for my columns.
There are 25 elements
now in your array, including 25 zero starting from row 1 to 5 and there are
five columns as well. So this is how we can generate arrays of zeros and ones.
Similarly, you can also create ones, right.
And there is another of
a 2-D array. So three comma 3, 3 rows and three columns. Now you can find the
nine elements, right. Why
there is a function of 0 and 1? Because
there are some functions where we need to know whether it is true or false. So
0 represents our false value, and 1 represents a true one.
Now we want to see about
the linspace. So it returns evenly spaced numbers over a
specified interval, right. NP dot linspace, what does it mean? Linear space, linspace simply meaning linear space.
I have given a start
parameter of 0, and ending parameter is ten, and the 3 is not my step parameter
at this time. Okay, this is the number of elements you want in your array,
right. So from 0 to 10, I want only three elements, and it should be equally or
evenly spaced. If you execute this, you will find that 0 and 5 are there along
with 10. Okay, so the difference between 0 and 5 is equal, and 5 and 10 are
also identical.
So your array has been
divided into three equal parts, right. For a stop parameter, I have told you
that in a range, it doesn't get to stop parameter but terminates one value
before. But in linspace, it takes the last value also which we have included.
So in the array, you can
find the same interval and is evenly spaced, right. So this is what our
linspace do.
Let's check out with the eye. So the
eye creates an index matrix. So if you do NP dot EYE,
you will create an index
matrix. All your
diagonals values are one, and rest values will be zero.
How to create random
value? How
do you generate random numbers? Okay,
there is a module named 'random' in our NumPy. So this random has many features
like rand, randn and we are going to discuss it one by one.
To create an array of
the given shape and populate it with the random samples from a uniform distribution. So now I have NP dot random. You need to call this random value because the randn is
a function of this random module. You need to call, this you can not do just by
NP dot rand, you need to NP dot random dot rand 2.
So what does it will do?
It will generate two random values, but keep this thing in your mind that it
will generate between 0 and 1. I have created 25 elements by giving five rows,
and five columns and all the values are between 0 & 1 interval.
Let's talk about the randn function. It returns a sample from the standard normal
distribution unlike rand, which is uniform, right. If you do NP dot random dot randn, it will generate values, which can be negative as
well as positive. It can take any values, and it is not that it will only take
values between 0 & 1, whether it will be negative or positive.
Let's now discuss array attributes and methods. Okay, if you want to change the shape of your
matrix you want to convert your rows into columns or your columns into rows, so
to do that, you just need to reshape.
There are other methods
like max, min, argmax,
argmin. These are all the useful methods for finding the
maximum or minimum values. And argmax and argmin just find the index location of your maximum
value or minimum value.
So in the first example,
I am taking ranarr. So this is my array elements, 10, 12, 41, 17, 49,
2, 46, 3, 19, 39. And now I want to find the maximum values out of this array.
So ranarr dot matrix name and dot max can
do your job. So it is 49 right here.
And if you want to find
the index of your maximum value, just use dot argmax and the parentheses, it will be four. So check the
index, zero indexes to the fourth index, and 49 is the highest value over here.
So it returns the index of your highest value.
Similarly, ranarr dot min will give you a minimum value. And argmin will find the index of your minimum value.
Now we are going to talk
about shape. So shape is an attribute that arrays have, right.
So if you want to check the array, like over here, I have taken an example of
this vector one, arr dot
shape. It gives a shape of 25
because we have 25 elements on our rows, we have no columns. If you want to
change it, you can just use arr
dot reshape to 1 to 25.
Now it will be converted into one row and 25 columns.
Another thing to know is
the type of data which we are having in our arrays. So to do that, you just
need to do arr dot dtype, and you will find what kind of integer or data is
there. So we have the integer values over here, so 1, 2, 3, 4, 5, 6, all our
integer. The dtype is returning 64-bit number value.
So that is all about NumPy arrays. Because there are some functions where we need to
know whether it is true or false, so 0 represents our false value, and 1
represents a true one.
Now we want to see about
the linspace. So it returns evenly spaced numbers over a
specified interval, right. NP dot linspace, what does it mean? Linear space, linspace simply meaning linear space.
I have given a start
parameter of 0, and ending parameter is ten, and the 3 is not my step parameter
at this time. Okay, this is the number of elements you want in your array,
right. So from 0 to 10, I want only three elements, and it should be equally or
evenly spaced. If you execute this, you will find that 0 and 5 are there along
with 10. Okay, so the difference between 0 and 5 is equal, and 5 and 10 are
also equal.
So your array has been
divided into three equal parts, right. For a stop parameter, I have told you
that in a range, it doesn't get to stop parameter but terminates one value
before. But in linspace, it takes the last value also which we have included.
So in the array, you can
find the same interval and is evenly spaced, right. So this is what our
linspace do.
Let's check out the eye. So the eye creates
an index matrix. So if you do NP dot EYE,
you will create an index
matrix. All your diagonals
values are one, and rest values will be zero.
How to create random
value? How
do you generate random numbers? Okay,
there is a module named 'random' in our NumPy. So this random has many features
like rand, randn and we are going to discuss it one by one.
To create an array of
the given shape and populate it with the random samples from a uniform distribution. So now I have NP dot random. You need to call this random value because the randn is
a function of this random module. You need to call, this you can not do just by
NP dot rand, you need to NP dot random dot rand 2.
So what does it will do?
It will generate two random values, but keep this thing in your mind that it
will generate between 0 and 1. I have created 25 elements by giving five rows,
and five columns and all the values are between 0 & 1 interval.
Let's talk about the randn function. It returns a sample from the standard normal
distribution unlike rand, which is uniform, right. If you do NP dot random dot randn, it will generate values, which can be negative as
well as positive. It can take any values, and it is not that it will only take
values between 0 & 1, whether it will be negative or positive.
Let's now discuss array attributes and methods. Okay, if you want to change the shape of your
matrix you want to convert your rows into columns or your columns into rows, so
to do that, you need to do reshape.
There are other methods
like max, min, argmax,
argmin. These are all the useful methods for finding the
maximum or minimum values. And argmax and argmin find the index location of your maximum value
or minimum value, respectively.
So in the first example,
I am taking ranarr. So this is my array elements, 10, 12, 41, 17, 49,
2, 46, 3, 19, 39. And now I want to find the maximum values out of this array.
So ranarr dot matrix name and dot max can
do your job. So it is 49 right here.
And if you want to find
the index of your maximum value, use dot argmax and the parentheses, it will be four. So check the
index, zero indexes to the fourth index, and 49 is the highest value over here.
So it returns the index of your highest value.
Similarly, ranarr dot min will give you a minimum value. And argmin will find the index of your minimum value.
Now we are going to talk
about shape. So shape is an attribute that arrays have, right.
So if you want to check the array, like over here, I have taken an example of
this vector one, arr dot
shape. It gives a shape of 25
because we have 25 elements on our rows, we have no columns. If you want to
change it, you can use arr
dot reshape to 1 to 25.
Now it will be converted into one row and 25 columns.
Another thing to know is
the type of data which we are having in our arrays. So to do that, you need to
do arr dot dtype, and you will find what kind of integer or data is
there. So we have the integer values over here, so 1, 2, 3, 4, 5, 6, all our
integer. The dtype is returning 64-bit number value.
So that is all about NumPy arrays.

Comments
Post a Comment
Please do not add any spam link in the comment box.