commit 295ebd3ae684917117124e64a08764db3d57e7c7 Author: Michael Zhang Date: Wed Sep 13 18:43:30 2023 -0500 initial diff --git a/perceptron/perceptron.m b/perceptron/perceptron.m new file mode 100644 index 0000000..20a0487 --- /dev/null +++ b/perceptron/perceptron.m @@ -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 \ No newline at end of file diff --git a/perceptron/runpercep.m b/perceptron/runpercep.m new file mode 100644 index 0000000..e664020 --- /dev/null +++ b/perceptron/runpercep.m @@ -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') \ No newline at end of file diff --git a/slides/2023-09-07 Matlab Tutorial.pdf b/slides/2023-09-07 Matlab Tutorial.pdf new file mode 100644 index 0000000..e5ece5c Binary files /dev/null and b/slides/2023-09-07 Matlab Tutorial.pdf differ diff --git a/slides/2023-09-07 Matrix Calculus Tutorial.pdf b/slides/2023-09-07 Matrix Calculus Tutorial.pdf new file mode 100644 index 0000000..c61d311 Binary files /dev/null and b/slides/2023-09-07 Matrix Calculus Tutorial.pdf differ diff --git a/slides/2023-09-12 Supervised-learning.pdf b/slides/2023-09-12 Supervised-learning.pdf new file mode 100644 index 0000000..490353c Binary files /dev/null and b/slides/2023-09-12 Supervised-learning.pdf differ diff --git a/zoo/buffalo.png b/zoo/buffalo.png new file mode 100644 index 0000000..9b4a9a0 Binary files /dev/null and b/zoo/buffalo.png differ diff --git a/zoo/dolphin.png b/zoo/dolphin.png new file mode 100644 index 0000000..f28d120 Binary files /dev/null and b/zoo/dolphin.png differ diff --git a/zoo/girl.png b/zoo/girl.png new file mode 100644 index 0000000..9653221 Binary files /dev/null and b/zoo/girl.png differ diff --git a/zoo/penguin.png b/zoo/penguin.png new file mode 100644 index 0000000..183efb9 Binary files /dev/null and b/zoo/penguin.png differ diff --git a/zoo/seal.png b/zoo/seal.png new file mode 100644 index 0000000..a74e616 Binary files /dev/null and b/zoo/seal.png differ diff --git a/zoo/zombie.png b/zoo/zombie.png new file mode 100644 index 0000000..a0eaa07 Binary files /dev/null and b/zoo/zombie.png differ diff --git a/zoo/zoo.csv b/zoo/zoo.csv new file mode 100644 index 0000000..b385ffe --- /dev/null +++ b/zoo/zoo.csv @@ -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 \ No newline at end of file diff --git a/zoo/zoo.m b/zoo/zoo.m new file mode 100644 index 0000000..8b8c979 --- /dev/null +++ b/zoo/zoo.m @@ -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 \ No newline at end of file diff --git a/zoo/zoo.mat b/zoo/zoo.mat new file mode 100644 index 0000000..cae1738 Binary files /dev/null and b/zoo/zoo.mat differ