All right.
Okay. So now I'm going to tell you the solution.
Well, first of course we have to change our regressor here
because we want to use our polynomial regression model
which is based on Lin rank two.
So that's the first change.
Lin Rick two. Yes.
And now in the predict method,
do you think we need to keep x or change that by something else?
Well of course we can't keep x because remember x is
only that matrix of single feature containing the position levels.
Remember that when we use the Lin rank two regressor which is a linear regressor,
it has to be applied to the transformed matrix
of features X into this matrix of features at the different powers,
you know, position level, then squared position level, and then the other powers.
Here we only chose n equals two so far.
So we only have the squared position levels.
But that's exactly what you need to change here.
You can't skip X because it is the single feature.
So you need to input here that transformed matrix of features
X containing the different powers of that single feature position level.
And that's of course exactly this poly rig
fit transform method apply it to this matrix of single feature x.
So I'm copying this.
And that's exactly what we have to input inside this predict method.
You understand.
So that was the little thing not to forget.
And now you're all good.
You're ready
to have a beautiful visualization of the polynomial regression results.
Let's just replace this linear here by Bo
Li normal and now 100% ready.
Let's visualize the polynomial regression results.
And there you go.
Now we have indeed a way more adaptive regression
curve coming indeed much closer to the real results.
You know, the real salaries.
Indeed.
If we compare two, I'm
going to zoom out a bit so that we can see both of them at the same time.
There we go.
So if we compare these points where we had issues previously with the linear
regression model, well, we can clearly see now that the issue is resolved
because indeed the predictions on this blue
curve come way closer to the real salaries.
And this is only with n equals two.
I'm going to show you then that with higher powers you know n equals 3 or 4.
Well we will get even better results.
And I'm going to actually show this to you right away.
So now what we're going to do is, well, we're going to keep this,
but we're going to remove this because we're going to retrain
the polynomial regression model with a higher degree.
Let's take for example,
you can try with three but we're going to directly try with four.
So there you go I'm going to remove the output.
Retrain the polynomial regression model on the whole data set with.
Therefore this time a degree of four, which means that the polynomial regression
equation will be salary equals b0 plus b1 times position level plus
b2 times position level square plus B3 times position
level of the power of three plus B4 times position level the power of four.
So that will be the new polynomial regression equation.
And so therefore let's retrain it.
Let's build this new polynomial regression model by just running this cell again.
All right. So now we have it.
And now very simply we're going to visualize the new
polynomial regression results by clicking this cell here.
And now as you can see now the polynomial regression
model is perfectly fitting this data set.
So here we clearly have overfitting.
But that's okay only in this situation because we want to have
a perfect prediction of the salary between position level six and seven okay.
And now one final thing because I really want you
to have the best results and best visualizations.
Indeed.
As you can see here, what happened is that only some straight lines
where plotted between each consecutive points of the data set, right?
And therefore that makes this curve not as smooth as we would hope for.
So I actually prepared another code, but which we won't code together,
because this is just for the sake of having a more beautiful curve.
So we're going to get it from the original implementation.
It is actually right here.
You see visualizing the polynomial
regression results for higher resolution and smoother curve.
So I'm going to take all this code and then paste it
in our copy of the implementation right here.
And your code cell.
And you will see that
we will get indeed a much smoother and more beautiful curve as you can see.
Right.
And the trick to plot this curve, I'm going to explain this quickly
is just to, instead of taking you know, the integers zero, one,
two, three, four, five, six, seven, eight, nine, ten.
Well, we increase the density of these points by taking
not only these integers, but, you know, 11.1, 1.2, 1.3, and 1.4
up to, you know, 9.1, 9.2, 9.8, 9.9, ten.
That's what this 0.1 means.
That's what we call this tip.
All right. So you don't have to understand this.
You can if you want.
But this is something you will probably do only once in your life, because I remind
that usually your data sets will have many features.
And here I only took one feature in order to show you the results on a graph.
Because, you know, if we had many features, I couldn't
show this to you because we would have way too many dimensions.
So don't worry too much about this, okay?
But just appreciate the result, right?
We have a very well trained
and therefore very well fitted but overfitted model for this data set.
But that's fine because then indeed we will be able to get an amazing
and accurate prediction to figure out if there is truth or.