WEBVTT - This file was automatically generated by VIMEO

0
00:00:07.680 --> 00:00:08.080
Hi.

1
00:00:08.080 --> 00:00:12.780
In this video we will learn how to run r and Python code in the same

2
00:00:13.060 --> 00:00:15.340
notebook. As we already know,

3
00:00:15.560 --> 00:00:19.860
we can use R or Python kernel in the notebook, but, uh,

4
00:00:20.000 --> 00:00:24.400
one kernel applies for whole notebook. In our use case,

5
00:00:24.700 --> 00:00:26.840
we will have notebook with Python kernel,

6
00:00:27.140 --> 00:00:30.000
but we want to make some sort of analysis in R.

7
00:00:31.330 --> 00:00:36.300
This is a very effective combination because Python is a very strong programming

8
00:00:36.580 --> 00:00:40.820
language, but r has many excellent statistical packages.

9
00:00:42.330 --> 00:00:45.740
This functionality provides the R P Y two package,

10
00:00:46.390 --> 00:00:51.340
which provides us the interface to run our embedded in a Python

11
00:00:51.340 --> 00:00:55.640
process. We'll do the same chart, uh,

12
00:00:55.660 --> 00:00:59.640
the scatter plot of our dependent and explanatory variable,

13
00:01:00.490 --> 00:01:03.390
but in r using GG plot two library.

14
00:01:04.570 --> 00:01:08.830
And the second thing, we will do the same linear regression in R.

15
00:01:14.810 --> 00:01:18.790
Now please change the kernel to the base environment and, uh,

16
00:01:18.990 --> 00:01:19.823
rerun the notebook.

17
00:01:49.500 --> 00:01:52.390
When we want to execute our code in the cell,

18
00:01:52.570 --> 00:01:57.270
we have to load the i python's r p Y two extension First,

19
00:02:14.620 --> 00:02:19.040
the person sign means we are executing I python's magic command.

20
00:02:19.660 --> 00:02:23.800
If you are curious about ipy magic, please check the references.

21
00:02:25.160 --> 00:02:27.850
When you want to execute r code in the cell,

22
00:02:28.390 --> 00:02:31.730
you have to use the R magic command at the beginning, uh,

23
00:02:31.820 --> 00:02:35.050
which is the percent R or one line of code,

24
00:02:35.550 --> 00:02:39.530
or twice percent and r if the whole cell will be R code.

25
00:02:41.580 --> 00:02:42.640
Now let's do the chart

26
00:02:53.920 --> 00:02:57.410
next to R magic command. We can input few parameters.

27
00:02:57.640 --> 00:03:01.520
Most important parameters are inputs and outputs.

28
00:03:02.030 --> 00:03:03.520
That means our objects,

29
00:03:03.650 --> 00:03:07.840
which we would like to pass from Python to R and objects,

30
00:03:07.890 --> 00:03:12.270
which we would like to pass back from R to Python. In our case,

31
00:03:12.650 --> 00:03:14.630
we just need to specify the input,

32
00:03:14.890 --> 00:03:18.650
and this is our data frame For our chart. Uh,

33
00:03:18.650 --> 00:03:20.850
we can specify its hate and width.

34
00:03:21.820 --> 00:03:26.480
We do not need to specify any output Now because we want just to plot one chart,

35
00:03:32.700 --> 00:03:36.680
and this is the code to explain the code. First, we check, uh,

36
00:03:36.780 --> 00:03:40.520
if GG plot two package exists. If not, it'll be installed.

37
00:03:42.260 --> 00:03:45.800
And this part is the code to plot the scatter plot.

38
00:03:55.960 --> 00:03:56.793
And here it is.

39
00:04:01.870 --> 00:04:05.810
Please notice this data frame, which is used as input to the chart,

40
00:04:06.230 --> 00:04:09.570
but in reality it is already converted to R metrics.

41
00:04:14.490 --> 00:04:19.150
Now let's do the linear regression in R and compare if we will go the same

42
00:04:19.390 --> 00:04:22.110
values of intercept and the coefficient.

43
00:04:29.730 --> 00:04:33.580
Our input will be just our data frame, uh,

44
00:04:33.600 --> 00:04:38.100
but we would like to get back from R to Python, three objects. Uh,

45
00:04:38.100 --> 00:04:42.600
first will be the intercept. Second, uh, will be the coefficient,

46
00:04:43.020 --> 00:04:46.400
and the last one will be the vector of our predictions.

47
00:04:52.700 --> 00:04:56.790
This is the code. Let's explain it. In this line,

48
00:04:57.370 --> 00:04:58.950
we create the model, uh,

49
00:04:58.960 --> 00:05:03.310
where we specify our dependent variable and the explanatory variable

50
00:05:03.970 --> 00:05:08.950
and also our data frame. Here in these two lines, uh,

51
00:05:08.970 --> 00:05:11.950
we extract the intercept and the coefficient,

52
00:05:12.910 --> 00:05:16.690
and in the last line we make the predictions. Let's run it.

53
00:05:21.300 --> 00:05:24.320
We see here our information about our model.

54
00:05:28.110 --> 00:05:30.250
And now let's print the variables

55
00:05:39.300 --> 00:05:42.320
and let's also print, uh, the vector of predictions.

56
00:05:58.180 --> 00:06:01.940
Everything seems working. Uh, now, uh,

57
00:06:02.080 --> 00:06:06.660
we can see that the returned values are in a form of MPI array.

58
00:06:09.440 --> 00:06:12.620
We can compare the values of the intercept and coefficient,

59
00:06:19.920 --> 00:06:21.940
and we see that they are the same.

60
00:06:28.830 --> 00:06:33.810
In our next video, we will learn how to download and export the notebook.
