Classification
Kernel SVM
2023-12-18
If the data cannot be separated by a straight line or hyperplane, the kernel trick can implicitly map the data into a higher-dimensional feature space, where linear classification may become possible.
For example, if we have green and blue data points placed on feature x1, there is no single decision boundary that can classify them. However, by transforming the data (x12) into feature x2, we can create a decision boundary represented by the red line.

Similar to the example given above, similarity characteristics can also be used. For instance, using the RBF (Radial Basis Function) with feature x1 data, we can represent the data of x1 in feature x2 (x2 = exp(-(x1+2)2)) and in feature x3 (x3 = exp(-(x1-1)2)). This representation can yield a linear decision boundary in the x2 vs. x3 window as shown below.

The first method used here is called a polynomial kernel, and the second method used is the Gaussian RBF kernel.
By using a kernel, not only can we transform features, but we can also support kernel functions that enable the calculation of data (x,y) → transformation (Φ(x),Φ(y)) → dot product (<Φ(x)Φ(y)>). This allows us to reduce the amount of numerical calculations dramatically by calculating data (x,y) → dot product (K(x,y)).
Even if we have no knowledge of the actual transformation of individual data, it is still possible to perform calculations as long as we have the kernel. (Detail explanation will be given below.)
Following are some of the widely used kernels. In this blog, we will use polynomial kernels and RBF kernels for Excel demonstration.
Linear Kernel:
\(K(a,b)=a^Tb\)K
Polynomial Kernel:
\(K(a,b)=(\gamma a^Tb+r)^d\)
Gaussian RBF Kernel:
\(K(a,b)=\exp (-\gamma\parallel a-b\parallel ^2)\)
We borrow the objective function and constraints from the soft margin SVM.
\(\max_\lambda(\sum _i^{\ }\lambda_i-\dfrac{1}{2}w^Tw)\)
Substituting the equation of the coefficient, w, defined below into the objective function:
\(w=\sum _{\ }^{\ }\lambda_jy_ix_i\)
The objective function becomes
\(\max_\lambda\left(\sum _i^{\ }\lambda_i-\dfrac{1}{2}\sum _i^{\ }\sum _j^{\ }\lambda_i\lambda_jy_iy_jx_ix_j\right)\)
By introducing Kernel, K(xixj) in place of xixj, the objective function in a Kernel form:
\(\max_\lambda\left(\sum _i^{\ }\lambda_i-\dfrac{1}{2}\sum _i^{\ }\sum _j^{\ }\lambda_i\lambda_jy_iy_jK(x_i,x_j)\right)\)
The definition of the Kernel for each case are deployed as follows:
For the linear Kernel for the two variables (x1, x2)
\(K(x_i,x_j)=(x1_ix1_j+x2_ix2_j)\)
For the polynomial Kernel for the two variables with d=3, r=1, γ=1
\(K(x_i,x_j)=(x1_ix1_j+x2_ix2_j+1)^3\)
For RBF Kernel with γ=1,
\(K(x_i,x_j)=\exp \left(-(x1_i-x1_j)^2-(x2_i-x2_j)^2\right)\)
Constraints concerned, there is no change from the soft margin SVM.
\(\sum _{\ }^{\ }\lambda_iy_i=0\)
\(0\le \lambda_i\le C\)
In the case of kernel SVM, the predicted \(\hat y\) for the projected Φ(x) in the new feature space is obtained from:
\(\hat y(x)=w^T\Phi(x)+b=[\sum _i^{\ }\lambda_iy_i\Phi(x_i)]^T\Phi(x)+b=\sum _i^{\ }\lambda_iy_iK(x_i,x)+b\)
If there are m number of the support vectors whose λ > 0, the bias of each sample, bk is calculated by the following equation,
\(b_k=y_k-\sum _j^{\ }\lambda_jy_jK(x_j,x_k)\) where k=1,m
then, take the average of them to get the bias for Hyperplanes.
\(b=\sum _k^{\ }b_k/m\)
Step by step procedures are illustrated below:
Step 1. Allocate a Lagrange multiplier λ per sample with a random value > 0
Step 2. Calculate Kernel K(xi, xj). The size of K matrix = [nxn]
Step 3. Perform a matrix multiplication yyT, The size of yyT = [nx1][1xn]=[nxn]
Step 4. Perform a matrix multiplication λλT, The size of λλT=[nxn]
Step 5. Perform component-wise multiplication (Hadamard product) to compute the objective function
\(A(i,j)=\lambda\lambda^T(i,j)\odot yy^T(i,j)\odot K(i,j)\)
Step 6. Calculate the objective using A(i,j) from Step 5.
\(\sum _i^{\ }\lambda_i-\dfrac{1}{2}\sum _i^{\ }\sum _j^{\ }A(i,j)\)
Step 7. Constraints
\(\sum _i^{\ }\lambda_iy_i=0,\)
\(0\le \lambda_i\le C\)
Step 8. Run Solver w.r.t. λ with the constraints
Step 9. Calculate bias, b, and perform any inference toward new sample.
In this blog, we use XOR data to demonstrate the polynomial Kernel.

(step 1) Generate XOR data
- Use Array Function to generate 200 two dimensional (x,y) data points over the Cells A2:B101 with mean=0 and std=1 by using norm.inv(rand(),0,1).
- At Column C, generate XOR data by XOR(if(x>0,1,0),if(y>0,1,0))
- Labeling by TRUE = 1, FALSE = -1.
(step 2) Generate random valued Lagrange multipliers at Column F corresponding samples
- Select Cell F2:F101 first
- = RAND()
- Run Array Function by Cntl+Alt+Shift
(step 3) Calculate Kernel Matrix, K [100x100] at H2:DC101
- Fill H1:DC1 with 2~101 for column index as an input for INDEX function
- Fill G2:G101 with 2~101 for row index as an input for INDEX function
- Perform the following Excel function at Cell H2
- (SUMPRODUCT(INDEX($A$1:$B$201,$G2,1):INDEX($A$1:$B$201,$G2,2),INDEX($A$1:$B$201,H$1,1):INDEX($A$1:$B$201,H$1,2))+1)^3
- Autofill over the cells H2:DC101
(step 4) Calculate yyT Matrix [100x100] at H104:DC203
- Select Cells H104:DC203, and perform the following Array Function
- =MMULT(C2:C101,TRANSPOSE(C2:C101))
(step 5) Calculate λλT Matrix [100x100] at H206:DC305
- Select Cells H206:DC305, and perform the following Array Function
- =MMULT(F2:F101,TRANSPOSE(F2:F101))
(step 6) Constraints
- Cell B104 =SUMPRODUCT(C2:C101,F2:F101) which must be zero at Solver
- Cell B106 =MAX(F2:F101) which must be < C at Solver
(step 7) Calculate the objective function to maximize
- Cell B105 =SUM(F2:F101)-0.5*SUM(SUMPRODUCT(H2:DC101,H104:DC203,H206:DC305))
(step 8) Run Solver with the setting as below

(step 9) Calculate the predicted at Column D before the bias calculation at the next step.
\(w^T\Phi(x)=\sum_{}^{}\lambdaiyiK(xi,x)\)
=SUMPRODUCT(C$2:C$101,F$2:F$101,INDEX($G$1:$DC$101,2,G101):INDEX($G$1:$DC$101,101,G101))
As you can see above, when performing predictions on a new variable, the entire training data is needed to compute the kernel. In other words, all kernel methods become lazy learners.
(step 10) Bias, b is the average of y-wTΦ(x).
=(SUM(C2:C101)-SUM(D2:D101))/100
The results of the optimization show that, as can be seen below, some of the given data points are located outside the decision boundary for y=1 and y=-1.

The usage example of the RBF kernel used the following Half-moon data, and the RBF kernel equation defined earlier was used.
\(K(x_i,x_j)=\exp (-(x1_i-x1_j)^2-(x2_i-x2_j)^2)\)
The hyperparameters are γ, which represents the standard deviation, and C, which is the Lagrange multiplier used to constrain. We use γ=3, C=3 in the Excel example.

The results show that about 30% of the data is located outside the boundary. It is necessary to tune the hyperparameters for a better result.
