# MATLAB Perceptron

I cant seem to figure out why I have a high percentage error.

I'm trying to get a perceptron between X1 and X2 which are Gaussian distributed data sets with distinct means and identical co-variances.

Below is my code:

N=200;
C= [2 1; 1 2]; %Covariance
m1=[0 2];
m2=[1.5 0];%mean
X1 = mvnrnd(m1, C, N/2);
X2 = mvnrnd(m2, C, N/2);

X = [X1; X2];
X = [X ones(N,1)]; %bias
y = [-1*ones(N/2,1); ones(N/2,1)]; %classification

%Split data into training and test
ii = randperm(N);
Xtr = X(ii(1:N/2),:);
ytr = X(ii(1:N/2),:);
Xts = X(ii(N/2+1:N),:);
yts = y(ii(N/2+1:N),:);
Nts = N/2;

w = randn(3,1);
eta = 0.001;
%learn from training set
for iter=1:500
j = ceil(rand*N/2);
if( ytr(j)*Xtr(j,:)*w < 0)
w = w + eta*Xtr(j,:)';
end
end

%apply what you have learnt to test set
yhts = Xts * w;
disp([yts yhts])
PercentageError = 100*sum(yts .*yhts < 0)/Nts;


What am I doing wrong and how can I address this challenge?

• I think you should explain the essence of what you are trying to do, and what you have investigated already. "What's wrong with this code" questions aren't that suitable for StackExchange. – Sean Owen Oct 29 '14 at 8:10

You are altering weights in the wrong direction for the negative cases.

The line

w = w + eta*Xtr(j,:)';


should be

w = w + eta*Xtr(j,:)'*ytr(j);


With that change I got 12% error.

I think this line is wrong:

ytr = X(ii(1:N/2),:);


ytr should be the label of the training data. In this case, it should be

ytr = y(ii(1:N/2),:);