ISLR Chapter 9 — Support Vector Machines
Tags: datascience, machinelearning, tutorial
Published: 2020-11-08 23:44:40

> This is a summary of chapter 9 of the _Introduction to Statistical Learning_ textbook. I’ve written a 10-part guide that covers the entire book. The guide can be read at [my website](https://www.bijenpatel.com/guide/), or [here](https://dev.to/bijenpatel/series/9611) at DEV. Subscribe to stay up to date on my latest Data Science & Engineering guides!

_Support vector machines_ (SVMs) are often considered one of the best “out of the box” classifiers, though this is not to say that another classifier such as logistic regression couldn’t outperform an SVM.

The SVM is a generalization of a simple classifier known as the _max<span id="rmm">i</span>mal margin classifier_. The maximal margin classifier is simple and intuitive, but cannot be applied to most datasets because it requires classes to be perfectly separable by a boundary. Another classifier known as the _support vector classifier_ is an extension of the maximal margin classifier, which can be applied in a broader range of cases. The support vector machine is a further extension of the support vector classifier, which can accommodate non-linear class boundaries.

SVMs are intended for the binary classification setting, in which there are only two classes.

# Maximal Margin Classifier

## What is a Hyperplane?

In a _p_-dimensional space, a hyperplane is a flat subspace of dimension _p_ — 1\. For example, in a two-dimensional setting, a hyperplane is a flat one-dimensional subspace, which is also simply known as a line. A hyperplane in a _p_-dimensional setting is defined by the following equation:

![mmc-hyperplane-equation.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463155170/KMeAUFzj9.png)

Any point _X_ in _p_-dimensional space that satisfies the equation is a point that lies on the hyperplane. If some point _X_ results in a value greater than or less than 0 for the equation, then the point lies on one of the sides of the hyperplane.

In other words, a hyperplane essentially divides a _p_-dimensional space into two parts.

## Classification Using a Hyperplane

Suppose that we had a training dataset with _p_ predictors and _n_ observations. Additionally, the observations were either associated with one of two classes. Suppose that we also had a separate test dataset. Our goal is to develop a classifier based on the training data, and to classify the test data. How can we classify the data based on the concept of the separating hyperplane?

Assume that it is possible to create a hyperplane that separates the training data perfectly according to their class labels. We could use this hyperplane as a natural classifier. An observation from the test dataset would be assigned to a class, depending on which side of the hyperplane it is located. The determination is made by plugging the test observation into the hyperplane equation. If the value is greater than 0, it is assigned to the class corresponding to that side. If the value is less than 0, then it is assigned to the other class.

However, when we can perfectly separate the classes, many possibilities exist for the hyperplane. The following chart shows an example where two hyperplanes separate the classes perfectly.

![separating-hyperplane.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463180848/klNPnwcH4.png)

This is where the maximal margin classifier helps determine the hyperplane to use.

## Maximal Margin Classifier

The maximal margin classifier is a separating hyperplane that is farthest from the training observations.

The method involves determining the perpendicular distance from each training observation to some hyperplane. The smallest such distance is known as the margin. The maximal margin classifier settles on the hyperplane for which the margin is largest. In other words, the chosen hyperplane is the one that has the farthest minimum distance to the training observations.

The maximal margin classifier is often successful, but can lead to overfitting when we have a lot of predictors in our dataset.

The points that end up supporting the maximal margin hyperplane are known as _support vectors_. If these points are moved even slightly, the maximal margin hyperplane would move as well. The fact that the maximal margin hyperplane depends only on a small subset of observations is an important property that will also be discussed in the sections on support vector classifiers and support vector machines.

![support-vectors.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463211623/nsBlxtvfh.png)

## Construction of the Maximal Margin Classifier

The maximal margin hyperplane is the solution to an optimization problem with three components:

![mmc-construction.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463233217/UDARbVizd.png)

_M_ is the margin of the hyperplane. The second component is a constraint that ensures that the perpendicular distance from any observation to the hyperplane is given by the following:

![perpendicular-distance-equation.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463285962/duoDQW5CS.png)

The third component guarantees that each observation will be on the correct side of the hyperplane, with some cushion _M_.

## Non-separable Case

The maximal margin classifier is a natural way to perform classification, but only if a separating hyperplane exists. However, that is usually not the case in real-world datasets.

![non-separable-data.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463300003/Wn0xQ6nrC.png)

The concept of the separating hyperplane can be extended to develop a hyperplane that _almost_ separates the classes. This is done by using a _soft margin_. The generalization of the maximal margin classifier to the non-separable case is known as the supper vector classifier.

# Support Vector Classifier

In most cases, we usually don’t have a perfectly separating hyperplane for our datasets. However, even if we did, there are cases where it wouldn’t be desirable. This is due to sensitivity issues from individual observations. For example, the addition of a single observation could result in a dramatic change in the maximal margin hyperplane.

Therefore, it is usually a good idea to consider a hyperplane that does _not_ perfectly separate the classes. This provides two advantages:

*   Greater robustness to individual observations
*   Better classification of most of the training observations

In other words, it is usually worthwhile to misclassify a few training observations in order to do a better job of classifying the other observations. This is what the support vector classifier does. It allows observations to be on the wrong side of the margin, and even the wrong side of the hyperplane.

![svc.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463319534/fQYoPL_lO.png)

## Details of the Support Vector Classifier

The support vector classifier will classify a test observation depending on what side of the hyperplane that it lies. The hyperplane is the solution to an optimization problem that is similar to the one for the maximal margin classifier.

![svc-construction.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463336630/zxRkXcQuM.png)

_M_ is the margin of the hyperplane. The second component is a constraint that ensures that the perpendicular distance from any observation to the hyperplane is given by the following:

![perpendicular-distance-equation-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463363562/CBUHWtSJ7.png)

_ϵ_ is a _slack variable_ that allows observations to be on the wrong side of the margin or hyperplane. It tells us where the _i_-_th_ observation is located, relative to the hyperplane and margin.

*   If _ϵ_ = 0, the observation is on the correct side of the margin
*   If _ϵ_ > 0, the observation is on the wrong side of the margin
*   If _ϵ_ > 1, the observation is on the wrong side of the hyperplane

_C_ is a nonnegative tuning parameter that bounds the sum of the _ϵ_ values. It determines the number and severity of violations to the margin and hyperplane that will be tolerated.

In other words, _C_ is a budget for the amount that the margin can be violated by the _n_ observations. If _C_ = 0, then there is no budget, and the result would simply be the same as the maximal margin classifier (if a perfectly separating hyperplane exists). If _C_ > 0, no more than _C_ observations can be on the wrong side of the hyperplane because _ϵ_ > 1 in those cases, and the constraint from the fourth component doesn’t allow for it. As the budget increases, more violations to the margin are tolerated, and so the margin becomes wider.

It should come as no surprise that _C_ is usually chosen through cross-validation. _C_ controls the bias-variance tradeoff. When _C_ is small, the classifier is highly fit to the data, resulting in high variance. When _C_ is large, the classifier may be too general and oversimplified for the data, resulting in high bias.

In support vector classifiers, the support vectors for the hyperplane are a bit different than the ones from the maximal margin hyperplane. They are the observations that lie directly on the margin and the wrong side of the margin. The larger the value of _C_, the more support vectors there will be.

The fact that the supper vector classifier is based only on a small subset of the training data means that it is robust to the behavior of observations far from the hyperplane. This is different from other classification methods such as linear discriminant analysis, where the mean of all observations within a class help determine the boundary. However, support vector classifiers are similar to logistic regression because logistic regression is not very sensitive to observations far from the decision boundary.

# Support Vector Machines

First, we will discuss how a linear classifier can be converted into a non-linear classifier. Then, we’ll talk about support vector machines, which do this in an automatic way.

## Classification with Non-linear Decision Boundaries

The support vector classifier is a natural approach for classification in the binary class setting, if the boundary between the classes is linear. However, there are many cases in practice where we need a non-linear boundary.

In chapter 7, we were able to extend linear regression to address non-linear relationships by enlarging the feature space by using higher-order polynomial functions, such as quadratic and cubic terms. Similarly, non-linear boundaries can be created through the use of higher-order polynomial functions. For example, we could fit a support vector classifier using each predictor and its squared term:

![squared-terms.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463389905/f0k24KQJg.png)

This would change the optimization problem to become the following:

![svm-optimization-problem.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463422323/xIQM0c17K.png)

However, the problem with enlarging the feature space is that there are many ways to do so. We could use cubic or even higher-order polynomial functions. We could add interaction terms. Many possibilities exist, which could lead to inefficiency in computation. Support vector machines allow for enlarging the feature space in a way that leads to efficient computations.

## Support Vector Machines

The _support vector machine_ is an extension of the support vector classifier that enlarges the feature space by using _kernels_. Before we talk about kernels, let’s discuss the solution to the support vector classifier optimization problem.

## Solution to Support Vector Classifier Optimization Problem

The details of how the support vector classifier is computed is highly technical. However, it turns out that the solution only involves the inner products of the observations, instead of the observations themselves. The inner product of two vectors is illustrated as follows:

![inner-product.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463435645/r85_ZsayG.png)

![inner-product-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463447645/rdM70NOTv.png)

The linear support vector classifier can be represented as:

![linear-svc-equation.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463464067/Xe0C02SpZ.png)

There are _n_ parameters (_α_ᵢ) per training observation. The parameters are estimated with the inner products between all pairs of training observations.

To evaluate the support vector classifier function _f_(_x_), we compute the inner product between a new observation _x_ and each training observation _x_ᵢ. However, the _α_ᵢ parameters are nonzero only for the support vectors. In other words, if an observation is not a support vector, then its _α_ᵢ is zero. If we represent _S_ as the collection of the support vectors, then the solution function can be rewritten as the following:

![linear-svc-equation-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463483594/M4p0AKpuX.png)

## Kernels

A kernel is a function that quantifies the similarity of two observations, and is a generalization of the inner product. The kernel function used in the support vector classifier is simply

![kernel-function.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463492900/weKzm056m.png)

This is known as a linear kernel because the support vector classifier results in a linear boundary. The linear kernel determines the similarity of observations by using the Pearson correlation.

Instead of using the linear kernel, we could use a polynomial kernel:

![polynomial-kernel.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463503242/8jjKsZIcK.png)

Using a nonlinear kernel results in a non-linear decision boundary. When the support vector classifier is combined with a nonlinear kernel, it results in the support vector machine. The support vector machine takes on the form:

![svm-function.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463519395/7eXKIj_26.png)

![svm.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463533442/rgt2v7Poz.png)

The polynomial kernel is just one example of a nonlinear kernel. Another common choice is the radial kernel, which takes the following form:

![radial-kernel-equation.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463562456/1nTXRbfHf.png)

![svm-radial.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463573634/38dRg7b88.png)

The advantage to using kernels is that the computations can be performed without explicitly working in the enlarged feature space. For example, in the polynomial kernel, we are simply determining the summation of the inner products, and _then_ transforming our result to a higher degree or dimension _d_. This is known as the kernel trick.

# SVMs with More than Two Classes

The concept of separating hyperplanes upon which SVMs are based does not lend itself naturally to more than two classes. However, there are a few approaches for extending SVMs beyond the binary class setting. The most common approaches are _one-versus-one_ and _one-versus-all_.

## One-Versus-One Classification

A _one-versus-one_ or _all-pairs_ approach develops multiple SVMs, each of which compares a pair of classes. Test observations are classified in each of the SVMs. In the end, we count the number of times that the test observation is assigned to each class. The class that the observation was assigned to most is the class assigned to the test observation.

## One-Versus-All Classification

The one-versus-all approach develops multiple SVMs, each of which compares one class to all of the other classes. Assume that each SVM resulted in the following parameters from comparing some class k to all of the others:

![one-versus-all-1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463599463/532yBfT3K.png)

Let x represent a test observation. The test observation is assigned to the class for which the following is the largest:

![one-versus-all-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605463611291/k_E3Qla3N.png)

# SVMs vs Logistic Regression

As previously mentioned, only the support vectors end up playing a role in the support vector classifier that is obtained. This is because the loss function is exactly zero for observations that are on the correct side of the margin.

The loss function for logistic regression is not exactly zero anywhere. However, it is very small for observations that are far from the decision boundary.

Due to the similarities between their loss functions, support vector classifiers and logistic regression often give similar results. However, when the classes are well separated, support vector machines tend to perform better. In cases where there is more overlap, logistic regression tends to perform better. In any case, both should always be tested, and the method that performs best should be chosen.

_Originally published at_ [https://www.bijenpatel.com](https://www.bijenpatel.com/guide/islr/support-vector-machines/) _on August 9, 2020._

> I will be releasing the equivalent Python code for these examples soon. Subscribe to get notified!

# ISLR Chapter 9 — R Code

## Support Vector Classifiers


```r
library(ISLR)
library(MASS)
library(e1071)

# We will generate a random dataset of observations belonging to 2 classes
set.seed(1)
x=matrix(rnorm(20*2), ncol=2)
y=c(rep(-1, 10), rep(1, 10))
x[y==1,]=x[y==1,] + 1

plot(x, col=(3-y))

# To use SVM, the response must be encoded as a factor variable
data = data.frame(x=x, y=as.factor(y))

# Fit a Support Vector Classifier with a cost of 10
# The scale argument is used to scale predictors
# In this example, we will not scale them
svmfit = svm(y~., data=data, kernel="linear", cost=10, scale=FALSE)

# Plot the fit
plot(svmfit, data)

# Determine which observations are the support vectors
svmfit$index

# Fit an SVM with a smaller cost of 0.1
svmfit = svm(y~., data=data, kernel="linear", cost=0.1, scale=FALSE)

# The e1071 library contains a tune function
# The function performs cross-validation with different cost values
set.seed(1)
tune.out = tune(svm, y~., data=data, kernel="linear", ranges=list(cost=c(0.001, 0.01, 0.1, 1, 5, 10)))

# Check the summary to see the error rates of the different models
# The model with a cost of 0.1 has the lowest error
summary(tune.out)

# Choose the best model
bestmod = tune.out$best.model
summary(bestmod)

# The predict function can be used to predict classes on a set of test observations
xtest = matrix(rnorm(20*2), ncol=2)
ytest = sample(c(-1, 1), 20, rep=TRUE)
xtest[ytest==1,]=xtest[ytest==1,] + 1
testdata=data.frame(x=xtest, y=as.factor(ytest))
ypred = predict(bestmod, testdata)
table(predict=ypred, truth=testdata$y)</span>
```


## Support Vector Machines


```r
# Now, we will fit a Support Vector Machine model
# We can do this by simply using a non-linear kernel in the svm function

# Generate a dataset with a non-linear class boundary
set.seed(1)
x=matrix(rnorm(200*2), ncol=2)
x[1:100,]=x[1:100,]+2
x[101:150,]=x[101:150,]-2
y=c(rep(1, 150), rep(2, 50))
data = data.frame(x=x, y=as.factor(y))
plot(x, col=y)

# Split the data into training and test sets
train = sample(200, 100)

# Fit an SVM with a radial kernel
svmfit=svm(y~., data=data[train,], kernel="radial", gamma=1, cost=1)
plot(svmfit, data[train,])

# Perform cross-validation using the tune function to test different choices for cost
set.seed(1)
tune.out = tune(svm, y~., data=data[train,], kernel="radial",
                ranges=list(cost=c(0.1, 1, 10, 100, 1000),
                gamma=c(0.5, 1, 2, 3, 4)))
summary(tune.out)
# Cost of 1 and Gamma of 2 has the lowest error

# Test the model on the test dataset
table(true=data[-train,"y"], pred=predict(tune.out$best.model, newx=data[-train,]))</span>
```