Pegasos kernel SVM
Background
The Support Vector Machine finds the maximum-margin decision boundary between two classes. Pegasos ("Primal Estimated sub-GrAdient SOlver") trains it by stochastic sub-gradient descent on the hinge-loss + L2 objective, and the kernelised version replaces dot products with a kernel so the boundary can be non-linear (e.g. an RBF kernel). The trained model is stored as one coefficient per training point plus a bias .
Problem statement
Implement pegasos_kernel_svm(data, labels, kernel, lambda_val, iterations, sigma) that trains a kernel SVM with the Pegasos update and returns . Initialise , . For each step and each sample , with rate , compute the decision value and update only when the point is inside the margin:
Kernels: 'linear' with , and 'rbf' with .
Input
data—np.ndarrayof shape(n_samples, n_features).labels—np.ndarrayof shape(n_samples,): labels in .kernel—str:'linear'or'rbf'.lambda_val—float: regularisation strength .iterations—int: number of passes .sigma—float: RBF bandwidth (ignored for the linear kernel).
Output
Returns a tuple (alphas, b): alphas is a list[float] of length n_samples (rounded to 4 dp) and b is a float (rounded to 4 dp).
Examples
Example 1
Input: data = [[1,2],[2,3],[3,1],[4,1]], labels = [1,1,-1,-1]
kernel = 'rbf', lambda_val = 0.01, iterations = 100, sigma = 1.0
Output: alphas = [100.0, 99.0, -100.0, -100.0], b = -150.0
Explanation: under the RBF kernel each pass nudges the coefficients of the samples that violate the margin (); after 100 passes the and define a non-linear boundary separating the two classes.
Constraints
- The learning rate decays as with starting at .
- Update and only when .
- Compute from the current and (full kernel sum over all samples).
- Round
alphasandbto 4 decimals.
Notes
- This is the kernelised Pegasos: rather than a weight vector , the model keeps one coefficient per training point, so it works even in the infinite-dimensional feature space of the RBF kernel.
- The shrinkage term in the update is what maximises the margin instead of merely fitting the data.
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reproduces the reference RBF run
- •Returns one alpha per sample and a scalar bias
- •Linear kernel separates a linearly separable set
- •Deterministic: identical inputs give identical models