Skip to content

Instantly share code, notes, and snippets.

@LinZap
Created March 17, 2016 07:34
Show Gist options
  • Save LinZap/3100599f4542b46665ca to your computer and use it in GitHub Desktop.
Save LinZap/3100599f4542b46665ca to your computer and use it in GitHub Desktop.
PLA
clear
close all
% Generate data
sample_size = 200;
A=repmat([2,2], sample_size,1)+randn(sample_size, 2);
B=repmat([-2,-2],sample_size,1)+randn(sample_size, 2);
X=[A; B];
Y=[ones(sample_size,1); -ones(sample_size,1)];
figure, plot(A(:,1),A(:,2),'ro'), hold on, plot(B(:,1),B(:,2),'xb');
pause;
% cycilc PLA
w=zeros(1,3);
Xext = [X ones(size(X,1), 1)];
num_mistake=1;
iter = 1;
while num_mistake > 0
disp(['iteration= ' num2str(iter)]);
% check for mistake
err = sign(w*Xext') - Y';
err_index=find(err);
for t=1:length(err_index)
w = w + Y(err_index(t))*Xext(err_index(t), :);
end;
num_mistake = length(err_index);
iter = iter +1;
end;
% plot results: 2d line
range = axis;
ezplot([num2str(w(1)) '*x+' num2str(w(2)) '*y+' num2str(w(3))], range)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment