iTranslated by AI
Thinking about Optimization (2) — SVM
Purpose
Continuing from the previous article, this time we will cover SVM (Support Vector Machine).
You might have read about SVM in books and vaguely understood that it is optimized to maximize the margin between support vectors and the decision boundary, leading you to simply use the API. Since this often leaves the underlying process unclear, I would like to actually work through the calculations manually.
Scope of this article
We will find the decision boundary for a dataset consisting of three points. We will formulate the primal problem and derive the dual problem from it. By examining these specifically, we aim to gain a better understanding of how it works.
Dataset
I considered three patterns, but I felt the following one was best from an educational perspective and for ease of calculation, so I will use it. While other cases ultimately lead to similar results, hand-calculations showed that the tedious parts vary each time, making the article unnecessarily long without adding much essence. Therefore, I decided to narrow the problem setting down to one type.
As shown below:

We prepare a dataset
Primal Problem
For a while, I will write it out following PRML.
We want to find the decision boundary in this form. We will classify the two classes based on
Since the parameter values for the decision boundary have not yet been determined, we will call it a "hyperplane" instead of a boundary[1].
The distance from
Since the support vectors are those for which this is minimized, the distance to the support vectors is:
Since we want to maximize this value—that is, the distance between the support vector and the hyperplane (the margin)—the parameters for the decision boundary can be found by solving the following Max-Min problem:
Since
Under these conditions, the remaining task is to maximize
Dual Problem
Since it is difficult to solve the primal problem as it is, we will find an equivalent problem called the dual problem and solve it. According to the reference [Kernel Methods], when solving a convex optimization problem, considering the dual problem is often useful.
Here, instead of using the PRML notation exactly, we will use
To find the points that give the extreme values, we take the partial derivatives of
Substituting these into equation (3), the coefficient of
Organizing the indices and setting the last expression as
The following problem using this
...
Skipping a lot here
It is a bit hard to understand why the task of maximizing
By reading pp. 212–215 of [Kernel Methods], we see that since the primal problem is a convex optimization problem and satisfies the Slater's condition[3], strong duality holds. Therefore, the optimal value
In short, in this case, we just need to solve the dual problem to find
Since
Example
Let's find the decision boundary for the three-point dataset mentioned at the beginning.
Writing equation (4) specifically:
Also, calculating the inner products between data points beforehand:
Therefore,
We maximize this. The possible maximum value is
First, for the second term on the right-hand side,
Ultimately, though the calculation is a bit tedious, the solution is found in the shaded region, and by maximizing

At this point, from equation (6), the decision boundary is:
Determining
Implementing in Python
Since we are here, let's also take a look at the implementation in Python. We will use scikit-learn.
from sklearn.svm import LinearSVC
X = np.array([
[2, 1],
[3, -1],
[0, 1],
])
y = np.array([1, -1, 1])
clf = LinearSVC(C=.1)
result = clf.fit(X, y)
I think something like this should work. Since I wasn't sure how to visualize the decision boundary, I referred to [Python] Thoroughly Explaining the Theory and Implementation of Support Vector Machines (SVM).
import mglearn
mglearn.plots.plot_2d_separator(result, X, fill=False)
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')
plt.show()
I suspect the formulas used internally might be slightly different, resulting in the decision boundary shown below, but I believe it is essentially not much different from what we calculated this time. Or so I would like to believe[4].

Summary
We attempted to find the SVM decision boundary by manual calculation for a dataset that is as simple as possible. Even for such a small dataset, the calculation is very demanding, making it clear that realistic problems cannot be solved by hand. Therefore, while we usually handle it with APIs as before, I think it's not a bad thing to know the ideas behind it.
References
- [PRML] C. M. Bishop, Pattern Recognition and Machine Learning, pp.325-331, Springer, 2006
- [Kernel Methods] Kenji Fukumizu, Introduction to Kernel Methods (カーネル法入門), Asakura Shoten, 2010
-
In this case, it is a regular plane. ↩︎
-
Since the normal vector of the hyperplane is
, we just need to find\mathbf{w} such thatt lies on the hyperplane. At this point,\xi_i + \mathbf{w} t is the distance we want to calculate. Since|\mathbf{w} t| = |\mathbf{w}| |t| , the distance to the hyperplane is0 = y(\xi_i + \mathbf{w} t) = \mathbf{w}^T \xi_i + \mathbf{w}^T \mathbf{w} t + b = y(\xi_i) + |\mathbf{w}|^2 t ↩︎|\mathbf{w}| |t| = |y(\xi_i)| / |\mathbf{w}|. -
While an extreme dataset might not satisfy it, the three-point dataset considered here has sufficient separability and satisfies the condition. ↩︎
-
Since it's a manual calculation, there is of course a high possibility that I made a mistake. ↩︎
Discussion