Hello and welcome to this art tutorial.
Today we're going to implement Support Vector Machine
or more commonly called SVM.
And after having implemented it on Python we're going to do it on R.
So let's quickly set the folder as Working directory machine learning A-Z
part three classification SVM.
And here we are.
Make sure that you have the social network ads dot csv file.
And then you can click on this more button
here to set the folder as working directory.
All good.
Now we're going to go to our
classification template that we made in the logistic regression section.
So let's take everything from here to the end.
Copy and let's paste it in our SVM file.
Here we go.
And now we need to change very few things.
So first we need to create our classifier here
which is going to be the SVM classifier of course.
And then we just need to change the title here for the graph
the training set results.
So we're going to specify that it's the SVM classifier.
Then for the test set here SVM.
And now let's move back up to create our classifier okay.
So as usual we're going to import a library.
And this time the library is going to be the most popular library
that is used for SVM which is the library E10 71.
So let's do this first.
Let's have a look at the packages here.
Check out to see if you have the E10 71 library.
It is installed on my RCU because I used it a lot of times, but
if you installed R for the first time then you might not have it.
So I'm going to just
write this command here for those of you who need to install it.
So install dot packages. Here it is.
And then in parentheses you put in quotes
the name of the library which is named this way E10 71.
All right.
So if you select and execute this line it will install the package.
I won't do it because it's already installed but it will do it I promise you.
So I'm going to put that as comment there.
And now let's start creating our classifier.
Well first we need to, you know, add this line library E10 71.
And that's in case you want to make some automated script
that select automatically your library here because this won't be
selected all the time.
So you want to make sure that you select it.
And now we are going to create our SVM classifier.
So as usual we're going to call our classifier.
Classifier equals.
And then here we're going to use very simply the function SVM.
So let's do this SVM.
Then let's press F1 to look at the parameters f1.
And here are the parameters the arguments.
So the first argument is formula.
So let's add it.
So as usual it's the formula that is the dependent variable expressed
using a tilde with respect to all your independent variables
which you represent by a dot.
So here we're just going to add formula equals.
So first we take our dependent variable which is purchased.
All right.
And now we use this tilde here.
And then a dot a dot meaning that we're taking
all the independent variables of our data set okay.
Now comma to go to the next argument then the next argument is data.
So of course that's the data on which you want to train your classifier
on which you want your class for you
to learn the data to make the future classification.
And of course, this data is our training set.
Okay. Perfect
comma.
Let's go to the next argument.
The next argument is x and y.
But we don't really care about that.
And however we do care about the type here and the kernel
because the type as you notice there's two type of SVM.
There is the SVM for classification and the SVR for regression,
which is kind of the same support vector machines algorithm,
but there is one for classification and one for regression.
So here with this argument type you choose the classifier version.
And the default type for classification is C classification.
So that's the one we're going to choose for type.
So let's add here type equals
c classification.
All right.
And now the last very important argument is the kernel.
So we're starting with the most simple SVM which is the linear SVM.
So here we're going to choose the linear kernel.
And then we're going to try some more sophisticated SVM
with some Gaussian kernel.
But for now we're going to choose a linear kernel.
So here we'll input kernel equals linear.
All right.
And that's all with these four arguments our classifier is ready.
Our SVM classifier is ready.
So we're going to select the different steps or our sections of our code.
So let's first select this one to preprocess the data.
Here it is okay. So perfect.
We have our data set here our training set and our test set.
So the data set contains 400 observations which are informations about users
of a social network including the age and the estimated salary.
In this column.
Purchase here tells if yes or no.
The user bought a car when the user got the ad that
the car company put on the social network for marketing campaign purposes.
And so our class very as usual, it's going to try to classify
the users into the two categories.
Yes, they bought the car and no, they didn't buy the car.