Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to discuss about a very important library which is called as numpy.
So till now you can actually see we have covered the advanced Python concepts where we discussed about
iterator generator and decorators.
Now we are moving to a new module which is called as data analysis with Python.
So here we are going to focus on some of the libraries like numpy pandas, some visualization libraries
like matplotlib, seaborn which are fundamentally used for data analysis.
And out of all this particular library, numpy is super important because even in deep learning modules
also we will be using numpy some or the other way.
NumPy is uh, like with the help of the library of numpy, you will be able to see that we will be able
to create arrays, we will be able to work with matrices and all this operation that we specifically
do like mathematical operations.
It will be very, very fast.
Okay.
So in this video we are going to discuss about numpy, how to properly create arrays, how to work with
arrays, how to work with matrices with the help of NumPy itself.
So first of all, as usual we will go ahead with the definition.
So here you can see numpy is a fundamental library for scientific computing in Python.
It provides supports for arrays matrices along with a collection of mathematical functions to operate
on this data.
Structures in this lesson will cover the basics of numpy focusing on arrays and vectorized operations.
So first thing, whenever you create an environment specific new environment, numpy is not by default
installed.
So there are two ways how to probably install it.
First of all, go to requirements.txt.
Go ahead and update numpy and just go ahead and in your terminal you just need to go ahead and write
pip install r requirements.txt, which I've already shown you.
One more way.
Is that directly in the Jupyter notebook file itself you can just go ahead and write pip install.
Okay.
So let's say I will just go ahead and write pip install numpy.
And when I write this you'll be able to see the installation will happen.
And already since I'm using the same virtual environment.
So here it shows that requirement is already satisfied.
So when we go ahead with the further sessions.
So like whenever there is a library that is required, we will be installing either in this way or directly
updating in the requirements.txt.
So this is the first step.
Now as I said, what exactly is numpy used for.
It provides supports for arrays and matrices.
So first of all for using numpy we need to import it.
So for importing it I will just go ahead and write for numpy as np.
So NP is nothing, but it is just an alias for numpy itself.
Right?
So we don't have to write numpy dot at every places.
We can directly use np dot.
Okay, so this is the first thing that we really need to do.
Now we will go ahead and I'll show you how to create arrays using numpy okay.
Now first of all we will go ahead and create a 1D array okay.
So a simple 1D array a one dimensional array.
How it is basically created.
I will create a variable called as R1 and here I will write NP dot array.
So NP dot array is a function which will actually help us to create an array.
And inside this I can give list I can give tuples.
Let's say if I just give one list like this 12345 okay.
Now if I go ahead and execute or print this array okay.
So here you will be able to see that as soon as I execute this I am able to see that I'm getting an
array itself.
Right.
Print R1 uh, and uh, by default you will be thinking that this is a kind of a list format.
No, it is not.
So if I go ahead and see or print the type of R1, okay, a R1.
So here you will be able to see that it is a numpy dot ndarray n dimensional array.
Okay.
Uh, if you also want to see the shape of the array like what exactly is the shape of the array.
So I can go ahead and write a R1 dot shape okay.
If I execute it here, you can see it says that hey it has five comma blank.
So it is a single dimension.
Whenever there is a single dimension, only one number will be available.
Okay.
Like one comma dash like that it will be blank.
So whenever we have blank on the other side it is basically talking about single dimension here.
No specific rows or columns are there.
Okay.
Let's say I want to convert this into a rows and columns.
Let's say I'm right now many people will be thinking, hey, these are like one column, one sorry,
one row and five columns.
Nothing like that.
Whenever we work with single dimension array there, we don't try to provide columns or rows right there.
It is just treated as a single dimension array with five elements.
Okay, let's say that I want to probably go ahead and do one thing.
Let's say this is my array that I've actually created array two okay.
So now I want to go ahead and reshape it reshape it to a two dimensional array.
So what I can do I can go ahead and write R two dot shape okay.
And here uh Um, I will just go ahead and write one comma five.
Let's see whether this will get executed.
So here it says tuple object is not callable.
The reason is very simple.
Here we need to provide with this particular brackets.
No worries.
Um tuple object is not callable.
Let's see.
Array dot.
Sorry I wrote reshape I wrote shape okay it should be reshape.
My.
Sorry my mistake.
So it should be reshape one comma five.
Now you can see that very much simple right?
A single dimension array is basically having one braces entry and one braces closing.
Right.
But here you can see there are two braces that is basically getting opened and two braces that is basically
getting closed.
So this becomes a two dimensional array.
In three dimensional array you will be able to see that there will be three braces that will be opening
and three braces that will be closing.
Okay.
So this is how you can actually identify.
Now since I have told hey, I need to reshape this five elements to one comma five, one comma five
basically means one row and five columns right?
Understand this one comma five.
Basically means one row and five columns.
And anyhow, if I probably go ahead and see here the total number of elements will be five only.
Right.
So that is the reason we have reshaped to one comma five.
If I go ahead and reshape to one comma four, it will not work.
Why?
Because there are five elements.
How can I reshape to one comma?
Four.
Like one row and four columns, the total should be five elements.
Right now only four elements are basically there.
So here I will just go ahead and write one comma five.
This basically means one row and five columns right.
So total how many elements it is becoming five elements right.
I will not add one plus five.
Don't do that mistake okay.
So once I am reshaping it I am able to get this okay 12345.
Now you may be thinking can we go ahead and create like this directly?
Yes, you can do it.
So if I go ahead and write R two dot and if I use NP dot array and here let's say I go ahead and use
my nested list.
Right.
So I will go ahead and just use a list within a list.
Okay, so here you can see that I will be able to get an R two dot shape if I do.
Dot shape.
Then again here you will be able to see I'm getting one row and five elements.
What did I do?
I just used list inside a list.
Now let me do one thing.
Let me just go.
Ahead and print like this and let me add more elements.
Now I'll go ahead and add one more list.
Okay.
Like let's say 2345.
Right.
And this also basically has five elements two three, four, five.
Now if I go ahead and print R two and I also go ahead and print my RR2 dot shape.
Okay.
Now if I execute it here, you can see this time I'm getting the first row and the second row.
All the elements 1234, five is present in the first row 23456 is present in the second row.
That basically means how many.
Column 12345 columns.
Right.
So the shape will be two comma five.
by two comma five, because there are two rows and five columns.
Now I hope you are able to understand this is also a two dimensional array.
YC2 times it is opening over here and two times it is closing over here right.
So it becomes a two dimensional array right.
So I hope you got an idea.
And this array, this array what I have actually written is nothing but 2D array.
Two dimensional array.
Right.
So it is a 2D array.
Right.
Single single dimension.
Array is nothing but this.
Right.
So it is a 1D array.
I am just going to write down the comment so that you can refer this 1D array.
Okay.
Perfect.
So 2D array a 1d array.
We have discussed about it.
Now let us go ahead and create a arrays with some of the inbuilt function okay.
So for this I will just go ahead and use NP dot arrange function.
Now inside this arrange function let's say I want elements between zero comma ten.
And the third parameter that you will probably be seeing is step okay.
So if I go ahead and write two, that basically means I have to skip every one element and go to the
second element and display it.
So what it is getting displayed between 0 to 10 02468.
Now there are five elements.
If I want to reshape this into a two dimension, I can basically do.
Since there are five elements I can do five comma, comma 1 or 1 comma five.
If I do five comma one, what does this basically mean?
That basically means I have five rows one, two, three, four, five, and one column.
Right?
So this is how we can actually reshape or convert a single dimension array into a two dimensional array.
Right.
So this is how we can basically do.
Now we're also going to check out some of the inbuilt functions that are present in numpy array.
So one is NP dot ones.
So if I go ahead and use NP dot once with three comma four, that basically means it is going to create
a two dimensional array with three rows and four columns.
And all the elements is going to be one.
So see over here.
So three rows one, two three and four columns.
Now, why you may be thinking why we have this function.
See, when we see in deep learning, right.
Some of the some of the inbuilt functions of numpy, right.
Like NP dot ones or NP dot zeros.
Let's say I have to initialize the initial weights to zero.
I can directly use this inbuilt function and create it over there right so quickly it helps us to create
all these elements.
Otherwise I had to write multiple lists over here and then probably convert that into a numpy array.
Right now this was one uh, there is also an option to create, uh, an identity identity matrix or
an array.
And here for that we use NP dot I.
And if I just say hey what is identity matrix.
It's like all the diagonal elements will be one remaining.
All will be zero.
Let's say if I do three cross two then here you can see in diagonal elements I have one remaining.
All the elements are zero.
So this is basically an identity matrix okay.
So I hope you have got an idea with respect to NP dot array how to reshape it, how to see the type
and what all things you really need to do while reshaping it.
Okay, so all these things have been distributed or spoken about, right?
Again, I will quickly write this kind of code over here so that you'll be able to see this.
Array is array shape.
You want to see number of dimension.
You want to see two dimension or one dimension.
You can use n type size number of elements you want to see.
You can go ahead and write array dot size.
If you want to see the data type of that particular array, you can just go and write r dot dtype.
Output may vary based on the platform.
It can come in 64 and 32.
It depends on the platform item size in bytes like how much bytes is specifically used.
Okay, so all these things are there.
So let's go ahead and execute it.
So here you can see 123456 shape two comma three.
This is int32.
This is my system.
So I will just go ahead and write int32 in your system.
It may vary to some other value based on the operating system that you have right.
So perfect.
This is mostly about some of the amazing attributes.
I'll write down a comment.
Uh Builds attributes off off of numpy array.
Okay.
So perfect.
So here you have this right.
Now let's do some kind of vectorized operation.
Okay.
Vectorized operation.
So numpy.
NumPy vectorized operations okay.
Now what I am actually going to do is that I'm going to perform some amazing operations over here.
And here.
You can go ahead and create this quickly okay.
NumPy vectorized operation.
So first of all I will go ahead and write array array one.
Let me just go ahead and create NP dot array.
And here I'm going to probably give some elements like 12345 okay.
Uh again I will be using another array which will be NP dot array.
And here I'm going to basically use ten, 20, 30, 40 and 50 okay.
I've just defined something over here right.
So first of all I will go ahead with element wise addition.
Okay.
Element wise addition.
And here I'm just going to go ahead and print.
This will be my addition.
And here let's go ahead and let's say I will go ahead and write R1 plus R2.
Right.
So if we really want to perform element wise addition, that basically means what this one will get
are added to this ten two will get added to 20, which is present in the same index inside that particular
numpy array, that all elements will get added.
And directly.
We can do that just by using this addition operation.
Perfect.
Now what I will do, I will go ahead and also perform element wise sub subtraction okay.
So let's go ahead and do that.
So here I'm actually going to write print subtract subtraction colon.
And here uh let's go ahead and do this uh comma r1 minus r2.
So with respect to element wise subtraction we can basically do this.
Similarly if you want to do multiplication.
Again all you have to do is that just do the same.
Copy paste over here and just write r1 multiplied by R2.
And similarly you want to do division.
You can also do that perform modulation operation whatever you think you really want to do.
So all this the best thing about numpy is that they are just going to do element wise.
Element wise basically means the element that is present in the same index automatically this will be
considered.
So if I go ahead and execute it here, you can see this is all my output that I am able to get okay.
And remember one thing that all the return type will also be an array.
Okay.
So this is one very amazing thing.
Let's talk about some of the universal functions.
Universal functions basically means some functions that applies to the entire array.
Right.
So let's say I will go ahead and write RR and NP dot array.
And here I'm going to basically use an element Five, five, six.
Okay.
Let's try.
Now, what I'll do first, uh, universal uh, function that I'm going to apply, which is available
in NP dot array.
It is nothing but print.
Uh, let's say I want to perform square root.
So NP dot sqrt function is basically there.
And here I'm going to just give the array right.
Automatically the square root is going to get performed.
There is also a function which is called as exponential right.
We can also perform this.
So for that we'll go ahead and write np dot exp exp exponential.
And here I'm going to basically use this array okay.
Next thing I will just go ahead and perform sign.
Now the best thing is that whatever operation I'm applying this will also get, uh, applied element
wise, right.
So here I can also go ahead and use NP dot sign.
Okay.
Sign sign sign sign.
And here also I can go and give my array okay.
So this is also another inbuilt function.
And similarly you can go ahead and explore a lot.
So I'll just go ahead and apply natural log also.
And for that I will just use print print or NP dot log.
Okay I'm going to do this.
And here is all my function okay.
Sorry NP dot log of array okay.
So here you can see with respect to all the values all this operation is basically taking place okay.
Uh similarly mathematical equations also you can perform it is up to you whatever things you really
want to perform now coming to the universal function.
So let's go ahead and write my universal functions.
Now inside this universal functions you funks.
I'm just going to basically go ahead and define uh.
Now see uh sorry universe function is done.
Right.
So let's go ahead and um do the slicing operations array slicing and indexing okay.
Array slicing and indexing Okay.
So we will go ahead and perform this entire operations with multiple examples.
So guys now let's go ahead and perform array slicing and indexing operations.
And here let me just go ahead and write R is equal to.
Let me just go ahead and create a two dimensional array.
Now this time while I'm creating a two dimensional array I will take some of the elements like one,
two, three, four, five, four, four.
Then I will go ahead and create my another list where I will take five, six, seven, eight.
And let me just go ahead and take nine, ten, 11, 12.
Okay.
So here in short you have three rows and four columns right.
So let's go ahead and print this array.
Okay.
So here I'm just going to use array colon let's say new line.
And here I'm going to just display this array itself.
Okay.
Now let's go ahead and access the element.
First of all I will just go ahead and print this.
So here you can see array takes 1 or 2 positional argument.
Um.
Array.
Okay.
This was the problem.
Let me just close this up.
I have to use a two dimensional list over here.
So this is specifically my array.
Okay.
Now my question is that how do I pick up or access a single element from this particular array?
Okay.
Now, uh, here what I'm actually going to do here, uh, specifically, let's say I want to go ahead
and pick up one element.
Okay.
Now, one very important thing over here is that you should know the rows and columns, right?
This is how your entire two dimensional array is basically built on rows and columns.
Let's say I want to pick up this particular element array of one right.
This one element.
So this is my array right.
Now what I will do I will just go ahead and see which index.
Let's say I have to probably go ahead and check out which row it is present on.
So right now zeroth row right.
So if I go ahead and write zeroth row I will be able to get one, 234.
Right.
Now out of this Which column I have to probably retrieve.
I need to retrieve this one value.
So obviously it is available in the zeroth column.
So always remember indexing basically starts with zero and 012.
And you always need to probably look at whenever you are accessing any values, right.
You have to access in such a way that you have to focus on the indexes.
It can be the row index or it can be the column index.
So once I go ahead and execute it here, you will be able to see that I'm able to get the answer okay.
Now let's say um I want to probably go ahead and pick up elements like seven 811, 12.
Now how do I do that now?
Seven eight 1112 okay.
Seven eight 1112 understand it is available in which row?
It is simple that it is available in second and third row right now.
Second and third row.
How can I write?
Okay I can basically say start from second row, go till the end of the row.
Right.
So it can be any number of rows.
So seven and 11 I will be able to pick up this two columns.
Right.
So sorry this two rows.
So if I go ahead and execute it.
So two columns zero one okay I have to write one colon.
I'm extremely sorry.
So here you can see I am able to get 5678 and 910 1112.
Now with respect to column I have to focus on which column.
0122 and last.
Right.
So here I will go ahead and write two colon.
Right.
So once I do two colon what will happen.
I will just go ahead and pick up this particular columns.
And out of all these results what I'm actually going to focus on I'll just be focusing on this column
and this column so that I get seven, 811, 12.
So once I go ahead and execute it here, you'll be able to see that I'm able to get the answer right.
Very much easy.
Very much amazing over here.
Now let's do one more exercise.
Okay.
So here, uh, let me just go ahead and write print and let me go ahead and write one more over here.
Let's say I go ahead and write print RR and I'll say one colon.
Okay.
Now when I say one colon over here, let's say that I want to pick up three, four.
Seven eight.
Okay.
Now.
Three.
Four.
Seven.
Eight.
Uh, let's say if I want to pick up three, four seven, eight, I, you know that I have to focus on
which all columns.
I have to focus on zero and first column.
So zero and first column, how do I write it down.
So I will go ahead and write zero colon two.
That basically means it indicates I have to read from zero.
And before the second column that is first column I have to read all the elements over there.
So if I go ahead and execute it here, you'll be able to see 12345678I got it.
Now I want 3478.
So I have to focus again on the last two columns.
So now my next uh specific value will be or I can also write like this in a separate brackets.
Or I can also go give a comma and just go ahead and write one colon.
Right.
So once I do that so I'm able to get 2341 colon I have written zero one.
So it should be two colon okay.
So two colon.
You'll be able to see that I will be having this uh 3478 so you can pick up.
Go ahead and pick up any kind of elements based on this index mechanism itself.
Right.
And this is very much simple and this is very much clear, I hope.
And you can still practice more ways or like if I want to go ahead and pick up six, seven, ten, 11,
let's do this.
Okay.
Let's do this.
If I want to go ahead and pick up six, seven, ten, 11.
So here I'm going to write array of six 710, 11 is present in which rows.
First of all I have to focus on the rows.
It is on the first and the second row.
So I will just go ahead and write one colon.
So here I'm actually going to get 56789 ten 1112.
Now my column needs to focus on this first index and second index.
So what I can actually do I will go ahead and write one colon three.
Right.
So till the third index before the third index I will be able to get the element.
So here is my six seven 1011 right.
So here you can actually see that how easily and how beautifully we are able to probably do the slicing
operation and get all the values.
Okay.
Now if I want to also go ahead and modify the elements modify array elements.
Right.
Array elements I can also do that.
So for that I'll just go ahead and say let's say I want to go ahead and take this array and replace
the zeroth comma zeroth element with 100.
Right.
Now it is one.
If I go ahead and execute it now right.
If we go ahead and print the array, you will be able to see that I'm able to get this particular element
over here.
Right.
So these are some of the operations.
You can again replace multiple things.
You can replace multiple segments of numbers.
Also let's say if I go ahead and write print colon.
And here I'll go ahead and write.
Or let me just do one thing array.
And let's say that I want to go ahead and replace one colon, all the elements from first row till the
last, uh, and uh, all these elements with 100.
Now if I go ahead and print the array, I will also able to do this operation.
And here you can see all the elements after the first uh, row is basically having 100.
Right before that I have basically changed this particular element.
Perfect.
So I hope you are able to understand this.
Now, uh, let's do some of the practical applications with respect to array.
Again, first one is a statistical concept okay.
So I'll focus on statistical concept which is basically called as normalization okay.
So we will be learning about normalization as we go ahead with machine learning.
So normalization um you know what exactly it is I need to change my data to have a mean of zero and
a standard deviation of one okay.
So we usually we do this.
And for this uh, there's a formula.
Uh right now no need to focus more on that because anyhow I'll be discussing about that.
So calculate the mean and standard deviation.
Let's say this is my array.
We will go ahead and calculate the mean and standard deviation.
For that we can use NP dot mean of data NP dot standard deviation of data.
Then we have normalized data.
It is nothing but data minus mu divided by standard deviation.
This is also called as standard normal distribution.
Um, and here we can probably go ahead and display our output.
So this is how my output looks like after initially my this was my data after normalization.
This is what my data is.
Uh, normalization is nothing but converting this data in such a way that your mean is zero and your
standard deviation is one.
Right?
And it is specifically used in statistical concepts.
And we do a lot of things with respect to that.
Okay.
Um, let's take, uh, one more operation with respect to, uh, statistical operation, how to calculate
mean median mode.
So here I will be giving you one more example.
So here you can see NP dot array.
NP dot mean and b dot median NP dot standard deviation NP dot variance.
Right.
So these are some of the statistical concepts that we do.
If you really want to find out mean then we can go ahead and write NP dot mean median NP dot median.
Standard deviation is nothing but NP dot std and variance.
You can specifically use NP dot variance.
And these are some of the important concepts that we specifically use in, um, statistics.
Right.
So if I go ahead and execute it, you'll be able to see all these values.
Perfect.
Now let's do one thing.
Let's quickly do one more operation.
Uh, that I am really interested in this.
And whenever you work with pandas, later on, you will be using this.
That is nothing but logical operation.
Okay.
Logical operations.
Let's say this is my data.
And here I'm going to basically use NP dot array.
And let me just go ahead and create the elements like 123456789 ten.
Okay.
Now I can go ahead and apply some logical operations.
I can just say data greater than five.
Now what will happen when I write data greater than five.
It is just going to give me an array which says whichever value is greater than five, that will be
true, right?
And whichever value is less than five is basically false okay.
So it is basically applying a condition on this particular data.
Now let's say that I just want to retrieve all those elements that are greater than five.
So all I have to do, I will be using an indexing where I will say, hey, data of data greater than
five.
And here you can see that I am able to get the value that is nothing but six, seven eight, nine,
ten.
Right.
And this is super important because in pandas, when you do the data analysis because we will be having
Excel sheet will be having a data frame there.
We specifically apply this kind of logical operations within the arrays itself.
Right.
So any condition you can go ahead and write, let's say I will go ahead and write another condition
where data is less than eight.
So let's say all the elements written 5 to 8 right.
Greater than or equal to five and less than or equal to eight.
So here you can see an error is there.
Okay.
Let's do one thing.
We'll go ahead and write.
And there also it is not working.
So let me do one thing.
Let me uh do one thing as such.
I'll do a, I'll just use some brackets so that it will become simple for it.
And here I will use another bracket.
Okay.
Or data?
Data.
Okay.
Here, I'll close it.
Okay.
Now here you can see that I've used this particular brackets.
The reason was that why we are getting an error.
Because there are many things that we were probably putting as a condition.
Right.
And it got confused.
The compiler also got confused.
So this and will not work.
Instead logical and will specifically work over here.
So here you can see that I'm having the value 5678.
So multiple types of condition you can put, you can use or you can use and whatever things you specifically
want right.
Anything you can actually use it.
It is up to you.
So I hope you are able to understand this particular question, this particular topic.
Sorry.
And yes, this was it from my side.
Uh, in the upcoming session, we'll be discussing about our next library, which is called as pandas.
So here in short, we have discussed about numpy.
And as we go ahead we'll be still discussing about numpy.
Even in machine learning, we will be seeing a lot of multiple examples with respect to numpy.
So yes, this was it.
I will see you all in the next video.
Thank you.
Take.