This blog will help you master the fundamentals of classification algorithms and the different types of classification algorithms with their pros and cons. You will also explore some exciting machine learning project ideas that implement different types of classification algorithms. So, without much ado, let's dive in.
Imagine that the pandemic is over and today is a weekday. All the schools, colleges, and offices are open, and you should reach your institution by 8 A.M. You set the alarm last night and managed to wake up at 6 A.M. You have taken your bath and are now nicely dressed in your clothes. You now approach the wall-hook to grab your belt, but alas! It's not there. You start panicking and are searching for it here and there. Finally, you call out for your mother, and after 10 minutes of searching, she finds it.
And, now, on the one hand, you are happy that you have found your belt, but on the other hand, you are worried about reaching your institution late. So, on your way to the destination, you start wishing for a magical wardrobe where you could throw your stuff, and it could automatically classify things and neatly place them. We all have been through this.
But, if you think that such a wardrobe can't be a reality, let me tell you that's not true. With Artificial Intelligence, it has become possible to build what we like to call 'smart wardrobes.' These will allow you to access things from your wardrobe just by a single tap on your phone. Of course, you might have to explain a few characteristics of your clothing to it initially, for example, its color, size, and type. But, as the wardrobe gradually understands your dress, you will be easily able to utilize it.
We expect the wardrobe to perform classification, grouping things having similar characteristics together. And there are quite a several machine learning classification algorithms that can make that happen. We will look through all the different types of classification algorithms in great detail but first, let us begin exploring different types of classification tasks.
In Machine Learning, most classification problems require predicting a categorical output variable called target, based on one or more input variables called features. The idea is to fit a statistical model that relates a set of features to its respective target variable to use this model to predict the output for future input observations. However, it is essential to keep in mind that predicting a single output variable will not always be the case. There are many other possible causes, and let us inspect them one by one.
There are mainly four types of classification tasks that one may come across, these are:
This type of classification involves separating the dataset into two categories. It means that the output variable can only take two values.
Binary Classification Example: The task of labeling an e-mail as "spam" or "not spam." The input variable here will be the content of the e-mail that we are trying to classify. The output variable is represented by 0 for "not spam" and 1 for "spam."
In multi-class classification, the output variable can have more than two possible values.
Multi-Class Classification Example: Identifying the flower type in the case of Iris Dataset where we have four input variables: petal length, sepal length, petal width, sepal width, and three possible labels of flowers: Iris Setosa, Iris Versicolor, and Iris Virginica.
Image Source: Wikipedia Commons
This is an extraordinary type of classification task with multiple output variables for each instance from the dataset. That is, one instance can have multiple labels.
Multi-Label Classification Example: In Image classification, a single image may contain more than one object, which can be labeled by the algorithm, like bus, car, person, etc.
Imbalanced classification refers to classification problems where the instances of the dataset have a biased or skewed distribution. In other words, one class of input variables has a higher frequency than the others.
Imbalanced Classification Example: Detecting fraudulent transactions through a credit card in a transaction dataset. Usually, such transactions are remarkably less in number, and this would thus make it difficult for the machine to learn such transactions.
To solve classification problems, we use mathematical models that are known as machine learning classification algorithms. Their task is to find how the target variables are related to input features xi to the output values, yi. In mathematical terms, estimating a function, f(xi), predicts the value of the output variable by taking the associated features as an input. We can write this as,
where y'i represents the predicted response for the output variable.
Now that we understand the task at hand, we will now move forward towards different steps that explain how classification algorithms in machine learning work.
Before we apply any statistical algorithm to our dataset, we must thoroughly understand the input variables and output variables. In classification problems, the target is always qualitative, but sometimes, even the input values can also be categorical, for example, the gender of customers in the famous Mall Customer Dataset. And as classification algorithms are mathematically derived, one must convert all their variables into numerical values. The first step in the working of a classification algorithm is to ensure that the variables, whether input or output, have been encoded correctly.
After processing the dataset, the next step is to divide the dataset into two parts: the testing dataset and the training dataset. This step allows using the training dataset to make our machine learn the pattern between input and output values. On the other hand, a testing dataset tests the model's accuracy that we will try to fit into our dataset.
Once we split the dataset into training and testing, the next task is to select the model that best fits our problem. For that, we need to be aware of the popular classification algorithms. So, let us dive into the pool of different types of classification algorithms and explore our options.
There are various classification algorithms in data mining used by Data Scientists every day to gain a deeper understanding of their dataset. In this section, we will explore the popular ones in great detail. You are likely to feel like a superhero by understanding the pros and cons of classification algorithms.
Here is a list of different types of classification algorithms that you will learn about:
Naive Bayes classifier, one of the simplest and effective classification algorithms. Its basis is Bayes' theorem which describes how the probability of an event is evaluated based on prior knowledge of conditions that might be related to the event. Mathematically, this theorem states-
Where P(Y|X) is the probability of an event Y, given that even X has already occurred.
P(X) is the probability of event X,
P(Y) is the probability of event Y,
P(X|Y) is the likelihood of event X given a fixed value of Y.
If X represents a feature variable and Y represents a target variable, then the Bayes Classifier will assign that label to the feature that produces the highest probability. For simplicity, consider a two-class problem where the feature variable can have only two possible values, Y=1 or Y=0. Then, the Bayes Classifier will predict class 1 if Pr(Y=1|X=x0) > 0.5, and class two otherwise.
In cases of more than one feature, we can use the following formula for evaluating the probability,
where we have assumed that the two features X1 and X2, are independent of each other. In fact, because of this assumption, the word 'Naive' is attached to Bayes' classifier.
Consider the following dataset where a sportsperson plays or not was observed along with the weather conditions.
Data Source: Mitchell, T. M. (1997). Machine learning.
Suppose we now have to predict whether the person will play or not, given that humidity is 'High' and the wind is 'Strong.' Then, using the Bayes' classifier, we can compute the probability as follows:
This algorithm is similar to Bayes' classifier as it also predicts the probability that Y is associated with an input variable, X. It uses the logistic function,
and fits the parameters 𝛃0 and 𝛃1 using the maximum likelihood technique. This technique involves maximizing the likelihood function given by
After evaluating the two parameters, one can easily use the logistic function to predict the target variable probability p(xi) for a given input xi.
In the case of more than one feature variables (X1, X2,..., XP), the formula can be generalized as
This algorithm involves dividing a dataset into segments based on certain feature variables from the dataset. The threshold values for these divisions are usually mean or mode of the respective feature variable (if they are numerical). As a tree can represent the set of splitting rules used to segment the dataset, this algorithm is known as a decision tree.
Look at the example below to understand it better.
The text in red represents how the dataset has been split into segments based on the output variable. The outcome is the one that has the highest proportion of a class.
Now, the question which is quite natural to ask is what criteria this algorithm uses to split the data. There are two widely used measures to test the purity of the split (a segment of the dataset is pure if it has data points of only one class).
The first one is the Gini index defined by
that measures total variance across the N classes. Another measure is cross-entropy, defined by
In both equations, pmk represents the proportion of training variables in the mth segment that belongs to the kth class.
We split the dataset into segments based on that feature, giving rise to the minimum value of entropy or Gini index.
A forest consists of a large number of trees. Similarly, a random forest involves processing many decision trees. Each tree predicts a value for the probability of target variables. We then average the probabilities to produce the final output.
We evaluate each tree as follows:
Advantages of Random Forest Classification Algorithm
This algorithm utilizes support vector classifiers with an exciting change that makes it suitable for evaluating a non-linear decision boundary. And that becomes possible by enlarging the feature variable space using special functions called kernels. The decision boundary that this algorithm considers allows labeling the feature variable to a target variable. The mathematical function that it uses for evaluating the boundaries is given by
where K represents the kernel function, and ⍺i and ꞵ0 beta are training parameters.
Advantages of SVM Classification Algorithm
Disadvantages of SVM Classification Algorithm
Applications of SVM Classification Algorithm
Time for a tea break!
Dear reader, please feel free to take a short break now to celebrate that you have learned so much about classification algorithms. FYI, till now, the algorithms that we have discussed are all instances of supervised classification algorithms. These are the algorithms that have pre-defined target variables for them in the dataset. But, in the real world, this may not always be the case. So, we will now explore unsupervised classification algorithms where the task at hand would be to learn the pattern among input features and group together similar ones.
KNN algorithm works by identifying K nearest neighbors to a given observation point. It then evaluates the proportions of each type of target variable using the K points and then predicts the target variable with the highest ratio. For example, consider the following case where we have to label a target value to point X. Then, if we take four neighbors around it, this model will predict that the point belongs to class with the color pink.
K-Means Clustering is a clustering algorithm that divides the dataset into K non-overlapping groups. The first step for this algorithm is to specify the expected number of clusters, K. Then, the task is to divide the dataset into K clusters so that within-the-cluster variation is as tiny as possible. The algorithm proceeds as follows:
In conclusion, this algorithm minimizes the sum of squares of deviations between an input point and the respective cluster centroid. The reason for naming it as K-means clustering is that step 2a) evaluates the mean of the observations belonging to a particular cluster as the cluster centroid.
Now that you are familiar with so many different classification algorithms, it is time to understand which one to use when.
Below we have a list that will help you understand which classification algorithms you should use for solving a business problem.
Now you are entirely ready to explore some hands-on machine learning projects which implement these algorithms for solving real-world problems.
Well, we now hope that you've mastered the concepts of classification algorithms and feel like a superhero.