Skip to content

Commit 42b3941

Browse files
authored
Add files via upload
1 parent ce6cae2 commit 42b3941

File tree

81 files changed

+3899
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+3899
-0
lines changed

CH05/AllMethodsCatsDogs.m

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
% here's part from class
2+
% load data ...
3+
% do wavelets ..
4+
% take svd ...
5+
data = [5+ 2*randn(32*32, 80), .1*randn(32*32, 80)];
6+
[u,s,v] = svd(data,0);
7+
8+
% *** so replace above with the real cat and dog data ***
9+
10+
11+
% set up
12+
xdog=v(1:80,2:4);
13+
xcat=v(81:160,2:4);
14+
xtrain=[xdog(1:50,:);xcat(1:50,:)];
15+
xtest=[xdog(51:80,:);xcat(51:80,:)];
16+
Ctrain=[ones(50,1); 2*ones(50,1)];
17+
Ctest=[ones(30,1); 2*ones(30,1)];
18+
19+
% k-NN
20+
knn = fitcknn(xtrain, Ctrain);
21+
predKNN = predict(knn,xtest);
22+
23+
cMatKNN = confusionmat(Ctest, predKNN)
24+
errorKNN = sum(abs(predKNN-Ctest))/60*100
25+
26+
% LDA
27+
predLDA = classify(xtest, xtrain, Ctrain);
28+
29+
cMatLDA = confusionmat(Ctest, predLDA)
30+
errorLDA = sum(abs(predLDA-Ctest))/60*100
31+
32+
33+
% Naive Bayes
34+
nb=fitNaiveBayes(xtrain,Ctrain);
35+
predNB=nb.predict(xtest);
36+
37+
cMatNB=confusionmat(Ctest, predNB)
38+
errorNB=sum(abs(predNB-Ctest))/60*100
39+
40+
% SVM
41+
svm = svmtrain(xtrain,Ctrain);
42+
predSVM = svmclassify(svm,xtest);
43+
44+
cMatSVM=confusionmat(Ctest,predSVM)
45+
errorSVM=sum(abs(predSVM-Ctest))/60*100
46+
47+
% AdaBoost
48+
% pick AdaBoost, 100 classifiers, weak learners are Discriminants
49+
ab = fitensemble(xtrain,Ctrain,'AdaBoostM1',100,'Discriminant');
50+
predAB = predict(ab,xtest);
51+
52+
cMatAB = confusionmat(Ctest,predAB)
53+
errorAB = sum(abs(predAB-Ctest))/60*100
54+
55+
% EM / Gaussian mixture models
56+
gm = fitgmdist(xtrain, 2); % choose number of clusters
57+
predKM = cluster(gm,xtest); % can use clustering to classify new data
58+
59+
cMatKM = confusionmat(Ctest,predKM)
60+
errorKM = sum(abs(predKM-Ctest))/60*100
61+
62+
% some cool ways to view these clustering results are here:
63+
% http://www.mathworks.com/help/stats/gaussian-mixture-models.html#brajyl2
64+
65+
% k-means
66+
km = kmeans(xtrain, 2); % choose number of clusters
67+
% I don't think they have a built-in way to evaluate this on new data?
68+
69+
70+
71+
72+
73+

CH05/AllMethodsCatsDogs2012.m

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
clear all; close all; clc
2+
3+
% here's part from class
4+
% load data ...
5+
% do wavelets ..
6+
% take svd ...
7+
data = [5+ 2*randn(32*32, 80), .1*randn(32*32, 80)];
8+
[u,s,v] = svd(data,0);
9+
10+
% *** so replace above with the real cat and dog data ***
11+
12+
13+
% set up
14+
xdog=v(1:80,2:4);
15+
xcat=v(81:160,2:4);
16+
xtrain=[xdog(1:50,:);xcat(1:50,:)];
17+
xtest=[xdog(51:80,:);xcat(51:80,:)];
18+
Ctrain=[ones(50,1); 2*ones(50,1)];
19+
Ctest=[ones(30,1); 2*ones(30,1)];
20+
21+
% k-NN - 2014
22+
%knn = fitcknn(xtrain, Ctrain);
23+
%predKNN = predict(knn,xtest);
24+
25+
% k-NN - 2012b
26+
idx=knnsearch(xtrain,xtest,'K',1)
27+
28+
29+
30+
31+
%cMatKNN = confusionmat(Ctest, predKNN)
32+
%errorKNN = sum(abs(predKNN-Ctest))/60*100
33+
34+
% LDA
35+
%predLDA = classify(xtest, xtrain, Ctrain);
36+
37+
%cMatLDA = confusionmat(Ctest, predLDA)
38+
%errorLDA = sum(abs(predLDA-Ctest))/60*100
39+
40+
41+
% Naive Bayes - 2014
42+
%nb=fitNaiveBayes(xtrain,Ctrain);
43+
%predNB=nb.predict(xtest);
44+
45+
% 2012b
46+
nb=NaiveBayes.fit(xtrain,Ctrain);
47+
predNB=nb.predict(xtest);
48+
49+
50+
cMatNB=confusionmat(Ctest, predNB)
51+
errorNB=sum(abs(predNB-Ctest))/60*100
52+
53+
54+
% EM / Gaussian mixture models
55+
%gm = fitgmdist(xtrain, 2); % choose number of clusters
56+
gm = gmdistribution.fit(xtrain, 2); % choose number of clusters
57+
predKM = cluster(gm,xtest); % can use clustering to classify new data
58+
59+
cMatKM = confusionmat(Ctest,predKM)
60+
errorKM = sum(abs(predKM-Ctest))/60*100
61+
62+
break
63+
64+
% SVM
65+
svm = svmtrain(xtrain,Ctrain);
66+
predSVM = svmclassify(svm,xtest);
67+
68+
cMatSVM=confusionmat(Ctest,predSVM)
69+
errorSVM=sum(abs(predSVM-Ctest))/60*100
70+
71+
% AdaBoost
72+
% pick AdaBoost, 100 classifiers, weak learners are Discriminants
73+
ab = fitensemble(xtrain,Ctrain,'AdaBoostM1',100,'Discriminant');
74+
predAB = predict(ab,xtest);
75+
76+
cMatAB = confusionmat(Ctest,predAB)
77+
errorAB = sum(abs(predAB-Ctest))/60*100
78+
79+
80+
% some cool ways to view these clustering results are here:
81+
% http://www.mathworks.com/help/stats/gaussian-mixture-models.html#brajyl2
82+
83+
% k-means
84+
km = kmeans(xtrain, 2); % choose number of clusters
85+
% I don't think they have a built-in way to evaluate this on new data?
86+
87+
88+
89+
90+
91+
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
clear all; close all; clc
2+
3+
load fisheriris;
4+
x1=meas(1:50,:); % setosa
5+
x2=meas(51:100,:); % versicolor
6+
x3=meas(101:150,:); % virginica
7+
8+
plot3(x1(:,1),x1(:,2),x1(:,4),'go'), hold on
9+
plot3(x2(:,1),x2(:,2),x2(:,4),'mo')
10+
plot3(x3(:,1),x3(:,2),x3(:,4),'ro')
11+
grid on, set(gca,'Fontsize',[15])
12+
legend('','','','Location','NorthWest'),legend boxoff
13+
14+
15+
16+
17+
18+
19+
20+
21+
load dogData.mat
22+
load catData.mat
23+
CD=double([dog cat]);
24+
[u,s,v]=svd(CD-mean(CD(:)),'econ');
25+
26+
figure(3)
27+
for j=1:4
28+
subplot(2,2,j)
29+
U=flipud(reshape(u(:,j),64,64));
30+
U2=U(1:2:64,1:2:64);
31+
pcolor(U2), colormap(hot), axis off
32+
end
33+
34+
figure(4)
35+
for j=1:4
36+
subplot(4,1,j), bar(v(:,j),'FaceColor',[.6 .6 .6],'EdgeColor','k')
37+
set(gca,'Fontsize',[15]), axis([0 160 -0.2 0.2])
38+
end
39+
40+
figure(5)
41+
xbin=linspace(-0.25,0.25,20);
42+
for j=1:4
43+
subplot(4,2,2*j-1)
44+
pdf1=hist(v(1:80,j),xbin)
45+
pdf2=hist(v(81:160,j),xbin)
46+
plot(xbin,pdf1,xbin,pdf2,'Linewidth',[2])
47+
axis([-.2 0.2 0 20]), set(gca,'Fontsize',[15])
48+
end
49+
50+
figure(2)
51+
subplot(2,2,1)
52+
plot3(v(1:80,1),v(1:80,2),v(1:80,3),'ro','Linewidth',[1],'MarkerEdgeColor','k',...
53+
'MarkerFaceColor',[0 1 0.2],...
54+
'MarkerSize',8), hold on % [0.49 1 .63]
55+
plot3(v(81:end,1),v(81:end,2),v(81:end,3),'bo','Linewidth',[1],'MarkerEdgeColor','k',...
56+
'MarkerFaceColor',[0.9 0 1],...
57+
'MarkerSize',8)
58+
view(-138,64), grid on, set(gca,'Fontsize',[15])
59+
axis([-0.2 0.2 -0.2 0.2 -0.5 0.5])
60+
61+
% figure(2), subplot(2,2,3)
62+
% plot(diag(s)/sum(diag(s)),'ko');
63+
% axis([0 100 0 0.06])
64+
% set(gca,'Fontsize',[15])
65+
66+
67+
%dog_wave=dc_wavelet(dog);
68+
%cat_wave=dc_wavelet(cat);
69+
70+
%%
71+
load catData_w.mat
72+
load dogData_w.mat
73+
CD2=[dog_wave cat_wave];
74+
[u2,s2,v2]=svd(CD2-mean(CD2(:)),'econ');
75+
76+
figure(6)
77+
for j=1:4
78+
subplot(2,2,j)
79+
Xd=flipud(reshape(dog_wave(:,j),32,32));
80+
pcolor(Xd), colormap(hot), axis off
81+
end
82+
83+
84+
figure(7)
85+
for j=1:4
86+
subplot(2,2,j)
87+
U3=flipud(reshape(u2(:,j),32,32));
88+
pcolor(U3), colormap(hot), axis off
89+
end
90+
91+
figure(8)
92+
for j=1:4
93+
subplot(4,1,j), bar(v2(:,j),'FaceColor',[.6 .6 .6],'EdgeColor','k')
94+
set(gca,'Fontsize',[15]), axis([0 160 -0.2 0.2])
95+
end
96+
97+
figure(5)
98+
for j=1:4
99+
subplot(4,2,2*j)
100+
pdf1=hist(v2(1:80,j),xbin);
101+
pdf2=hist(v2(81:160,j),xbin);
102+
plot(xbin,pdf1,xbin,pdf2,'Linewidth',[2])
103+
axis([-.2 0.2 0 20]), set(gca,'Fontsize',[15])
104+
end
105+
106+
figure(2)
107+
subplot(2,2,2)
108+
plot3(v2(1:80,1),v2(1:80,2),v2(1:80,3),'ro','Linewidth',[1],'MarkerEdgeColor','k',...
109+
'MarkerFaceColor',[0 1 0.2],...
110+
'MarkerSize',8), hold on % [0.49 1 .63]
111+
plot3(v2(81:end,1),v2(81:end,2),v2(81:end,3),'bo','Linewidth',[1],'MarkerEdgeColor','k',...
112+
'MarkerFaceColor',[0.9 0 1],...
113+
'MarkerSize',8)
114+
view(-138,64), grid on, set(gca,'Fontsize',[15])
115+
axis([-0.2 0.2 -0.2 0.2 -0.5 0.5])
116+
117+
% figure(2), subplot(2,2,4)
118+
% plot(diag(s2)/sum(diag(s2)),'ko');
119+
% axis([0 100 0 0.025])
120+
% set(gca,'Fontsize',[15])
121+
122+
%%
123+
figure(11)
124+
125+
master=zeros(32*5,32*4); count=1;
126+
for jj=1:4
127+
for j=1:5
128+
T2=flipud(reshape(dog(:,count),64,64));
129+
T=T2(1:2:64,1:2:64);
130+
master(1+32*(j-1):32+32*(j-1),1+32*(jj-1):32+32*(jj-1))=T;
131+
count=count+1;
132+
end
133+
end
134+
pcolor(master), shading interp, colormap(gray), axis off
135+
136+
figure(12)
137+
138+
master=zeros(32*5,32*4); count=1;
139+
for jj=1:4
140+
for j=1:5
141+
T2=flipud(reshape(cat(:,count),64,64));
142+
T=T2(1:2:64,1:2:64);
143+
master(1+32*(j-1):32+32*(j-1),1+32*(jj-1):32+32*(jj-1))=T;
144+
count=count+1;
145+
end
146+
end
147+
pcolor(master), shading interp, colormap(gray), axis off
148+

0 commit comments

Comments
 (0)