>> So in the last model when we're doing calculations,
we explored very briefly a library called NumPy.
Now NumPy, if you've actually started
exploring any data science courses already,
you may have actually already encountered NumPy.
You'll see a lot of modules
talking about using the NumPy objects and things like that,
because NumPy is actually a library
that's used not just for calculations in general,
but it's also designed for doing matrix calculations.
So because it can do matrix calculations,
it needs a way of storing matrices,
so it has an ability to store things like arrays and such as well.
Now, why am I saying all this?
Well, because it turns out if we take a closer look at
the values that we predicted when we
trained or when we tested specifically our model,
you might notice something interesting.
So when we called predict for our model and
passed in the test aid and asked it
to predicate some predicted values.
If you actually look at the type of y prediction,
you might have noticed something,
which is that y prediction,
if you look at the datatype,
is actually a NumPy array.
So it's not actually a DataFrame,
whereas y_test is a DataFrame.
Now this is significant because
so far we've been working with Pandas DataFrames.
So when we were doing queries,
we learned how to query a Pandas DataFrame.
When we were looking at information,
like how do I display the first few rows
or the last 10 rows and so on,
we were doing all these with methods on Pandas DataFrames.
So it's even possible maybe when
you are trying some of this out yourself,
that you did something like, "Hey,
let's do a y_pred duck tail to look at the last five rows," and it
failed because that method doesn't exist on NumPy arrays.
That's a method that only exist on a DataFrame.
So the other thing that might
happen is you might be doing things like trying
to merge two DataFrames together or split two DataFrames apart,
and you try and do again those actions on this
y_pred and it'll all fail because again,
it's not a DataFrame,
it's a NumPy array.
So let's just talk a little bit
more about NumPy and explore that some more.
So it's a Python package for
scientific computing used for a lot of matrix calculations.
It has an assortment of classes
and methods that will help you out.
When you're doing Python for data science,
you'll end up working with a mix of pandas objects and
methods as well as NumPy objects and methods.
In NumPy, you have an array.
So in pandas, we had a series that's
very similar to a one-dimensional NumPy array.
In fact, if I wanted to clear it,
if you would compare the code for a numpy array on
the left and a panda series on the other side,
you'll see the code is almost identical.
Here, I can tell it's an array
because I declared it as a NumPy array,
here I declared as a panda series,
but the syntax is the same.
If I want to print all the values in
the series or a specific value in the series,
the syntax is again still the same.
The big difference is you have an implicit index with
NumPy arrays and you have an explicit index if it's displayed 0,
1, 2 when you have a pandas series,
and they have different functionality.
A two-dimensional NumPy array is very
similar to a Pandas DataFrame in terms its structure.
When you're writing the code to access it,
and to create it, again it's going to look very similar.
I declare a NumPy array and I
specify the values for each row and each column.
If I want to print all the values,
I just pass in the array name,
if I want a particular row or a particular value,
I specify the index column position and the index row position;
row then column in that order.
Then if I've used the DataFrame again,
pass in the values for
the different columns and the different rows,
print the entire DataFrame,
or you can use that iloc index location to
specify a row and column position to request a specific value.
So again, at first glance,
they seem to be almost identical,
but they are different classes
and they have different capabilities.
So one of the things you're going to run
into is there's going to be
times where you may want the functionality of the DataFrame,
but it's in a NumPy array,
or maybe you want the functionality of
a NumPy array and it's in a DataFrame,
or you're trying to merge data from two objects together,
one is a NumPy array,
one's a dataframe, and when you try to merge them,
you'll get errors saying they're different data types.
So you might need to move data between a numpy and pandas object.
I'll give you one example here.
In this case, if I wanted my predicted values
to be in a Pandas DataFrame instead of a NumPy array,
all I have to do is call the DataFrame constructor,
pass in the NumPy array,
that's an object it will accept,
and it will return to me a dataframe containing the same values.
Now you can see that y_pred is still a NumPy array,
but my y_pred DataFrame is actually a DataFrame.
So as you explore that world data science,
be prepared, this is going to happen to you.
You will have times when something's in
a NumPy object and you need it
in a Pandas object or something's in
a Pandas object and you needed a NumPy object.
I'm not going to cover every possible conversion that might exist,
but be prepared for it and there's lots of
great documentation out to help
you when you're looking at a specific scenario.
Now let's take a look at this in the actual code.