34 lines
786 B
Mathematica
34 lines
786 B
Mathematica
|
% CSCI 5521 Introduction to Machine Learning
|
||
|
% Rui Kuang
|
||
|
% Run perceptron on random data points in two classes
|
||
|
|
||
|
n = 20; %set the number of data points
|
||
|
mydata = rand(n,2);
|
||
|
|
||
|
shiftidx = abs(mydata(:,1)-mydata(:,2))>0.05;
|
||
|
mydata = mydata(shiftidx,:);
|
||
|
myclasses = mydata(:,1)>mydata(:,2); % labels
|
||
|
n = size(mydata,1);
|
||
|
X = [mydata ones(1,n)']'; Y=myclasses;
|
||
|
Y = Y * 2 -1;
|
||
|
|
||
|
% init weigth vector
|
||
|
w = [mean(mydata) 0]';
|
||
|
|
||
|
for i = 1:1
|
||
|
w=rand(1,3)';
|
||
|
w(3,1)=0;%go through the origin for visualization
|
||
|
% call perceptron
|
||
|
wtag=perceptron(X,Y,w,10);
|
||
|
end
|
||
|
|
||
|
% call perceptron
|
||
|
% wtag=perceptron(X,Y,w);
|
||
|
% predict
|
||
|
ytag=wtag'*X;
|
||
|
|
||
|
% plot prediction over origianl data
|
||
|
|
||
|
%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')
|