This commit is contained in:
Michael Zhang 2023-09-13 18:43:30 -05:00
commit 295ebd3ae6
14 changed files with 97 additions and 0 deletions

42
perceptron/perceptron.m Normal file
View file

@ -0,0 +1,42 @@
% 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,:));
figure;
ginput(1);
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);
%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]);
%ginput(1);
pause(0.5); %change the delay
end
end
round = round + 1
err = sum(sign(w'*X)~=Y')/N %show misclassification rate
end

34
perceptron/runpercep.m Normal file
View file

@ -0,0 +1,34 @@
% 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')

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
zoo/buffalo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

BIN
zoo/dolphin.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

BIN
zoo/girl.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 KiB

BIN
zoo/penguin.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

BIN
zoo/seal.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 KiB

BIN
zoo/zombie.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB

1
zoo/zoo.csv Normal file
View file

@ -0,0 +1 @@
animal,hair,feathers,eggs,milk,airborne,aquatic,predator,toothed,backbone,breathes,venomous,fins,legs,tail,domestic,catsize,type aardvark,1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,mammal antelope,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,mammal bass,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,fish bear,1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,mammal boar,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal calf,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,mammal carp,0,0,1,0,0,1,0,1,1,0,0,1,0,1,1,0,fish catfish,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,fish cavy,1,0,0,1,0,0,0,1,1,1,0,0,4,0,1,0,mammal cheetah,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal chicken,0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,bird chub,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,fish clam,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,invertebrate crab,0,0,1,0,0,1,1,0,0,0,0,0,4,0,0,0,invertebrate crayfish,0,0,1,0,0,1,1,0,0,0,0,0,6,0,0,0,invertebrate crow,0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,0,bird deer,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,mammal dogfish,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,fish dove,0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,bird duck,0,1,1,0,1,1,0,0,1,1,0,0,2,1,0,0,bird elephant,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,mammal flamingo,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,1,bird flea,0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,insect frog,0,0,1,0,0,1,1,1,1,1,0,0,4,0,0,0,amphibian frog,0,0,1,0,0,1,1,1,1,1,1,0,4,0,0,0,amphibian fruitbat,1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,mammal giraffe,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,mammal gnat,0,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,insect goat,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,mammal gorilla,1,0,0,1,0,0,0,1,1,1,0,0,2,0,0,1,mammal gull,0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,bird haddock,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,fish hamster,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,0,mammal hare,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,mammal hawk,0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,0,bird herring,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,fish honeybee,1,0,1,0,1,0,0,0,0,1,1,0,6,0,1,0,insect housefly,1,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,insect kiwi,0,1,1,0,0,0,1,0,1,1,0,0,2,1,0,0,bird ladybird,0,0,1,0,1,0,1,0,0,1,0,0,6,0,0,0,insect lark,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,bird leopard,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal lion,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal lobster,0,0,1,0,0,1,1,0,0,0,0,0,6,0,0,0,invertebrate lynx,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal mink,1,0,0,1,0,1,1,1,1,1,0,0,4,1,0,1,mammal mole,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,0,mammal mongoose,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal moth,1,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,insect newt,0,0,1,0,0,1,1,1,1,1,0,0,4,1,0,0,amphibian octopus,0,0,1,0,0,1,1,0,0,0,0,0,8,0,0,1,invertebrate opossum,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,0,mammal oryx,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,mammal ostrich,0,1,1,0,0,0,0,0,1,1,0,0,2,1,0,1,bird parakeet,0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,bird pheasant,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,bird pike,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,fish piranha,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,fish pitviper,0,0,1,0,0,0,1,1,1,1,1,0,0,1,0,0,reptile platypus,1,0,1,1,0,1,1,0,1,1,0,0,4,1,0,1,mammal polecat,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal pony,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,mammal porpoise,0,0,0,1,0,1,1,1,1,1,0,1,0,1,0,1,mammal puma,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal pussycat,1,0,0,1,0,0,1,1,1,1,0,0,4,1,1,1,mammal raccoon,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal reindeer,1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,mammal rhea,0,1,1,0,0,0,1,0,1,1,0,0,2,1,0,1,bird scorpion,0,0,0,0,0,0,1,0,0,1,1,0,8,1,0,0,invertebrate seahorse,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,fish sealion,1,0,0,1,0,1,1,1,1,1,0,1,2,1,0,1,mammal seasnake,0,0,0,0,0,1,1,1,1,0,1,0,0,1,0,0,reptile seawasp,0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,invertebrate skimmer,0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,bird skua,0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,bird slowworm,0,0,1,0,0,0,1,1,1,1,0,0,0,1,0,0,reptile slug,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,invertebrate sole,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,fish sparrow,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,bird squirrel,1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,0,mammal starfish,0,0,1,0,0,1,1,0,0,0,0,0,5,0,0,0,invertebrate stingray,0,0,1,0,0,1,1,1,1,0,1,1,0,1,0,1,fish swan,0,1,1,0,1,1,0,0,1,1,0,0,2,1,0,1,bird termite,0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,insect toad,0,0,1,0,0,1,0,1,1,1,0,0,4,0,0,0,amphibian tortoise,0,0,1,0,0,0,0,0,1,1,0,0,4,1,0,1,reptile tuatara,0,0,1,0,0,0,1,1,1,1,0,0,4,1,0,0,reptile tuna,0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,fish vampire,1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,mammal vole,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,mammal vulture,0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,1,bird wallaby,1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,1,mammal wasp,1,0,1,0,1,0,0,0,0,1,1,0,6,0,0,0,insect wolf,1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,mammal worm,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,invertebrate wren,0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,bird buffalo,1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,mammal dolphin,0,0,0,1,0,1,1,1,1,1,0,1,0,1,0,1,mammal zombie,1,0,0,0,0,0,1,1,1,1,0,0,2,0,0,0,reptile penguin,0,1,1,0,0,1,1,0,1,1,0,0,2,1,0,1,bird seal,1,0,0,1,0,1,1,1,1,1,0,1,0,0,0,1,mammal girl,1,0,0,1,0,0,1,1,1,1,0,0,2,0,1,1,mammal
1 animal hair feathers eggs milk airborne aquatic predator toothed backbone breathes venomous fins legs tail domestic catsize type aardvark 1 0 0 1 0 0 1 1 1 1 0 0 4 0 0 1 mammal antelope 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 1 mammal bass 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 fish bear 1 0 0 1 0 0 1 1 1 1 0 0 4 0 0 1 mammal boar 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal calf 1 0 0 1 0 0 0 1 1 1 0 0 4 1 1 1 mammal carp 0 0 1 0 0 1 0 1 1 0 0 1 0 1 1 0 fish catfish 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 fish cavy 1 0 0 1 0 0 0 1 1 1 0 0 4 0 1 0 mammal cheetah 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal chicken 0 1 1 0 1 0 0 0 1 1 0 0 2 1 1 0 bird chub 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 fish clam 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 invertebrate crab 0 0 1 0 0 1 1 0 0 0 0 0 4 0 0 0 invertebrate crayfish 0 0 1 0 0 1 1 0 0 0 0 0 6 0 0 0 invertebrate crow 0 1 1 0 1 0 1 0 1 1 0 0 2 1 0 0 bird deer 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 1 mammal dogfish 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 1 fish dove 0 1 1 0 1 0 0 0 1 1 0 0 2 1 1 0 bird duck 0 1 1 0 1 1 0 0 1 1 0 0 2 1 0 0 bird elephant 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 1 mammal flamingo 0 1 1 0 1 0 0 0 1 1 0 0 2 1 0 1 bird flea 0 0 1 0 0 0 0 0 0 1 0 0 6 0 0 0 insect frog 0 0 1 0 0 1 1 1 1 1 0 0 4 0 0 0 amphibian frog 0 0 1 0 0 1 1 1 1 1 1 0 4 0 0 0 amphibian fruitbat 1 0 0 1 1 0 0 1 1 1 0 0 2 1 0 0 mammal giraffe 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 1 mammal gnat 0 0 1 0 1 0 0 0 0 1 0 0 6 0 0 0 insect goat 1 0 0 1 0 0 0 1 1 1 0 0 4 1 1 1 mammal gorilla 1 0 0 1 0 0 0 1 1 1 0 0 2 0 0 1 mammal gull 0 1 1 0 1 1 1 0 1 1 0 0 2 1 0 0 bird haddock 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 0 fish hamster 1 0 0 1 0 0 0 1 1 1 0 0 4 1 1 0 mammal hare 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 0 mammal hawk 0 1 1 0 1 0 1 0 1 1 0 0 2 1 0 0 bird herring 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 fish honeybee 1 0 1 0 1 0 0 0 0 1 1 0 6 0 1 0 insect housefly 1 0 1 0 1 0 0 0 0 1 0 0 6 0 0 0 insect kiwi 0 1 1 0 0 0 1 0 1 1 0 0 2 1 0 0 bird ladybird 0 0 1 0 1 0 1 0 0 1 0 0 6 0 0 0 insect lark 0 1 1 0 1 0 0 0 1 1 0 0 2 1 0 0 bird leopard 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal lion 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal lobster 0 0 1 0 0 1 1 0 0 0 0 0 6 0 0 0 invertebrate lynx 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal mink 1 0 0 1 0 1 1 1 1 1 0 0 4 1 0 1 mammal mole 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 0 mammal mongoose 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal moth 1 0 1 0 1 0 0 0 0 1 0 0 6 0 0 0 insect newt 0 0 1 0 0 1 1 1 1 1 0 0 4 1 0 0 amphibian octopus 0 0 1 0 0 1 1 0 0 0 0 0 8 0 0 1 invertebrate opossum 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 0 mammal oryx 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 1 mammal ostrich 0 1 1 0 0 0 0 0 1 1 0 0 2 1 0 1 bird parakeet 0 1 1 0 1 0 0 0 1 1 0 0 2 1 1 0 bird pheasant 0 1 1 0 1 0 0 0 1 1 0 0 2 1 0 0 bird pike 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 1 fish piranha 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 fish pitviper 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 0 reptile platypus 1 0 1 1 0 1 1 0 1 1 0 0 4 1 0 1 mammal polecat 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal pony 1 0 0 1 0 0 0 1 1 1 0 0 4 1 1 1 mammal porpoise 0 0 0 1 0 1 1 1 1 1 0 1 0 1 0 1 mammal puma 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal pussycat 1 0 0 1 0 0 1 1 1 1 0 0 4 1 1 1 mammal raccoon 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal reindeer 1 0 0 1 0 0 0 1 1 1 0 0 4 1 1 1 mammal rhea 0 1 1 0 0 0 1 0 1 1 0 0 2 1 0 1 bird scorpion 0 0 0 0 0 0 1 0 0 1 1 0 8 1 0 0 invertebrate seahorse 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 0 fish sealion 1 0 0 1 0 1 1 1 1 1 0 1 2 1 0 1 mammal seasnake 0 0 0 0 0 1 1 1 1 0 1 0 0 1 0 0 reptile seawasp 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 0 invertebrate skimmer 0 1 1 0 1 1 1 0 1 1 0 0 2 1 0 0 bird skua 0 1 1 0 1 1 1 0 1 1 0 0 2 1 0 0 bird slowworm 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 0 reptile slug 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 invertebrate sole 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 0 fish sparrow 0 1 1 0 1 0 0 0 1 1 0 0 2 1 0 0 bird squirrel 1 0 0 1 0 0 0 1 1 1 0 0 2 1 0 0 mammal starfish 0 0 1 0 0 1 1 0 0 0 0 0 5 0 0 0 invertebrate stingray 0 0 1 0 0 1 1 1 1 0 1 1 0 1 0 1 fish swan 0 1 1 0 1 1 0 0 1 1 0 0 2 1 0 1 bird termite 0 0 1 0 0 0 0 0 0 1 0 0 6 0 0 0 insect toad 0 0 1 0 0 1 0 1 1 1 0 0 4 0 0 0 amphibian tortoise 0 0 1 0 0 0 0 0 1 1 0 0 4 1 0 1 reptile tuatara 0 0 1 0 0 0 1 1 1 1 0 0 4 1 0 0 reptile tuna 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 1 fish vampire 1 0 0 1 1 0 0 1 1 1 0 0 2 1 0 0 mammal vole 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 0 mammal vulture 0 1 1 0 1 0 1 0 1 1 0 0 2 1 0 1 bird wallaby 1 0 0 1 0 0 0 1 1 1 0 0 2 1 0 1 mammal wasp 1 0 1 0 1 0 0 0 0 1 1 0 6 0 0 0 insect wolf 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 mammal worm 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 invertebrate wren 0 1 1 0 1 0 0 0 1 1 0 0 2 1 0 0 bird buffalo 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 1 mammal dolphin 0 0 0 1 0 1 1 1 1 1 0 1 0 1 0 1 mammal zombie 1 0 0 0 0 0 1 1 1 1 0 0 2 0 0 0 reptile penguin 0 1 1 0 0 1 1 0 1 1 0 0 2 1 0 1 bird seal 1 0 0 1 0 1 1 1 1 1 0 1 0 0 0 1 mammal girl 1 0 0 1 0 0 1 1 1 1 0 0 2 0 1 1 mammal

20
zoo/zoo.m Normal file
View file

@ -0,0 +1,20 @@
load zoo; %load matlab data
t = fitctree(trn_data,trn_lab,'PredictorNames', fields); %train a decision tree for classification
view(t,'Mode','graph'); %visualize the tree t
%for visualization
allHandles=findall(groot,'Type','text')
set(allHandles,'FontSize',24)
pause;
h = findall(groot,'Type','figure');
close(h);
for i=1:6
clf(gcf);
imshow(strcat(tst_names{i},'.png'));
set(gcf,'Position',[0 0 700 700]+300);
title(tst_names{i},'FontSize',40);
pause;
y=predict(t,tst_data(i,:)); %classify a test sample by tree t
title(sprintf('%s is %s',tst_names{i},y{1}),'FontSize',40,'color', 'r');
pause;
end

BIN
zoo/zoo.mat Normal file

Binary file not shown.