csci5521/perceptron/perceptron.m

46 lines
1.1 KiB
Mathematica
Raw Normal View History

2023-09-13 23:43:30 +00:00
% CSCI 5521 Introduction to Machine Learning
% Rui Kuang
% Perceptron function
function [w] = perceptron(X,Y,w_init,rate)
w = w_init;
N = size(X,2);
pos_idx = (Y==1);
neg_idx = (Y==-1);
mxx = max(X(1,:));
mnx=-0.1*mxx;%for visualization %mnx = min(X(1,:));
mxy = max(X(2,:));
mny=-0.1*mxy; % for visualization %mny = min(X(2,:));
2023-09-22 03:45:23 +00:00
%%% figure;
%%% ginput(1);
2023-09-13 23:43:30 +00:00
err = 1;
round = 0;
while err > 0
for ii = 1 : N %cycle through training set
if sign(w'*X(:,ii)) ~= Y(ii) %wrong decision?
w = w + rate*X(:,ii) * Y(ii); %then add (or subtract) this point to w
x1=mnx:0.01:mxx;
x2=-(w(1)*x1+w(3))/w(2);
2023-09-22 03:45:23 +00:00
%%% figure;
%%% clf;
%%% hold on
%%% plot(X(1,pos_idx),X(2,pos_idx),'b*','MarkerSize',10);
%%% plot(X(1,neg_idx),X(2,neg_idx),'r+','MarkerSize',10);
%%% plot(X(1,ii),X(2,ii),'ko','MarkerSize',15);
%%% plot(x1,x2);
%%% xlim([mnx mxx]);
%%% ylim([mny mxy]);
2023-09-13 23:43:30 +00:00
%ginput(1);
2023-09-22 03:45:23 +00:00
% pause(0.5); %change the delay
2023-10-01 23:09:50 +00:00
rate = rate * 0.9;
2023-09-13 23:43:30 +00:00
end
end
2023-10-01 23:09:50 +00:00
round = round + 1;
err = sum(sign(w'*X)~=Y')/N; %show misclassification rate
end
round