This is an SVM implementation with numpy and the quadratic programming framework CVXOPT.
The Support Vector Machine (SVM), proposed by Vladimir Vapnik and Alexey Chervonenkis in the early 1960s but not published until 19951, is an algorithm designed to classify data points of two discrete classes by discovering the optimal hyperplane to separate them in an n-dimensional space.
The mathematical modelling of the problem is as follows:
The classes
In order to define the Hyperplane, we need to calculate a weight W and a bias b such as:
To simplify these two expressions we can multiply them with
And we define
Now in order to get the equation of the width of the margin we can see that
For the convenience of our mathematic solution we decide to convert our minimization target to
We have reached to a point where we have to find the extremum of a function with constraints. This can be achieved with Lagrange multipliers:
By setting the partial derivatives of this function to 0 we can find the extremum we are looking for. So we have:
and
The problem we came up with is called a quadratic optimization problem and can be solved using a computer. The CVXOPT library is used in the code of this repository to implement our solution.
The above mathematics are just the foundation of the SVM classifier. The code contains also implementation of the soft margin and gaussian kernel trick.
This implementation includes an SVM module with Linear and Gaussian kernels, along with a test to showcase its ability to classify synthetic data.
In the case of linearly separable data, the SVM effectively classifies the points into two distinct classes. The training and test sets are visualized below:
Training Set | Test Set |
---|---|
![]() |
![]() |
Here, the SVM creates a linear decision boundary to separate the two classes in the training set, and it generalizes well to new, unseen data in the test set.
For non-linearly separable data, the SVM leverages the Gaussian kernel trick to handle complex decision boundaries. The training and test sets are illustrated below:
Training Set | Test Set |
---|---|
![]() |
![]() |
In this scenario, the SVM employs a Gaussian kernel to transform the feature space, allowing for more intricate decision boundaries that can capture the underlying patterns in the data.
These visualizations demonstrate the versatility of the SVM classifier in handling both linearly and non-linearly separable data through appropriate kernel choices.
Footnotes
-
Cortes, Corinna, and Vladimir Vapnik. "Support-vector networks." Machine learning 20 (1995): 273-297. ↩