machine learning - Matlab implementation of Perceptron - can't seem to fix plotting -
this first go ml (and matlab) , i'm following "learning data" yaser s. abu-mostafa.
i'm trying implement perceptron algorithm, after trying go through pseudocode, using other people's solutions can't seem fix problem (i went through other threads too).
the algorithm separates data fine, works. however, want plot single line, seems separates them in way '-1' cluster divided second cluster or more.
this code:
iterations = 100; dim = 3; x1=[rand(1,dim);rand(1,dim);ones(1,dim)]; % class '+1' x2=[rand(1,dim);1+rand(1,dim);ones(1,dim)]; % class '-1' x=[x1,x2]; y=[-ones(1,dim),ones(1,dim)]; w=[0,0,0]'; % call perceptron wtag=weight(x,y,w,iterations); % predict ytag=wtag'*x; % plot prediction on origianl data figure;hold on plot(x1(1,:),x1(2,:),'b.') plot(x2(1,:),x2(2,:),'r.') plot(x(1,ytag<0),x(2,ytag<0),'bo') plot(x(1,ytag>0),x(2,ytag>0),'ro') legend('class -1','class +1','pred -1','pred +1') %why don't 1 line? plot(x,y); the weight function (perceptron):
function [w] = weight(x,y,w_init,iterations) %weight summary of function goes here % detailed explanation goes here w = w_init; iteration = 1 : iterations %<- 100! ii = 1 : size(x,2) %cycle through training set if sign(w'*x(:,ii)) ~= y(ii) %wrong decision? w = w + x(:,ii) * y(ii); %then add (or subtract) point w end end sum(sign(w'*x)~=y)/size(x,2); %show misclassification rate end i don't think problem in second function added regardless
i'm pretty sure algorithm separates more 1 cluster can't tell why of learning i've done far math , theory , not actual coding i'm missing obvious..
Comments
Post a Comment