Hello and welcome to this art tutorial.
So we'll quickly set our folder as working directory.
Part three classification decision tree classification.
And here is the folder.
Make sure that you have the social network as CSV file.
Then you click on this more button here to set the folder as working directory.
Then let's quickly take our template.
Select everything from here to the bottom.
Copy and
paste it here.
All right.
And now let's change a few things.
So just not to forget this change the titles and the plot.
So we will replace classifier by decision tree.
And here as well.
Okay.
And now let's create our classifier.
So in order to create a decision tree classifier we will use again
the most popular library for that which is the R part library.
So now just check to see if you have the support library in your packages.
So for example mine is right here.
It might not be the case for you if you're starting R for the first time.
So I'll just write this line of code for those of you who need to install it.
And so as usual it's install
packages.
And then in quotes in the parenthesis you input the name of the package,
which is then our part.
All right.
And then to install the package you need to select this line and execute.
I won't do it right now because my package is already installed.
So I'll just put that as command.
And however we are going to include this line of code here library.
And then parenthesis are part to automatically select this library.
Because once this is executed this will be selected.
As you can see right now it's not selected but it will be once this is executed
okay.
And now we are ready to create our classifier.
So let's do this classifier as usual.
And then we are going to use actually a function
which is the same as a library or part.
So our part.
And then this function we will input the right parameters.
So well as you can see right now we can see what those parameters are.
But if you want more info we can click here and press F1.
And here we just need to click here to get some info about our part.
Okay.
So as we can see the first argument is formula.
And as usual we're going to write the formula
equals dependent variable tilde dot.
So that's the same as usual.
And then we have the data argument here
which is of course the data on which you want to train your classifier.
So this data will be the training set.
All right.
So let's input the arguments.
So remember the first argument was formula
equals
purchased.
That's the dependent variable tilde.
I just press alt n and then a dot
to include all the independent variables.
Then comma.
And then we put the second argument which remembered was data.
And we pick our training set.
Perfect.
And now let's execute the whole code.
So first we execute this pre-processing part here as usual.
Done. Perfect.
So we can have a look at the data set.
Data set I'll fine with our two independent
variables age and estimated salary.
And our dependent variable purchased training set.
All good and test set all good.
Okay.
So the training set and the test set are scaled
because we will plot the prediction regions with a high resolution.
So we need to scale.
Actually you can try to not scale the independent variables here.
You know because for decision tree you don't need to scale your independent
variables because the decision tree model is not based on Euclidean distance.
But since we want to plot the prediction regions with a high resolution,
you will see that your code will execute a huge time faster
than if you don't scale it.
Actually, I think that if you don't scale it, your code might break.
You can try that, but, Be careful.
So we will do it.
But then we will execute the code again without the scaling to plot the tree.
So we will clear everything.
And then the preprocessing part select everything except the feature scaling.
And then we will plot our tree in a very simple way.
But right now we want to plot the prediction regions.
So we scale are independent variables. Okay.
So perfect.
Now the classifier is ready.
So let's execute it.
All right.
All good.
Now we can execute this line to predict the test set results.
And actually what's funny is that y pred is not the same as what we were used to.
First, for example, we can see. Wipe it here.
Remember before wipe.
It wasn't in the data here.
We had to type it in the console to have a look at it.
And here it's here.
So let's click on it to find out what it is.
All right.
This is why pred and this is actually a matrix of two columns and 100 lines.
So what is this new y print?
What is it exactly?
Well, as you can see, the sum of the two cells
here in each line is equal to one.
So can you guess what it is?
Well these are probabilities.
The first column gives the probability
that the observation the user belongs to class zero.
That is done by the SVM.
And this probability in the second column
is the probability that the user buys the SUV.
So here if we look at the first observation
we can see that there is a very high probability that the user buys the SUV.
And so that means here
that the prediction here is that the user doesn't buy the SUV.
And if we look at the test here
and look at the index zero, we can see that indeed in reality
the user didn't buy the SUV and therefore
the prediction is correct.