Hello my friends.
All right.
So let's implement quickly
and efficiently the linear regression model on the whole data set.
First a quick reminder on what is the linear regression model
that we're about to build.
We're simply about to build this one right.
Because we have only one feature which is the position levels
and the dependent variable, which is of course the salary to predict.
All right.
So that's what we'll do first.
And then we'll of course implement the polynomial regression model with
I will tell you how many powers of that same position level feature.
Okay.
So let's do this starting by creating a new code cell.
All right.
So I hope you did it yourself first to make sure you refresh your skills.
And so if that's the case of course you stole it from the scikit learn library
because that's the library that contains this linear
regression class which allows to build a linear regression model.
So scikit learn from which we're going
to get access to the linear model which contains this class.
And from this linear model we're going to import
the linear regression class as Google Collab.
Guess it's perfectly perfect. So that's the first step.
Then we're going to create an object of this class.
So I know we used to call it regressor, but this time
since we're going to have two regressors, you know we're going to have
the linear regression regressor and the polynomial regression regressor.
Therefore this time I'm going to call it Lin rake for linear regressor okay.
So Lin rake will be created as an object of this linear regression class.
So I'm copying and pasting that here adding some parenthesis.
And remember we don't have to input anything in these parentheses
because there is not much to tune in the linear regression model.
Good.
And finally now we have, you know, the linear regression model.
But it is not smart yet.
It is not trained yet on this data set.
You know, to understand and learn the correlations
between the position levels and the salaries.
So that's what we're going to do right now by using this fit method,
which is exactly a training method that will train the model on these data.
All right.
So to do this we're going to call our object first Lin
Rag from which we're going to call the fit method
which remember has to take as input the couple of matrix
of features and dependent variable vector of the training set.
Right. That's what we did before.
But remember here that we didn't split the data set into a training set.
And it just said because we want to leverage
the maximum data in order to train our model.
And therefore this time we're going to take the whole matrix of features X
and the whole dependent variable vector y'all. Right.
So here we just input x and y and there we go.
We built the linear regression model in a flashlight.
So we're going to execute this.
Here we go.
And now we have not only our model but also a trained model on this data set
okay great.
Now we're going to close this.
And we're going to focus on
the heart of the matter which is the polynomial regression model.
I'm going to teach you now how to build the polynomial regression model.
All right. So we're going to create a new code cell.
And now
we're going to go back to this slide to explain exactly what we're going to do.
All right so as you understand what we've just built so far is this model.
We only have one feature
the position levels and the dependent
variable vector to predict which are the salaries.
Now what we're going to do is we're going to create a multiple linear
regression model.
But instead of having different features you know, X1, x2 and Zn,
well these features will be x1, x1, squared and x1 are the power of n.
And we'll actually tune this parameter a bit to try several powers.
So not to confuse the polynomial.
Linear regression is not a linear model, because you will see that it can learn
some nonlinear correlations, but we call it polynomial linear regression
because indeed there is this linear combination of the squared and,
you know, powered features x1, x1, squared and x1 and the power of n.
All right.
And so the process of building this model on Python
will be first to create a matrix of the powered features,
you know a matrix of features but not containing
different features like X1, x2, an x n, but a matrix of features containing x1
as a first feature, then x1 squared as a second feature,
and then x1 to the power of and as an end feature.
So that will be our matrix of features, and we will call it x poly.
And then we will create a linear
regressor object, you know, from the linear regression class
to integrate these powered features of this matrix of features
in this new linear regressor, you see the idea.
So it's a building process in two steps.
We're going to first create the matrix of features
containing these features at different powers.
And then we'll integrate that into a linear regression model.
Because indeed this is a linear combination of these powered features.
All right perfect.
So let's do this first step.
The first step is to actually import this tool
that will allow us to create this matrix of powered features.
You know X1X1 squared and x one are the power of n.
And this class is called polynomial features.
So we're going to import it first
which we can get from once again of course the scikit learn library.
There we go.
But this time we're going to get access to the pre-processing module
which contains that polynomial features class.
This is a class because indeed we're kind of preprocessing our X1 feature
because we want to create out of x1 well x1, x1 squared and x1 add the power of n.
So we're kind of doing preprocessing of the features.
All right.
So from this preprocessing module we're going to import.
Well that class put Li no more features.
Perfect.