What is Support Vector Machine? SVM Algorithm in Machine Learning
Support Vector Machine or SVM algorithm is a simple yet powerful Supervised Machine Learning algorithm that can be used for building both regression and classification models. SVM algorithm can perform really well with both linearly separable and non-linearly separable datasets. Even with a limited amount of data, the support vector machine algorithm does not fail to show its magic.
SVM Figure 1: Linearly Separable and Non-linearly Separable Datasets
Before diving right into understanding the support vector machine algorithm in Machine Learning, let us take a look at the important concepts this blog has to offer.
Interested in learning Machine Learning? Click here to learn more about this Machine Learning Training in Bangalore!
Support Vector Machine Algorithm Example
Support vector machine or SVM algorithm is based on the concept of ‘decision planes’, where hyperplanes are used to classify a set of given objects.
Let us start off with a few pictorial examples of support vector machine algorithm. As we can see in Figure 2, we have two sets of data. These datasets can be separated easily with the help of a line, called a decision boundary.
SVM Figure 2: Decision Boundary
But there can be several decision boundaries that can divide the data points without any errors. For example, in Figure 3, all decision boundaries classify the datasets correctly. But how do we pick the best decision boundary?
SVM Figure 3: Other Possible Decision Boundaries
Well, here’s the tip: the best decision boundary is the one that has a maximum distance from the nearest points of these two classes, as shown in Figure 4.
SVM Figure 4: Maximum Distance from the Nearest Points
Also, remember that the nearest points from the optimal decision boundary that maximize the distance are called support vectors.
SVM Figure 5: Margin and Maximum Margin Classifier
The region that the closest points define around the decision boundary is known as the margin.
That is why the decision boundary of a support vector machine model is known as the maximum margin classifier or the maximum margin hyperplane.
In other words, here’s how a support vector machine algorithm model works:
- First, it finds lines or boundaries that correctly classify the training dataset.
- Then, from those lines or boundaries, it picks the one that has the maximum distance from the closest data points.
Alright, in the above support vector machine example, the dataset was linearly separable. Now, the question, how do we classify non-linearly separable datasets as shown in Figure 6?
SVM Figure 6: Non-linearly Separable Dataset
Clearly, straight lines can’t be used to classify the above dataset. That is where Kernel SVM comes into the picture.
SVM Figure 7: After Using Kernel Support Vector Classifier
What does Kernel SVM do? How does it find the classifier? Well, the Kernel SVM projects the non-linearly separable datasets of lower dimensions to linearly separable data of higher dimensions. Kernel SVM performs the same in such a way that datasets belonging to different classes are allocated to different dimensions. Interesting, isn’t it?
Well, before exploring how to implement SVM in the Python programming language, let us take a look at the pros and cons of the support vector machine algorithm.
Learn to implement Machine Learning in this blog on Machine Learning with Python for the beginner as well as experienced.
Advantages of Support Vector Machine Algorithm
- Works very well with limited datasets
- Kernel SVM contains a non-linear transformation function to convert the complicated non-linearly separable data into linearly separable data.
Disadvantages of Support Vector Machine Algorithm
- Does not work well with larger datasets
- Sometimes, training time with SVMs can be high
Become Master of Machine Learning by going through this online Machine Learning course in Singapore.
How Does the Support Vector Machine Algorithm Work?
SVM libraries are packed with some popular kernels such as Polynomial, Radial Basis Function or RBF, and Sigmoid. The classification function used in SVM in Machine Learning is SVC. The SVC function looks like this:
sklearn.SVM.SVC (C=1.0, kernel= ‘rbf’, degree=3)
- C: Keeping large values of C will indicate the SVM model to choose a smaller margin hyperplane. A small value of C will indicate the SVM model to choose a larger margin hyperplane.
- kernel: It is the kernel type to be used in SVM model building. It can be ‘linear’, ‘rbf’, ‘poly’, or ‘sigmoid’. The default value of the kernel is ‘rbf’.
- degree: It’s only considered in the case of the polynomial kernel. It is the degree of the polynomial kernel function. The default value of a degree is 3.
Alright, let us dive right into the hands-on of SVM in the Python programming language.
If you have any doubts or queries related to Data Science, do a post on Machine Learning Community.
SVM Parameters include the values, estimators, and various constraints used to implement ML algorithms. There are three types of SV parameters in a Neural Network:
Kernel transforms the input data into any first as per the user requirements. The Kernels used in SVM could be linear, polynomial, radial basis functions(RBFs), and non-linear hyperplanes, created using the polynomial and RBF functions. You can obtain accurate classifiers by separating non-linear classes through an advanced kernel.
The C parameters in Scikit-learn denote the error or penalty representing any miscalculation. You can maintain regularization by understanding the miscalculation and changing the decision boundary through tweaking the C parameters.
Gamma parameters determine their influence over a single training example. There are two types of gamma parameters, low meaning ‘far’ and high meaning ‘close’ values. The low or far values define a Gaussian function with a large variance. Whereas, high or close values define it with small variance.
Applications of SVM
SVM is mainly used to classify the unseen data and have various application in different fields:
Classifies the images of people’s faces in an environment from non-face by creating a square box around it.
The Support vector machines are used for gene classification that allows researchers to differentiate between various proteins and identify biological problems and cancer cells.
Used in training models that are used to classify the documents into different categories based on the score, types, and other threshold values.
- Generalized Predictive Control(GPC)
Provides you control over different industrial processes with multivariable version and interactor matrix. GPC is used in various industries like cement mills, robotics, spraying, etc.
- Handwriting Recognization
SVM is widely used to recognize handwritten characters and test them against pre-existing data.
Compared to the traditional query-based searching techniques, SVM has better accuracy when it comes to search and classifying the images based on various features.
Building a Support Vector Machine Classification Model in Machine Learning Using Python
Problem Statement: Use Machine Learning to predict cases of breast cancer using patient treatment history and health data
Dataset: Breast Cancer Wisconsin (Diagnostic) Dataset
Let us have a quick look at the dataset:
Classification Model Building: Support Vector Machine in Python
Let us build the classification model with the help of a Support Vector Machine algorithm.
Step 1: Load Pandas library and the dataset using Pandas
Let us have a look at the shape of the dataset:
Step 2: Define the features and the target
Have a look at the features:
Have a look at the target:
Step 3: Split the dataset into train and test using sklearn before building the SVM algorithm model
Step 4: Import the support vector classifier function or SVC function from Sklearn SVM module. Build the Support Vector Machine model with the help of the SVC function
Step 5: Predict values using the SVM algorithm model
Step 6: Evaluate the Support Vector Machine model
Implementing Kernel SVM with Sklearn SVM module
Go through these Artificial Intelligence Interview Questions And Answers to excel in your A.I Interview.
Importing the libraries:
Importing the SVC function and setting kernel as ‘poly’:
Evaluating the model:
Importing the SVC function and setting kernel as ‘rbf’:
For the best of career growth, check out Intellipaat’s Machine Learning Certification and get certified.
Importing the SVC function and setting SVM kernel as ‘sigmoid’:
Evaluating the model:
What did we learn so far?
In this SVM tutorial blog, we answered the question, ‘what is SVM?’ Some other important concepts such as SVM full form, the pros and cons of the SVM algorithm, and SVM examples, are also highlighted in this blog. We also learned how to build support vector machine models with the help of the support vector classifier function. Additionally, we talked about the implementation of Kernel SVM in Python and Sklearn, which is a very useful method while dealing with non-linearly separable datasets.
Watch this Video on Mathematics for Machine Learning
Compare the SVM Machine Learning model with other Supervised Machine Learning classification models like Random Forest and Decision Tree!