All right, my friends.
Are you ready for the final steps of this implementation?
First, we're going to visualize the training set results.
And second, we will visualize the test set results.
So let's do this.
Let's start by creating a new code cell.
And there we go.
Now we just want to plot on a nice graphic the real salaries
compared to the predicted salaries.
So basically we're going to have a 2D plot
with the x axis being the numbers of years of experience,
you know, from 1 to 10 and the y axis being the salaries,
you know, in the range of salaries given in this data set.
And so we will plot in red points the real salaries
and in the blue straight line, the predicted salaries.
Okay.
And we will do that both
for the predictions in the training set and the predictions in a test set.
Are you ready?
Let's do this.
We're going to first call matplotlib as a reminder.
It has the shortcut plt.
Actually what we're going to call
is exactly the pie plot module from the matplotlib library.
And this is what is having the PLT shortcut.
So there we go.
Let's start by calling Pyplot plt.
And then we're going to call
a specific function from this module which is called scatter.
And scatter will allow us to put the red points
in a corresponding to the real salaries in the 2D plot.
Okay, so naturally what we have to input here are the coordinates
of these red points representing the real salaries
and these coordinates are nothing else than X train and Y train.
Right.
Because remember the x axis will be the numbers of years of experience.
That's what is contained in x train.
And the y axis will be the salaries.
And that is what is contained in y train.
So here and scatter here.
We just have to input the coordinates
first X train for the numbers of years of experience.
There you go.
And then Y train for the salaries okay.
And then we
can add another argument which is color
and which will allow us to choose color of these points.
And as we said, we're going to choose red.
Perfect.
Then next step is to plot the regression line.
So remember from the intuition lectures, the regression line is the line
of the predictions coming as close as possible to the real results,
you know, to the real salaries and therefore the predictions.
You know, the points corresponding to the predicted salaries will follow
a straight line, right, as in a linear function.
And therefore here we're not going to use the scatter method.
We're going to use the plot method
because that's what we use to plot the curve of a function.
And since our function is linear, it will actually be a straight line.
All right.
So there we go.
Let's call first the PLT module pyplot.
Then from this module we're going to call the plot function.
There you go.
And in this function will same as in the scatter function
we have to input the coordinates of the points or line nodes
corresponding to the predictions. So let's do this according to you.
What is the first coordinate of these predicted salaries.
Well that's of course X train Y is.
That is because right now we're visualizing the result of the training set
okay.
So these corresponds
to the numbers of years of experience of the employees in the training set.
That's the X coordinates okay.
Now we're going to input the y coordinates.
And according to you which ones are these.
Well it's something we haven't created yet.
You know we created actually the y print
variable containing the predicted salaries of the test set.
But we haven't created a vector containing the predicted salaries
of the training set. And that's totally fine.
What we'll actually enter here as the y coordinate of this plot
we're about to make,
are going to be exactly the predicted salaries of the training set.
And to get them I'm going to copy this and paste it right here.
But instead of inputting here X test
I'm going to input actually X train because calling the predict method
on X train, meaning on the numbers of years
of experience of the employees in the training set will get me
exactly the predicted salaries of the training set.
All right, just as before with predict X test.
So xtrain. Yes.
And then just as before, we're going to choose a color
which this time will be blue, right.
So that we can clearly see the difference
between the real salaries and the predicted salaries.
Okay. So this will plot the regression line.
Now next step what can we do.
So here we're going to add a title.
And to do this we have to call the title function.
And inside in the parentheses we just have to input in quotes.
Well the title we want for our graph.
And so let's say salary
versus experience okay.
You can choose any other title if you prefer another one.
So salary versus experience.
And then we're going to specify
the training set because indeed then we'll do the same for the test set.
Now we're just going to add a label to the x axis and to the y axis.
And for this we have to use the x label function
into which we have to input well the label we want to display on the x axis.
And we're going to choose and quote of course years of experience.
All right.
Good.
Now we're going to copy this because we're going to do the same for the y axis.
And the name of the function for this is y label.
And for the y label.
Well we will choose to display salary okay.
And finally in order to show the graphic well we just had to finish here
with PLT dot show, you know the show function.
And this will display the graphic in the output.