Hello my friends.
Congratulations again for having implemented
your first classification model, logistic regression.
And now, before we move on to the next model,
I would like to make a classification template that will allow us
to build much more efficiently the next classification models of this part.
And not only that, it is a template
that you will also be able to use for your personal data set.
All right.
So let's do this.
At the top of our studio we're going to click file and then click New File.
And then our script. All right.
And we're going to save this as classification template.
All right then we click save and you will see it appear in your file here.
That's the new file we just created.
All right then we're going to take our logistic regression implementation.
Again.
We're going to select everything
copy it and then paste it in our classification template.
And then let's change the title.
We're going to replace this by classification template.
And then in the data preprocessing sections here we're going
to keep everything as is except one thing.
We're going to add one line of code that will make sure the classes of
our dependent variable are indeed classes and, you know, not continuous numbers.
So for most of the models like logistic regression k
and then naive Bayes, SVM and kernel SVM, the package that we'll use to build
those models will recognize the classes of our data sets as classes.
But there will be two models that will build, which are decision tree
and random forest that will not recognize them as classes.
And therefore, to make this template applicable to all the models
we're going to build in this part three, we just need to add this little code
section here that I'm going to call, and coding the target feature as factor.
And that will make sure that the purchased
dependent variable of our data
set will be included as factors.
And to do so we use the factor function inside which we enter
first the dependent variable column which we access from our data set.
Then adding a dollar again.
And then there we go purchased.
So that's the first argument of this factor function.
And then we just need to specify the two classes here with this argument
levels that will be equal to our two classes zero and one,
which we have to enter in this C function which will take as input zero and one.
All right. So let me recap this.
This line of code is optional for the logistic regression model.
The k nearest neighbor model which will implement next.
Then the SVM model the kernel SVM model and the Naive Bayes model.
And it is compulsory for the decision tree classification model
and for the random forest classification model.
All right.
So I just want to include it here so that we can apply this template
to all the classification models of this part tree.
And for that let's make the other required changes.
So here when splitting the data
set into training set and a test set, we don't have to change anything.
Same we can keep feature scaling.
And then that's where we will change some things.
So first we will remove all this
because that's where we'll build our next classifier.
And so we can add a and saying create your classifier here okay.
Then we can replace logistic regression here by classifier.
All right.
To make it generic then here very important.
The logistic regression model is the only model of this part three
that returns first its predictions in the form of probabilities.
All the other models will return directly 0 or 1.
And therefore we're going to remove this line.
We're going to replace here prop pred by y bread.
And here in the predict function we're going to keep classifier.
Of course we're going to remove type equals response.
And we're going to keep new data equals to set minus three.
All right.
And this line of code
will be applicable to all the remaining classification models of part three.
We won't have to change anything here okay.
And then same for the confusion matrix.
This is only for the logistic regression model because it returns probabilities.
And so to adapt this to a model that returns only zero one,
well of course we need to remove larger than 0.5.
Right.
Because the widespread of our future classification models will only contain
zero one exactly as the dependent variable column of the test set.
Okay, great.
And then finally in the last two sections here visualizing the training set
and test results.
Same.
We're going to remove the probabilities here.
Then cut this way grid.
And then replace this proposed variable by y grid.
And then remove the type equals response parameter so that.
Now we don't have our predictions
returned in the form of probabilities but zeros and ones.
And then of course we're going to make it generic again by replacing logistic
regression here by classifier okay.
Excellent.
So now let's copy this because we're going to do this same down there
for the visualization of the test result.
So I'm going to replace these two lines by what we just copied.
There we go.
And then replace logistic regression again by classifier.
And now congratulations we have a generic template
that we will be able to use to build very efficiently.
Our next classification rules of board three.
So let's save this.
And now look forward to building those next models with you.
And until then enjoy machine learning.