Hi. In this video we will get the basics of simple linear regression.
Linear regression is the simplest model between two variables.
It can be called as a uni variate, linear regression as well. Uh,
multiple linear regression or the multivariate regression uses multiple
explanatory variables. Now back to simple linear regression.
Here, the variable which we would like to create a model for is the dependent
variable.
The input variable for this model is the explanatory variable.
That means that with a simple equation,
we can predict a value of the dependent variable from values of the explanatory
variable. And as the name suggests, it is linear.
That means the model is a straight line with some slope and an
intercept. The intercept is the value of the dependent variable.
When the explanatory variable is zero
best is to show you on an example on this chart here
on the X axis, uh, we have the explanatory variable,
uh, from our dataset,
which is the average length of uninterrupted sequences of capital letters.
On the axxis, we have the dependent variable, uh,
which is the length of the longest uninterrupted sequence of capital letters.
And, uh, for this one, we would like to create the regression model.
Uh,
these variables are in a clear and strong linear relationship with
a very strong correlation.
The line is our regression model of the variable. Uh,
that means, for example,
if the X variable had value of 40,
our model would predict the value of dependent variable around 400.
And uh, basically the red line is, uh, positioned that way.
So it minimizes the differences between the predictions and the real observed
values.
The definition of what exactly should be minimized is the loss
function. The light red area,
which you see around the model is the confidence interval.
I will not go into detail because there is much more to linear regression,
but in simple terms, the more wider the confidence interval is,
the more uncertain the model is about its predictions.
Now let's create our regression model in the notebook.
First we can make the same chart, which I showed you in the presentation.
It was done using seaborn L plot method. In this chart,
we can plot the scatter plot of two variables and a regression line on
top of it,
which provides us a great visual clue about the relationship of the
variables. We have to import the seaborne package,
and this is the code to explain the parameters,
we have to input the name of our X variable, the name of the Y variable,
our data frame object.
We want our regulation model to be fitted.
Alpha parameters stands for the transparency of each point in the scatter
plot. And this, uh,
helps us to identify more dense areas.
And the ones that are regression line to be red,
we can set the alpha parameter to 0.1.
Okay, this looks good. Uh,
but it'll be nicer if we filter the X variable up to
100 and the y variable up to 1000. For example,
we can create this condition and save it to our variable and
apply this condition to the data frame.
And this looks much better now.
Now let's show the correlation between these two variables.
We can see that their correlation is quite high, almost 0.5.
If you are new to pandas, do not worry about how we filter and so on. For now,
uh, we will go into pandas much deeper in a separate module.
Now let's create our regression model. Using security, learn
to create a model with LEA is quite easy.
We have to import our class what we need first.
This is the code. Uh, let's explain it
here in the first line,
we get our explanatory variable from the data frame and assign it to
a new variable X. Uh,
this will get us a Panda series object, uh,
but stick learn fit method to requires a data frame. And uh,
we changed the panda series to data frame with two frame method.
Here in the second line, we get our dependent variable from the data frame.
The return object will be panda series.
And this is okay for the second learn method.
Here we instantiate our model. Uh,
here are no calculations done for now.
And we set the fit intercept parameter to true
model fit. Here we fit the model.
Here are all the calculations done, like the optimization of the loss function.
And with predict method, we get the predictions.
Okay, now our model is fitted. We have our predictions.
Now let's measure how well this model is performing.
We can use for this, the multiple evaluation metrics. When
we do model dot score method, uh,
we can calculate the coefficient of determination. Uh,
this metric is used for evaluating regression models.
Its value can be between zero and one zero indicating a totally
bad model and one indicating a perfect model.
And our coefficient or determination is 0.24.
We can measure now the mean square error.
We have to import it first.
And this is the value for means square error. And uh, as I remember,
the means square error is also our loss function,
which the model tried to minimize, uh,
but it can be used also as a evaluation metric. And
a last thing,
what we will do is to extract our models intercept and the
coefficient.
This is the coefficient and i the intercept.
When we go back to our regression equation,
the beta one parameter is the cot,
and beta zero is our intercept.
In our next video, we will learn how to work with kernels.