Machine Learning Project 17 — Compare Classification Algorithms | by Omair Aasim | Towards Data Science

In the past 7 projects, we implemented the same project using different classification algorithms namely — “Logistic Regression”, “KNN”, “SVM”, “Kernel SVM”, “Naive Bayes”, “Decision Tree” and “Random Forest”.
The reason I wrote a separate article for each is to understand the intuition behind each algorithm.
#100DaysOfMLCode #100ProjectsInML
In a real scenario, when we are given a problem, we cannot predict which algorithm will perform best. Obviously from the problem, we can tell whether we need to apply Regression or Classification algorithm. But it is difficult to know which Regression or Classification algorithm to apply beforehand. It is only through trial and error and checking the performance metrics, we can narrow down and pick certain algorithms.
Today, I will show you how to compare different classification algorithms and pick the best ones. Rather than implementing the entire project using an algorithm and then finding out that the performance is not good, we will first check the performance of a bunch of algorithms and then decide which one to use to implement the project.
Let’s get started.
We are going to use the same dataset we used in Project 10. Our objective is to evaluate several classification algorithms and pick the best ones based on accuracy.
The sample rows are shown below. The full dataset can be accessed here.
We are going to assign the independent variables “Gender”, “Salary” and “Age” to X. The dependent variable “Purchased iphone” captures whether the user has purchased the phone or not. We will assign this to y.
We have a categorical variable “Gender” that we have to convert to number. We will use the class LabelEncoder to convert Gender to number.
Apart from the Decision Tree and Random Forest classifiers, the other classifiers require that we scale the data. So let’s do that now.
This is where all the fun stuff happens :)
I am going to compare 6 classification algorithms — the ones I have covered in previous projects. Feel free to add and test others as well.
Logistic Regression
KNN
Kernel SVM
Naive Bayes
Decision Tree
Random Forest
We will use 10 fold cross validation to evaluate each algorithm and we will find the mean accuracy and the standard deviation accuracy.
First, we will create a list and add objects of the different classifiers we want to evaluate. Then we loop through the list and use the cross_val_score method to get the accuracies.
Here is the output:Logistic Regression: Mean Accuracy = 82.75% — SD Accuracy = 11.37%K Nearest Neighbor: Mean Accuracy = 90.50% — SD Accuracy = 7.73%Kernel SVM: Mean Accuracy = 90.75% — SD Accuracy = 9.15%Naive Bayes: Mean Accuracy = 85.25% — SD Accuracy = 10.34%Decision Tree: Mean Accuracy = 84.50% — SD Accuracy = 8.50%Random Forest: Mean Accuracy = 88.75% — SD Accuracy = 8.46%
From the results, we can see that for this particular dataset, KNN and Kernel SVM have performed better than the rest. So we can shortlist these 2 to work on this project. This is exactly the same conclusion we arrived at by implementing each of those algorithms individually.
Hope you had fun. You can find the entire source code here.