感知器的对偶形式的程序实现,算法见李航老师的《统计学习方法》
clear;clc;close all
% define three points
x1 = [3, 3]';
x2 = [4, 3]';
x3 = [1, 1]';
x = [x1, x2, x3];
% x1 and x2 is positive example, x3 is negative example
y1 = 1;
y2 = 1;
y3 = -1;
y = [y1; y2; y3];
% dual form
n = numel(y);
alpha = zeros(n, 1);
beta = 0;
t = 1;
ent = 1;
is_right_class = false;
while ~is_right_class
is_right_class = true;
for i = 1 : n
if (x(:,i)' * x .* y. * alpha(:, t) + beta(t)) * y(i) <= 0
alpha(:, t + 1) = alpha(:, t);
alpha(i, t + 1) = alpha(i, t) + ent;
beta(t + 1) = beta(t) + ent * y(i);
t = t + 1;
is_right_class = false;
end
end
end
w = y' .* x * alpha(:, t);
b = beta(t);