输入:训练样本集 { (x_i, y_i) | i = 1, 2, ..., l }
参数:惩罚因子 C,容忍度 ε,最大迭代次数 max_iter
初始化:
1. α_i = 0, ∀ i
2. b = 0
3. 训练样本的预测值 f(x) = 0
定义:计算预测值
function f(x):
return Σ(α_i * y_i * K(x_i, x)) + b
定义:选择第一个拉格朗日乘子 α_1
function select_alpha1():
return random index from 1 to l
定义:选择第二个拉格朗日乘子 α_2
function select_alpha2(i):
select random index j ≠ i
定义:更新α值和b
function update_alpha_and_b(i, j):
if y_i ≠ y_j:
L = max(0, α_j - α_i)
H = min(C, C + α_j - α_i)
else:
L = max(0, α_i + α_j - C)
H = min(C, α_i + α_j)
if L == H:
return 0
η = K(x_i, x_i) + K(x_j, x_j) - 2 * K(x_i, x_j)
if η <= 0:
return 0
α_j_new = α_j + y_j * (E_i - E_j) / η
α_j_new = clip(α_j_new, L, H)
if α_j_new == α_j:
return 0
α_i_new = α_i + y_i * y_j * (α_j - α_j_new)
b1 = b - E_i - y_i * (α_i_new - α_i) * K(x_i, x_i) - y_j * (α_j_new - α_j) * K(x_i, x_j)
b2 = b - E_j - y_i * (α_i_new - α_i) * K(x_i, x_j) - y_j * (α_j_new - α_j) * K(x_j, x_j)
if 0 < α_i_new < C:
b = b1
else if 0 < α_j_new < C:
b = b2
else:
b = (b1 + b2) / 2
return 1
主函数:
for iter in 1 to max_iter:
num_changed_alphas = 0
for i from 1 to l:
E_i = f(x_i) - y_i
if (y_i * E_i < -ε and α_i < C) or (y_i * E_i > ε and α_i > 0):
j = select_alpha2(i)
num_changed_alphas += update_alpha_and_b(i, j)
if num_changed_alphas == 0:
break
返回 (α, b)