Hi, welcome back.
So in the last video, we looked at an introduction to the Python
for Machine Learning series where I introduced the four libraries we'll be
exploring as well as the basics of each of them.
We also went over some installation instructions.
So if you don't have numpy, pandas,
matplotlib or Seaborn installed, I highly suggest you check out my last
video and get an introduction as well as those libraries installed so that you'll
be ready to go for this video where we'll dive into numpy.
So in this video, we'll be exploring the basics of numpy
We'll be taking a look at numpy array.
Our lesson structure is outlined here.
We'll first go into an numpy arrays using python
lists and matrices and then we'll be going into numpy arrays using built in Numpy
functions, Numpy array methods and attributes and finally
will be looking at how to create random numpy arrays.
And with that, let's jump right in.
OK, so the first thing that we have to do is import numpy
and we're going to import it as a variable and we're going to call it NP.
OK, so the next thing we're going to do is turn a Python list into an numpy array.
And so we're going to do this by creating a variable called ARR and writing np.array
which is basically feeding something into a numpy array.
And we're going to input a python list 10, 20, 30.
And now we can print this out and see how it looks and it looks like a list,
but the difference is this is actually a numpy array so we can perform operations
on this array that we couldn't do if it was just a python list.
And we'll explore these operations in a later video.
So the next thing we're going to do is
create a variable called MAT, which is short for Matrix.
And we're basically going to turn a list
of Python lists into a multidimensional numpy array.
So we can type in the variable name
in the cell and it outputs this nice matrix looking array.
Now, notice how we didn't say print mat.
And so that's why it has the array in front of it.
But if we say print mat,
then it'll remove the array word and it'll just output a matrix - just like that.
So now we're going to explore some basic
builtin methods that are specific to numpy.
So the first is np.Zeros and this one is pretty self-explanatory.
It outputs an array with zeros
and the number in the parameter indicates how many elements you have in your list.
The next one we're going to look at is np.zeros, but with two parameters.
And this one is basically going to output
a multidimensional array, all with zeros again.
And
it will kind of have the same layout as
our MAT variable above because they're both multidimensional arrays
and the next one is np.ones and this one is also pretty self-explanatory.
As you can see, it outputs
an array with ones and we can again do that multidimensional form of it.
So the next builtin function we're going
to look at is NP.EYE, which basically outputs an identity matrix.
That is the that has the number of rows
and number of columns as what you specify in the parameter.
And you can look up what an identity matrix is on your own.
I won't go into that here,
but basically just know that it kind of acts as like an inverse matrix.
So I'll write it right here and you can
look this up if you're not familiar with it.
So the next function that we're going to look at is np.arange,
which is a really handy function because you basically enter a number and it
creates an array of that length for you and it starts from zero and goes all
the way up to one before the number that you type in and notice how that has
10 elements, which is what you specified in the parameter.
So you can go ahead and check that out by typing in AR equals np.Arange and then
10 in the bracket or in the parentheses and seeing what outputs.
And we can also try changing this to eight and seeing what it outputs.
And this will just be an array from zero to seven which has eight elements.
So the next thing we're gonna look at is the same function,
but with two parameters, and this one is basically going to start
at the value specified and end up the value you specify.
So the first number in your parameter is going to be the initial starting number,
and the last number is going to be what you stop with in your array.
And what this is going to do is it's basically just going to create an array
of whatever length it takes to get from the starting to the end.
And it just it's integer based.
So it just goes number to number until it reaches the end.
So for the np.Arange function,
we can also supply three parameters, so one way to see
kind of how many parameters and what types we need, we can do shift tab and that'll
bring up this Docstring where we can observe what parameters it can take.
So we see start stop and step.
So we've explored start and stop and now we're going to look at what step means.
So let's start out with the same five and twenty, but then we'll add a four
at the end this time and we'll see what happens.
So we see that this array also starts with the five, so that stays the same,
but we see that it kind of increments by four every time.
So five plus four is nine, nine plus for is, 13, 13 plus four is 17.
So the four at the end kind of indicates
what it was saying before the step, which is kind of how much it adds every time.
And it only ends at 17 because once you
add four to 17, it exceeds 20, which we don't want.
OK, so now we're going to look at ar.reshape.
So this is
another built in function of the Numpy package.
So what this does is it kind of reshapes an array.
So previously we had just a
an array with one row and 10 columns, which was what R was initially.
But now that we've reshaped to have two
rows and five columns, we're still going to have all our
elements, but they're just kind of rearranged.
So one way to see if
a reshape will work is if both the values multiply to the original parameter.
So two times five is ten.
Five times two is ten.
So both of those work
and we can actually see the shape of this
and it'll just output 2, 5 because that is the shape of our reshaped array.
Two rows and five columns.
And we can also try outputting 1, 10,
which is going to output the same thing as we had before when we had np.arrange
and a 10 in there, basically 1 row and 10 columns.
And we can also try rearranging this
version and we can save 10, 1 instead, which will be 10 rows and 1 column.
So you can kind of just see how we're reshaping the same array.
Basically that has the same elements because 10 times one is 10,
one times ten is 10, two times five is ten and five times 2 is 10.
So here are just a few more functions
that are pretty handy, so this one is self-explanatory.
It finds the max in your array.
So in this case, that is nine.
And then we're also going to do the same thing
to find the index of the max
And in this case, it's also going to be
nine because our indices actually line up with exactly what numbers in
paired with each index.
But basically Ar.Max finds the index of your maximum value.
Next, we're going to look at the min function, which finds the minimum value.
And we can also do arg.min on this one as
well, which will find the index of the minimum value.
Now we're going to look at random, so np.Random is what we're going to start
with for creating random arrays from now on.
So we're going to start with np.Random and then we're going to say randint()
And what this does is it basically finds
a random integer between the two values that you specify in your parameter.
So in this case, it's 19.
So we can also supply three parameters.
So I basically copied and pasted
the previous line and just added a 3 to the end, which basically outputs an array
with three random integers from five to 20.
Now we're going to look at just Rand.
And what this does is it outputs an array
with length, whatever you specify in your parameter, of values between zero and one.
And so they're all going to be decimals between zero and one.
Similar to this, we have np.random.rand
with 2 parameters, which is the same concept, but a multidimensional array
So now we have the same thing.
But we're going to add in a little letter here.
It's randn.
So randend
it goes from -1 to 1 instead of 0 to 1, and it's still outputs
an array with length, whatever you specify in the parameter.
And we can also make this into a multidimensional rate.
So they're all going to be values between
-1 and 1 and they're all going to be decimals.
Well, that's it for now.
If you've enjoyed the content in this video, make sure to give it a like
and comment down below any questions you may have.
I've also included a little activity
in the description box that relates to the skills we learned in this video.
So I welcome you to try that out as well. If you're new here,
then make sure to subscribe and turn on notifications so you don't miss any
videos that will help you on your journey towards mastering artificial intelligence.
Thanks for watching and I'll see you in the next one.