Write a Python function that implements a deterministic version of the Pegasos algorithm to train a kernel SVM classifier from scratch. The function should take a dataset (as a 2D NumPy array where each row represents a data sample and each column represents a feature), a label vector (1D NumPy array where each entry corresponds to the label of the sample), and training parameters such as the choice of kernel (linear or RBF), regularization parameter (lambda), and the number of iterations. Note that while the original Pegasos algorithm is stochastic (it selects a single random sample at each step), this problem requires using all samples in every iteration (i.e., no random sampling). The function should perform binary classification and return the model's alpha coefficients as a list and bias as a float.
Examples
data = np.array([[1, 2], [2, 3], [3, 1], [4, 1]]), labels = np.array([1, 1, -1, -1]), kernel = 'linear', lambda_val = 0.01, iterations = 100([100.0, 0.0, -100.0, -100.0], -937.4755)Starter Code
import numpy as np
def pegasos_kernel_svm(data: np.ndarray, labels: np.ndarray, kernel='linear', lambda_val=0.01, iterations=100, sigma=1.0) -> tuple:
"""
Train a kernel SVM using the deterministic Pegasos algorithm.
Args:
data: Training data of shape (n_samples, n_features)
labels: Labels of shape (n_samples,) with values in {-1, 1}
kernel: 'linear' or 'rbf'
lambda_val: Regularization parameter
iterations: Number of training iterations
sigma: RBF kernel bandwidth (only used if kernel='rbf')
Returns:
Tuple of (alphas, bias) where alphas is a list and bias is a float
"""
# Your code here
pass