Implement the Pegasos algorithm for kernel SVM classification. Pegasos is a stochastic sub-gradient descent method for solving the SVM optimization problem.
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()