← back

Pegasos Kernel SVM Implementation

#21 · Machine Learning · Hard

⊣ Solve on deep-ml.com

Problem

Implement the Pegasos algorithm for kernel SVM classification. Pegasos is a stochastic sub-gradient descent method for solving the SVM optimization problem.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy as np

def pegasos_kernel_svm(data: list[list[float]], labels: list[int],
                        kernel: str, T: int, lambda_param: float) -> list[float]:
    X = np.array(data, dtype=float)
    y = np.array(labels, dtype=float)
    n = len(y)

    # Kernel function
    def rbf_kernel(x1, x2, gamma=0.5):
        return np.exp(-gamma * np.sum((x1 - x2) ** 2))

    def linear_kernel(x1, x2):
        return np.dot(x1, x2)

    kern = rbf_kernel if kernel == 'rbf' else linear_kernel

    # Alpha coefficients (dual form)
    alphas = np.zeros(n)

    for t in range(1, T + 1):
        eta = 1.0 / (lambda_param * t)
        i = t % n  # Cycle through examples
        # Compute prediction
        decision = sum(alphas[j] * y[j] * kern(X[j], X[i]) for j in range(n))
        if y[i] * decision < 1:
            alphas[i] += 1

    # Compute support vector weights
    sv_indices = np.where(alphas > 0)[0]
    return alphas.tolist()

Explanation

  1. Pegasos works in the dual space, maintaining alpha coefficients for each training example.
  2. At each iteration t, pick a training example and compute the margin condition.
  3. If the example is misclassified or within the margin (y_i * decision < 1), increment its alpha.
  4. The kernel function allows non-linear decision boundaries (RBF) or linear ones.
  5. The final alphas identify the support vectors and their weights.

Complexity

  • Time: O(T * n) for T iterations with n kernel evaluations each
  • Space: O(n) for the alpha coefficients