WEBVTT - This file was automatically generated by VIMEO

0
00:00:07.700 --> 00:00:12.120
Hi. In this video we will get the basics of simple linear regression.

1
00:00:13.940 --> 00:00:17.760
Linear regression is the simplest model between two variables.

2
00:00:17.940 --> 00:00:22.880
It can be called as a uni variate, linear regression as well. Uh,

3
00:00:23.200 --> 00:00:27.240
multiple linear regression or the multivariate regression uses multiple

4
00:00:27.240 --> 00:00:31.190
explanatory variables. Now back to simple linear regression.

5
00:00:32.100 --> 00:00:36.990
Here, the variable which we would like to create a model for is the dependent

6
00:00:37.310 --> 00:00:38.143
variable.

7
00:00:38.700 --> 00:00:42.600
The input variable for this model is the explanatory variable.

8
00:00:43.270 --> 00:00:45.480
That means that with a simple equation,

9
00:00:45.780 --> 00:00:50.640
we can predict a value of the dependent variable from values of the explanatory

10
00:00:50.960 --> 00:00:54.090
variable. And as the name suggests, it is linear.

11
00:00:54.520 --> 00:00:59.010
That means the model is a straight line with some slope and an

12
00:00:59.210 --> 00:01:03.880
intercept. The intercept is the value of the dependent variable.

13
00:01:04.150 --> 00:01:06.200
When the explanatory variable is zero

14
00:01:07.930 --> 00:01:12.620
best is to show you on an example on this chart here

15
00:01:12.840 --> 00:01:16.800
on the X axis, uh, we have the explanatory variable,

16
00:01:17.750 --> 00:01:19.050
uh, from our dataset,

17
00:01:19.140 --> 00:01:24.090
which is the average length of uninterrupted sequences of capital letters.

18
00:01:25.550 --> 00:01:29.900
On the axxis, we have the dependent variable, uh,

19
00:01:29.900 --> 00:01:34.660
which is the length of the longest uninterrupted sequence of capital letters.

20
00:01:36.200 --> 00:01:39.860
And, uh, for this one, we would like to create the regression model.

21
00:01:41.040 --> 00:01:41.270
Uh,

22
00:01:41.270 --> 00:01:46.100
these variables are in a clear and strong linear relationship with

23
00:01:46.180 --> 00:01:47.300
a very strong correlation.

24
00:01:48.530 --> 00:01:53.080
The line is our regression model of the variable. Uh,

25
00:01:53.110 --> 00:01:54.520
that means, for example,

26
00:01:55.380 --> 00:01:59.000
if the X variable had value of 40,

27
00:02:00.300 --> 00:02:04.880
our model would predict the value of dependent variable around 400.

28
00:02:07.790 --> 00:02:11.770
And uh, basically the red line is, uh, positioned that way.

29
00:02:11.950 --> 00:02:16.330
So it minimizes the differences between the predictions and the real observed

30
00:02:16.330 --> 00:02:17.163
values.

31
00:02:17.270 --> 00:02:21.990
The definition of what exactly should be minimized is the loss

32
00:02:22.270 --> 00:02:26.270
function. The light red area,

33
00:02:26.960 --> 00:02:31.420
which you see around the model is the confidence interval.

34
00:02:32.180 --> 00:02:36.740
I will not go into detail because there is much more to linear regression,

35
00:02:37.160 --> 00:02:41.500
but in simple terms, the more wider the confidence interval is,

36
00:02:41.760 --> 00:02:45.180
the more uncertain the model is about its predictions.

37
00:02:46.730 --> 00:02:50.110
Now let's create our regression model in the notebook.

38
00:02:53.670 --> 00:02:57.820
First we can make the same chart, which I showed you in the presentation.

39
00:02:58.360 --> 00:03:02.720
It was done using seaborn L plot method. In this chart,

40
00:03:02.980 --> 00:03:07.840
we can plot the scatter plot of two variables and a regression line on

41
00:03:07.840 --> 00:03:08.490
top of it,

42
00:03:08.490 --> 00:03:13.160
which provides us a great visual clue about the relationship of the

43
00:03:13.360 --> 00:03:17.080
variables. We have to import the seaborne package,

44
00:03:37.400 --> 00:03:40.100
and this is the code to explain the parameters,

45
00:03:41.210 --> 00:03:46.150
we have to input the name of our X variable, the name of the Y variable,

46
00:03:47.620 --> 00:03:48.880
our data frame object.

47
00:03:50.300 --> 00:03:52.680
We want our regulation model to be fitted.

48
00:03:54.650 --> 00:03:59.520
Alpha parameters stands for the transparency of each point in the scatter

49
00:03:59.590 --> 00:04:01.720
plot. And this, uh,

50
00:04:02.250 --> 00:04:04.840
helps us to identify more dense areas.

51
00:04:06.570 --> 00:04:09.470
And the ones that are regression line to be red,

52
00:04:11.400 --> 00:04:14.300
we can set the alpha parameter to 0.1.

53
00:04:20.000 --> 00:04:21.730
Okay, this looks good. Uh,

54
00:04:21.750 --> 00:04:26.370
but it'll be nicer if we filter the X variable up to

55
00:04:26.430 --> 00:04:30.530
100 and the y variable up to 1000. For example,

56
00:04:41.870 --> 00:04:46.400
we can create this condition and save it to our variable and

57
00:04:46.610 --> 00:04:48.840
apply this condition to the data frame.

58
00:04:56.510 --> 00:04:58.330
And this looks much better now.

59
00:05:00.430 --> 00:05:03.450
Now let's show the correlation between these two variables.

60
00:05:10.100 --> 00:05:14.520
We can see that their correlation is quite high, almost 0.5.

61
00:05:17.580 --> 00:05:22.320
If you are new to pandas, do not worry about how we filter and so on. For now,

62
00:05:22.620 --> 00:05:26.920
uh, we will go into pandas much deeper in a separate module.

63
00:05:30.780 --> 00:05:35.400
Now let's create our regression model. Using security, learn

64
00:05:39.620 --> 00:05:42.640
to create a model with LEA is quite easy.

65
00:05:42.980 --> 00:05:45.800
We have to import our class what we need first.

66
00:06:14.410 --> 00:06:16.740
This is the code. Uh, let's explain it

67
00:06:19.860 --> 00:06:21.230
here in the first line,

68
00:06:21.570 --> 00:06:26.470
we get our explanatory variable from the data frame and assign it to

69
00:06:26.670 --> 00:06:29.630
a new variable X. Uh,

70
00:06:29.630 --> 00:06:32.390
this will get us a Panda series object, uh,

71
00:06:32.390 --> 00:06:37.390
but stick learn fit method to requires a data frame. And uh,

72
00:06:37.570 --> 00:06:41.990
we changed the panda series to data frame with two frame method.

73
00:06:46.150 --> 00:06:50.760
Here in the second line, we get our dependent variable from the data frame.

74
00:06:51.880 --> 00:06:54.260
The return object will be panda series.

75
00:06:54.520 --> 00:06:56.820
And this is okay for the second learn method.

76
00:07:02.270 --> 00:07:05.920
Here we instantiate our model. Uh,

77
00:07:05.990 --> 00:07:08.000
here are no calculations done for now.

78
00:07:11.630 --> 00:07:15.330
And we set the fit intercept parameter to true

79
00:07:24.700 --> 00:07:29.300
model fit. Here we fit the model.

80
00:07:29.850 --> 00:07:33.660
Here are all the calculations done, like the optimization of the loss function.

81
00:07:36.720 --> 00:07:40.100
And with predict method, we get the predictions.

82
00:07:46.810 --> 00:07:51.580
Okay, now our model is fitted. We have our predictions.

83
00:07:52.040 --> 00:07:55.820
Now let's measure how well this model is performing.

84
00:07:59.800 --> 00:08:03.300
We can use for this, the multiple evaluation metrics. When

85
00:08:07.200 --> 00:08:11.010
we do model dot score method, uh,

86
00:08:11.150 --> 00:08:16.120
we can calculate the coefficient of determination. Uh,

87
00:08:16.190 --> 00:08:19.400
this metric is used for evaluating regression models.

88
00:08:20.900 --> 00:08:25.680
Its value can be between zero and one zero indicating a totally

89
00:08:25.780 --> 00:08:29.160
bad model and one indicating a perfect model.

90
00:08:41.920 --> 00:08:46.700
And our coefficient or determination is 0.24.

91
00:08:51.170 --> 00:08:53.510
We can measure now the mean square error.

92
00:08:55.480 --> 00:08:56.980
We have to import it first.

93
00:09:20.600 --> 00:09:24.540
And this is the value for means square error. And uh, as I remember,

94
00:09:24.600 --> 00:09:28.460
the means square error is also our loss function,

95
00:09:28.710 --> 00:09:31.420
which the model tried to minimize, uh,

96
00:09:31.420 --> 00:09:34.660
but it can be used also as a evaluation metric. And

97
00:09:38.730 --> 00:09:39.563
a last thing,

98
00:09:39.920 --> 00:09:44.730
what we will do is to extract our models intercept and the

99
00:09:44.730 --> 00:09:45.563
coefficient.

100
00:09:58.170 --> 00:10:01.340
This is the coefficient and i the intercept.

101
00:10:13.990 --> 00:10:17.240
When we go back to our regression equation,

102
00:10:22.370 --> 00:10:24.550
the beta one parameter is the cot,

103
00:10:24.930 --> 00:10:27.790
and beta zero is our intercept.

104
00:10:35.510 --> 00:10:39.010
In our next video, we will learn how to work with kernels.
