So polynomial features.
And then of course, well you know anytime we import a class
the next step is to of course create an object of this class.
And this object will be exactly the tool that will allow us to create this matrix
of the features x1, x1 squared x1.
At the power of n, I will precise what n we choose.
Then we will choose several of them.
But there you go.
That's what we build with this polynomial features class.
And so well we're going to call this object of this class poly
underscore rig as polynomial regressor.
But it's not exactly the regressor itself because you know,
the final polynomial regressor will be the combination
of this matrix of powered features and the linear regressor.
And that's why we will call it actually Lin rank two.
You will see.
So poly rag will be created as an instance or
an object of this polynomial features class.
So we're going to paste that here adding some parenthesis.
And that is where we're going to choose the n.
You know that.
And here is exactly chosen in this new class.
So first we're going to start with two okay.
We're going to build a polynomial regression model
resulting from the equation y equals b0 plus b1 x1
plus b2 x1 squared where x1 is of course the position levels and y is the salaries.
All right.
So let's create this.
Let's precise degree which is the name of the parameter for that n equals two.
And then we'll try it three and four okay.
Then next step.
Well the next step is now to proceed to this transformation
of this simple matrix of features containing
only x1, meaning exactly this columns so far
this is our matrix of features of only one feature.
And now we're going to transform this matrix of a single feature
into this new matrix of features containing x1
as a first feature, x1 squared as a second feature.
And then that's it, because so far we start with an equal to.
But if we chose for example n equal three, well,
the matrix of features would be x1, x1, squared and x1 and the power of three.
All right.
So that's what we're going to do.
And to do this well we're going to take of course our fully read object again
from which we're going to call the method fit transform.
Again you're starting to know this method
that's you know, the method that usually proceed to a transformation.
And here the transformation is to turn this matrix of a single feature
into this new matrix of features composed of x1 as the first feature,
and x1 squared is the second feature.
All right, fit transform, then some parenthesis.
And then according to you now what do we have to input here?
Well, that's pretty obvious.
That's exactly the matrix of features we want to transform
into this matrix of squared features let's say right.
So x x of course that's what we want to transform x
so far is only composed of this column okay.
So fit transform x.
And now we have our new matrix of features.
However this exactly returns this new matrix of features.
And now we're going to create a new variable
which will be this new matrix of features itself.
And we're going to call it x underscore.
Poly equals what is returned by this
fit transform method applied to x. Great.
So now we have exactly the matrix of features composed of the position
levels and the squares of the position levels.
Good.
So now according to you what are we going to do.
Well as I said at the beginning, it's like now we have a new matrix
of features, you know, composed of these two variables here.
And well, very simply, we just have to build a linear regression
model that will integrate these features into this equation.
Y equals b0 plus b1 x1 plus b2 x1 squared.
Right.
You see the idea that's where the linear comes from.
And therefore well you know what to do.
You know how to create such a linear regressor.
But we have to create a new one which is of course
different than this one, because this one is already trained on this matrix
of single feature X and therefore having already learned coefficient.
So now we need to create a new one which we're going to call, of course Lin rag.
And I'm adding underscore two, which therefore will be
a new object of this linear regression class.
Adding some parenthesis.
And well, this object will be now trained on this new matrix of features
composed of the position levels at different powers.
And of course then the salary, because indeed we need the dependent
variable vector to train the linear regression model and any machinery model.
All right.
So the next step and final step is to of course call this new linear
regressor object that we've just created from which
we're going to call the fit method which will take as input.
Of course, this new matrix of features containing the position labels
at different powers, and of course the same dependent variable vector
which are the salaries. All right. So let's do this.
Let's input first x fully and then y.
And there you go.
You have your polynomial regression model.
Congratulations.
Now you know how to build a nonlinear regression model.
And that's what we're going to see clearly with the next steps.
When visualizing the linear regression results first
and the polynomial regression results.
Now you know how to build such a model.
And you have them in the toolkit.
So are you getting more power in machine learning?
More knowledge, more skills? Congratulations!
That's amazing.
And now I can't wait to move on to the next tutorial to show you indeed
the different results we get with linear regression and polynomial regression.
So I'll see you in the next tutorial.
Let's quickly run this cell in order to, you know, get the model itself.
And now, whenever you're ready, let's proceed
to the next tutorial for the visualizer sessions.
And until then, enjoy machine learning.