
SVM aims to find the hyperplane that best seperates data points of different classes in a high dimensional space. Aim is to maximize margins between the hyperplane and closest points from the dataset.
Implementation
- textual data represented as vectors using TF-IDF
- equation of hyperplane given as:
- distance between a point to a hyperplane is
- SVM aims to minimize value of
- this is known as the 'primal' problem of SVMs
Opitimization
1. Hinge Loss Function
- if predicted value and actual value have the same sign, cost is 0
- otherwise we calculat the loss value
2. Regularisation Function
The regularization function is often added to discourage the model from fitting the training data too closely and overfitting.
Kernels

What is a Kernel?
A kernel is a function that compares 2 data points and returns 1 similarity score.
2 Inputs --> Kernel Function --> 1 Similarity ScoreDuring training, the kernel compares many pairs of training points. This produces a kernel matrix, also called a gram matrix.
SVM uses these similarity scores to find the class boundary. Basically draw hyperplane.
The width of kernel represents the distance over which two points are considered similar.
- data in input space is not linearly seperable so we need to represent/map it in a higher dimensional space so that they become linearly seperable
- this representing onto a higher dimension is called the kernel trick
LHS represents the kernel function that takes in our lower dimensional and and gives us a single value. RHS represents a transformation, of and onto a higher dimension.
- since we are dealing with a high dimensional space, we might think we have to explicitly calculate the coordinates of data points in the higher dimension
- however, instead of doing this, we can compute dot products between the data points in this space
For Example:
With and :
| Route | Working | Result |
|---|---|---|
| Kernel | , then | 25 |
| Transform | and , then their dot product is | 25 |
Types of Kernels
| Type of SVM | Mercer Kernel | Description |
|---|---|---|
| Radial Basis Function | One class learning with as the width of the kernel | |
| Linear | Two class learning | |
| Polynomial | is the order of the polynomial | |
| Sigmoid | A Mercer kernel only for certain values of and |
Intuitive Understanding of the variables:
(width of kernel): this represents the distance between 2 points at which it is still considered similar.
Another way to see it is that controls how quickly that similarity decreases:
- small : narrow influence, therefore only very close points are similar
- large : wide influence, farther points are also considered similar
Code
from sklearn import svm
# Create a svm Classifier
clf = svm.SVC(kernel='rbf') # RBF kernel
# Train the model using the training sets
clf.fit(train_review_tfidf, train_sent)
# Predict the response for test dataset
y_pred = clf.predict(test_review_tfidf)from sklearn import svm
svm = svm.SVC(kernel='linear')
svm.fit(X_train, y_train)
predictions = svm.predict(X_test)
print("Predictions:", predictions)
print("Labels:", y_test)SVCstands for Support Vector Classifier, and it lives insklearn.svm- the input is already vectorised, so
TfidfVectorizermust run beforefit kernelaccepts'linear','rbf','poly'and'sigmoid', which are the 4 available kernels- the default kernel is
'rbf', sosvm.SVC()with no argument is the RBF version gammasets the kernel width for'rbf', where a largegammagives a narrow reach and a wiggly boundaryCcontrols the penalty for training mistakes, and in sklearn a largeCmeans less regularisation

SVM needs its text as vectors before it can find a boundary, and the usual choice is Term Frequency-Inverse Document Frequency (TF- IDF).