SVM
kernel design
XOR problem
machine learning
support vector machines

Designing a Kernel for a support vector machine XOR

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Support Vector Machines (SVMs) are powerful supervised learning models widely used for classification and regression tasks. One of the key components that enable SVMs to handle non-linear decision boundaries is the kernel function. Kernels allow SVMs to implicitly map input data into high-dimensional feature spaces, where a linear separation becomes feasible. This article walks through the process of designing a kernel for an SVM to solve the XOR problem, highlighting the technical details with concrete examples.

Understanding the XOR Problem

The XOR (exclusive or) problem is a classic example in machine learning and pattern recognition. It is a binary classification problem where the output is true if the inputs are different, and false if the inputs are the same. Given inputs (x1,x2)(x_1, x_2), the XOR function is defined as:

XOR(x1,x2)=(x1¬x2)(¬x1x2)\text{XOR}(x_1, x_2) = (x_1 \land \lnot x_2) \lor (\lnot x_1 \land x_2)

The XOR problem is non-linearly separable in a 2D space, meaning no single straight line can separate the classes perfectly. This challenges conventional linear classifiers and makes it an ideal test case for kernel functions.

The Kernel Trick

The kernel trick is a method that enables SVMs to construct a non-linear decision boundary by mapping input features into a high-dimensional space using a kernel function. The kernel function computes the inner product of two vectors in this high-dimensional space without explicitly performing the transformation, which is computationally efficient.

Mathematically, the kernel function k(x,x)k(x, x') corresponds to an implicit feature map ϕ(x)\phi(x) such that:

k(x,x)=ϕ(x),ϕ(x)k(x, x') = \langle \phi(x), \phi(x') \rangle

This means we never need to compute ϕ(x)\phi(x) directly. We only need the kernel value, which can be much cheaper to compute than the explicit mapping, especially when the feature space is very high-dimensional or even infinite-dimensional.

Designing a Kernel for XOR

Solving the XOR problem with an SVM requires selecting an appropriate kernel that can transform the input features into a space where the classes are linearly separable. Two common choices are:

1. Polynomial Kernel

A polynomial kernel of degree dd is defined as:

k(x,x)=(xx+c)dk(x, x') = (x \cdot x' + c)^d

For the XOR problem, using d=2d = 2 and a small positive cc effectively creates a feature space where the XOR data points become linearly separable. The polynomial kernel maps inputs to a space that includes interaction terms between features, which is exactly what the XOR relationship requires.

2. Radial Basis Function (RBF) Kernel

The RBF kernel maps the input into an infinite-dimensional feature space, defined as:

k(x,x)=exp(xx22σ2)k(x, x') = \exp\left(-\frac{|x - x'|^2}{2\sigma^2}\right)

The parameter σ\sigma controls the spread of the Gaussian function and must be tuned accordingly. A smaller σ\sigma makes the kernel more sensitive to individual data points (higher variance, risk of overfitting), while a larger σ\sigma produces smoother boundaries (higher bias).

Worked Example with Polynomial Kernel

Given the dataset for XOR:

x1x_1x2x_2XOR output
000
011
101
110

Using a degree-2 polynomial kernel with c=0c = 0, the implicit feature map is:

ϕ(x)=(x12, 2x1x2, x22)\phi(x) = (x_1^2,\ \sqrt{2}\, x_1 x_2,\ x_2^2)

The transformed feature set becomes:

  • (0,0)(0,0,0)(0, 0) \to (0, 0, 0)
  • (0,1)(0,0,1)(0, 1) \to (0, 0, 1)
  • (1,0)(1,0,0)(1, 0) \to (1, 0, 0)
  • (1,1)(1,2,1)(1, 1) \to (1, \sqrt{2}, 1)

In this 3D transformed space, the classes can be linearly separated by a plane. For instance, a separating hyperplane can be found along the 2x1x2\sqrt{2}\, x_1 x_2 dimension: points with the interaction term equal to zero belong to class 1, while the point with a non-zero interaction term belongs to class 0. This demonstrates why the polynomial kernel succeeds where a linear classifier fails.

Why the Linear Kernel Fails

A linear kernel computes k(x,x)=xxk(x, x') = x \cdot x', which does not create any interaction terms between features. Since the XOR function fundamentally depends on the interaction between x1x_1 and x2x_2, a linear kernel cannot produce a feature space where the classes are separable. This is why kernel selection matters.

Implementing the Kernel in SVM

In practice, modern libraries like scikit-learn handle kernel computation internally. Here is a concise example:

python
1from sklearn.svm import SVC
2import numpy as np
3
4X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
5y = np.array([0, 1, 1, 0])
6
7# Polynomial kernel, degree 2
8svm_poly = SVC(kernel='poly', degree=2, coef0=1)
9svm_poly.fit(X, y)
10print("Poly predictions:", svm_poly.predict(X))
11
12# RBF kernel
13svm_rbf = SVC(kernel='rbf', gamma=2.0)
14svm_rbf.fit(X, y)
15print("RBF predictions:", svm_rbf.predict(X))

Both kernels correctly classify all four XOR data points.

Practical Considerations

  • Kernel selection: Start with the RBF kernel as a default since it works for most non-linear problems. Use polynomial kernels when you have domain knowledge suggesting that feature interactions of a specific degree are important.
  • Hyperparameter tuning: Use cross-validation to tune σ\sigma (or γ=1/(2σ2)\gamma = 1/(2\sigma^2)) for RBF and dd and cc for polynomial kernels.
  • Scaling: Always standardize features before applying SVM, as kernel functions are sensitive to feature magnitudes.

Summary

The XOR problem demonstrates why kernel functions are essential for SVMs. A linear kernel cannot separate XOR classes because there is no linear boundary in the original 2D space. Polynomial and RBF kernels solve this by mapping data into higher-dimensional spaces where a linear separator exists. The kernel trick makes this mapping computationally efficient by computing inner products in the transformed space without explicit transformation.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design