# Multiclass Classification Using SVM

*This article was published as a part of theÂ Data Science Blogathon.*

**Introduction**

**Handwritten digit classification** is one of the multiclass classification problem statements. In this article, weâ€™ll introduce the multiclass classification using **Support Vector Machines (SVM)**. Weâ€™ll first see what exactly is meant by multiclass classification, and weâ€™ll discuss how SVM is applied for the multiclass classification problem. Finally, weâ€™ll look at **Python** code for multiclass classification using **Sklearn SVM**.

The particular question on which we will be focusing in this article is as follows:

**“How can you extend a binary classifier to a multi-class classifier in case of SVM algorithm?”**

**Multiclass Classification:Â **In this type of classification, the machine learning model should classify an instance as only one of three classes or more. **For Example, **Classifying a text as positive, negative, or neutral.

**Let’s get started,**

**What are Support Vector Machines (SVM)?**

SVM is a **supervised machine learning algorithm** that helps in both **classification** and **regression** problem statements. It tries to find an optimal boundary (known as hyperplane) between different classes. In simple words, SVM does complex data transformations depending on the selected kernel function, and based on those transformations, it aims to maximize the separation boundaries between your data points.

**Working of SVM:Â **

In the simplest form where there is a linear separation, SVM tries to find a line that maximizes the separation between a two-class data set of 2-dimensional space points.

**The objective of SVM: **The objective of SVM is to find a hyperplane that maximizes the separation of the data points to their actual classes in an n-dimensional space. The data points which are at the minimum distance to the hyperplane i.e, closest points are called **Support Vectors**.

**For Example, **ForÂ the given diagram, the three points that are layered on the scattered lines are the Support Vectors (2 blue and 1 green), and the separation hyperplane is the solid red line.

Fig. Image showing the Support Vectors

**Image Source: link**

**Multiclass Classification Using SVM**

In its most basic type, SVM doesnâ€™t support multiclass classification. For multiclass classification, the same principle is utilized after breaking down the multi-classification problem into smaller subproblems, all of which are binary classification problems.

The popular methods which are used to perform multi-classification on the problem statements using SVM are as follows:

ðŸ‘‰ **One vs One (OVO) approach**

ðŸ‘‰ **One vs All (OVA) approach**

ðŸ‘‰ **Directed Acyclic Graph** (**DAG) approach**

**Â **

Now, let’s discuss each of these approaches one by one in a detailed manner:

**One vs One (OVO)Â **

ðŸ‘‰ This technique breaks down our multiclass classification problem into subproblems which are binary classification problems. So, after this strategy, we get binary classifiers per each pair of classes. For final prediction for any input use the concept of** majority voting **along with the distance from the margin as its** confidence criterion**.

ðŸ‘‰ The major problem with this approach is that we have to train **too many SVMs**.

ðŸ‘‰ Let’s have Multi-class/ Multi-labels problems with L categories, then:

For the (s, t)- th classifier:

– Positive Samples: all the points in class s ({ **x**_{i} : s âˆˆ **y**_{i }})

– Negative samples: all the points in class t ({ **x**_{i }: t âˆˆ **y**_{i }})

– f_{s, t}(**x**): the decision value of this classifier

(Â large value of f _{s, t}(**x**) â‡’ label s has a higher probability than the label t )

– f _{t, sÂ }(**x**) = – f_{ s, t}(**x**)

– Prediction: Â Â f(**x**)= argmax _{s} ( Î£ _{t} f_{s, t}(**x**) )

ðŸ‘‰ Letâ€™s have an example of **3 class classification problem**: Green, Red, and Blue.

Fig. Image showing 3 different classes

**Image Source: link**

ðŸ‘‰ In the **One-to-One** approach, we try to find the hyperplane that separates between every two classes, neglecting the points of the third class.

**For example, **hereÂ Red-Blue line tries to maximize the separation only between blue and red points while It has nothing to do with the green points.

Fig. One vs One Approach

**Image Source: link**

**Â **

**One vs All (OVA)**

ðŸ‘‰ In this technique, if we have N class problem, then we learn N SVMs:

SVM number -1 learns “class_output = 1” vs “class_output Â **â‰ Â **1″

SVM number -2 learns “class_output = 2” vs “class_outputÂ **â‰ Â **2″

:

SVM number -N learns “class_output = N” vs “class_outputÂ **â‰ Â **N”

Then to predict the output for new input, just predict with each of the build SVMs and then find which one puts the prediction the farthest into the positive region (behaves as a confidence criterion for a particular SVM).

ðŸ‘‰ Now, a very important comes to mind that **“Are there any challenges in training these N SVMs?”**

**Yes, **there are some challenges to train these N SVMs, which are:

**1. Too much Computation:Â **ToÂ implement the OVA strategy, we require more training points which increases our computation.

**2. ProblemsÂ becomes Unbalanced: **Let’s you are working on anÂ

**MNIST**dataset, in which there are 10 classes from 0 to 9 and if we have

**1000 points**per class, then for any one of the SVM having two classes, one class will have 9000 points and other will have only 1000Â data points, so our problem becomes unbalanced.

**Now, how to address this unbalanced problem?**

You have to take some representative (subsample) from the class which is having more training samples i.e, majority class. You can do this by using some below-listed techniques:

– * Use the 3-sigma rule of the normal distribution: *Fit data to a normal distribution and then subsampled accordingly so that class distribution is maintained.

– Pick some data points randomly from the majority class.

– Use a popular subsampling technique named **SMOTE**.

ðŸ‘‰ Let’s have Multi-class/ multi-labels problems with L categories, then:

For the t -th classifier:

– Positive Samples: all the points in class t ({ **x**_{i} : t âˆˆ **y**_{i }})

– Negative samples: all the points not in class t ({ **x**_{i} : t âˆ‰ **y**_{i} })

– f_{t}(**x**): the decision value for the t -th classifier.

( large value of f_{t} â‡’ higher probability that **x **isÂ in the class t)

– Prediction: Â Â f(**x**) = argmax _{t} f_{t}(**x**)

ðŸ‘‰Â In the **One vs All**Â approach, we try to find a hyperplane to separate the classes. This means the separation takes all points into account and then divides them into two groups in which there is a group for the one class points and the other group for all other points.

**For example,**Â here, the Greenline tries to maximize the gap between green points and all other points at once.

Fig. One vs All approach

**Image Source: link**

** NOTE:** A single SVM does binary classification and can differentiate between two classes. So according to the two above approaches, to classify the data points from L classes data set:

ðŸ‘‰ In the One vs All approach, the classifier can use **L** SVMs.

ðŸ‘‰ In the One vs One approach, the classifier can use **L(L-1)/2 **SVMs.

**Directed Acyclic Graph (DAG)**

ðŸ‘‰ This approach is more hierarchical in nature and it tries to addresses the problems of the One vs One and One vs All approach.

ðŸ‘‰ This is a graphical approach in which we group the classes based on some logical grouping.

ðŸ‘‰ **Benefits:** Benefits of this approach includes a fewer number of SVM trains with respect to the OVA approach and it reduces the diversity from the majority class which is a problem of the OVA approach.

ðŸ‘‰ **Problem:** If we have given the dataset itself in the form of different groups ( **e.g,Â cifar 10 image classification dataset** ) then we can directly apply this approach but if we don’t give the groups, then the problem with this approach is of finding the logical grouping in the dataset i.e, we have to manually pick the logical grouping.

**End Notes**

*Thanks for reading!*

If you liked this and want to know more, go visit my other articles on Data Science and Machine Learning by clicking on the **Link**

Please feel free to contact me on **Linkedin, Email.**

Something not mentioned or want to share your thoughts? Feel free to comment below And Iâ€™ll get back to you.

__About the author__

__About the author__

**Chirag Goyal**

Currently, I am pursuing my Bachelor of Technology (B.Tech) in Computer Science and Engineering from the **Indian Institute of Technology Jodhpur(IITJ). **I am very enthusiastic about Machine learning, Deep Learning, and Artificial Intelligence.

*The media shown in this article are not owned by Analytics Vidhya and is used at the Authorâ€™s discretion.Â *

## One thought on "Multiclass Classification Using SVM"

## Raymundo Addun says: September 04, 2022 at 6:27 am

I just admire your intelligence!!!