A brief Introduction to Support Vector Machine

Support Vector Machine (SVM) is one of the most popular Machine Learning Classifier. It falls under the category of Supervised learning algorithms and uses the concept of Margin to classify between classes. It gives better accuracy than KNN, Decision Trees and Naive Bayes Classifier and hence is quite useful.

Who should read this post

Anyone with some prior knowledge of Machine Learning concepts and is Interested in learning about SVM. If you are a beginner in the field, go through this post first.

After reading this post, you’ll know:

  1. What is SVM exactly
  2. How to use SVM classifier from Sklearn (Python)
  3. Tuning its parameters for better results

So let’s begin!

 

What is SVM?

As mentioned earlier, SVM lies in the class of supervised algorithms used for classification. Let’s start with a 2 class example:

Given classes X1 and X2, we want to find the decision boundary which separates the 2 classes the best i.e. with minimum error.

SVM does this with a ‘Hyperplane’. Now this hyperplane can be a single line in case of a 2-dimensional data and can be a plane in 3-dimensional one.

Related image
Credits: ResearchGate.com

 

Without going into the math behind the curtain, let’s understand some basic functionality here.

Support Vector Machines uses the concept of ‘Support Vectors, which are the closest points to the hyperplane.

In the above example, the red line denotes our decision boundary that separates the 2 classes (Blue stars and Red circles) and the hyphened lines represent our ‘Margin’, the gap we want between the Support Vectors of both the classes.

The Margin is defined with the help of the Support Vectors (hence the name). In our example, Yellow stars and Yellow circles are the Support Vectors defining the Margin.  The better the gap, the better the classifier works. Hence support vectors play an important role in developing the classifier.

Every new data point in test data will be classified according to this Margin. If it lies on the right side of it, it’ll be classified as a Red circle otherwise as a Blue star.

The best part is, SVM can also classify non-linear data.

Related image
Credits: Stackexchange.com

Things get little tricky in the case of non-linear data. Here SVM uses the ‘Kernel-trick, it uses a kernel function to map the non-linear data to higher dimensions so that it becomes linear and finds the decision boundary there.

A Kernel function is always used by SVM, whether it is linear or non-linear data, but its main function comes into play when the data is inseparable in its current form. Here, the Kernel function adds dimensions to the problem for classification.

Now let’s see some code.

 

Using Support Vector Machines

With the help of Sklearn, you can harness the power of SVM classifier with just a few lines of code.

from sklearn import svm 

#Using a linear kernel function
Classifier clf = svm.SVC(kernel='linear') 

#Training our classifier on training set with labels 

'''
X_train is your training data
y_train are the corresponding labels
y_pred are the predicted samples of the test data X_test
'''
clf.fit(X_train, y_train) 

#Predicting output on the Test set
y_pred = clf.predict(X_test)

#Finding the Accuracy
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

In this case, we are using a linear kernel as you can see. Depending on the problem, you can use different types of Kernel functions:

  • Linear
  • Polynomial
  • Radial Basis Function
  • Gaussian
  • Laplace

… and many more. Choosing the right kernel function is important for building the classifier. In the next section, we will tune the hyperparameters for our making our classifier even better.

You can access the complete code here.

If you’re bored,  here’s a cute cat!

 

Want more Cute Cat Photos? Check out our website by clicking the photo #cutecats #catloverscommunity #cats #kittens #catvideos

 

Tuning Parameters

Kernel: We have already discussed how important kernel functions are. Depending on the nature of the problem, the right kernel function has to be chosen as the kernel-function defines the hyperplane chosen for the problem. Here is a list of the most used Kernel functions.

Regularization: Ever heard of the term Overfitting? If you haven’t I think you should learn some basics from here. In SVM, to avoid overfitting, we choose a Soft Margin, instead of a Hard one i.e. we let some data points enter our margin intentionally (but we still penalize it) so that our classifier don’t overfit on our training sample. Here comes an important parameter Gamma (γ), which control Overfitting in SVM. The higher the gamma, the higher the hyperplane tries to match the training data. Therefore, choosing an optimal gamma to avoid Overfitting as well as Underfitting is the key.

Error Penalty: Parameter C represents the error penalty for misclassification for SVM. It maintains the tradeoff between smoother hyperplane and misclassifications. As mentioned earlier, we do allow some misclassifications for avoiding overfitting of our classifier.

These are the most important parameters used for tuning the SVM classifier.

Overall SVM has many advantages as it provides high accuracy, has low complexity and also works quite well for non-linear data. The disadvantage being, it needs more training time compared to other algorithms such as Naive Bayes.

That is it for SVMs! If you have any questions, let me know in the comments.

Congratulations on making it to the end of the post!

Here’s a cookie for you

Image result for cookie
Don’t eat too much

 

One thought on “A brief Introduction to Support Vector Machine

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: