/
testAccuracy.asv
157 lines (137 loc) · 4.65 KB
/
testAccuracy.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
clear all; clc;
load('dataset/processedDataset_raw.mat');
%% Create gaussian EM
Left = GaussianEM(20,size(train_setl,2),'Left');
Right = GaussianEM(20,size(train_setr,2),'Right');
Left.train(train_setl,3,1,0,2,4);
Right.train(train_setr,3,1,0,2,4);
%
conf = zeros(2);
setl = test_same_setl;
setr = test_same_setr;
for i=1:size(setl,1)
datapoint = setl(i,:);
lpr = (datapoint-mul)/coeffl';
rpr = (datapoint-mur)/coeffr';
lp = Left.getLikelihood(lpr); rp = Right.getLikelihood(rpr);
choice = datasample(find([lp,rp] == max([lp,rp])),1,'Replace',false);
conf(1,choice) = conf(1,choice)+1;
end
for i=1:size(setr,1)
datapoint = setr(i,:);
lpr = (datapoint-mul)/coeffl';
rpr = (datapoint-mur)/coeffr';
lp = Left.getLikelihood(lpr); rp = Right.getLikelihood(rpr);
[lp,rp];
choice = datasample(find([lp,rp] == max([lp,rp])),1,'Replace',false);
conf(2,choice) = conf(2,choice)+1;
end
conf
%% Create multiple gaussians
ust = unique(train_users);
ntr = size(ust,1);
gaussians = cell(ntr,2);
K = 1; maxIter = 3;
for user = 1:ntr
user
IND = find(train_users==ust(user));
user_set = train_set(IND,:); labels = labelstr(IND);
left = user_set(labels==1,:); right = user_set(labels==2,:);
L = GaussianEM(K,size(train_set,2),'Left');
R = GaussianEM(K,size(train_set,2),'Right');
% L = Gaussian(size(train_set,2),'Left');
% R = Gaussian(size(train_set,2),'Right');
L.train(left,maxIter,0,0,2,4);
R.train(right,maxIter,0,0,2,4);
gaussians{user,1} = L; gaussians{user,2} = R;
end
%% Test on multiple gaussians
conf = zeros(2);
set = test_same_set;
labels = labelstes;
for j=1:size(labels,1)
datapoint = test_same_set(j,:);
l = -Inf; r = -Inf;
for i=1:ntr
ll = gaussians{i,1}.getLikelihood(datapoint)+eps; l = l+ll;
lr = gaussians{i,2}.getLikelihood(datapoint)+eps; r = r+lr;
end
[ll,lr]
like = find([ll,lr]==max([ll,lr]));
[num2str(j),' should be: ',num2str(labels(j)),' and is ',num2str(like)]
conf(labels(j),like) = conf(labels(j),like)+1;
end
conf
%% Create Gaussian clustering
L = train_set(labelstr==1,:);
R = train_set(labelstr==2,:);
SET = train_set;
LABELS = labelstr;
SET2 = test_same_set;
LABELS2 = labelstes;
Iter = 5:5:10;
Clusters = 5:2:20;
table = zeros(size(Iter,2),size(Clusters,2));
self = zeros(size(Iter,2),size(Clusters,2));
for K = 1:size(Clusters,2)
for MaxIter = 1:size(Iter,2);
[K,MaxIter]
Left = GaussianEM(Clusters(K),size(train_set,2),'Left');
Right = GaussianEM(Clusters(K),size(train_set,2),'Right');
% Baseline = GaussianEM(K,size(train_set,2),'Nothing');
%
Left.train(L,Iter(MaxIter),0,0,2,4);
Right.train(R,Iter(MaxIter),0,0,2,4);
% Baseline.train(train_set(labelstr==3,:),MaxIter,1,1,1,10000);
S = size(SET,1);
conf = zeros(2);
predictions = zeros(S,1);
for i=1:S
probability = [max(Left.getLikelihood(SET(i,:))),max(Right.getLikelihood(SET(i,:)))];
predictions(i) = datasample(find(probability == max(probability)),1,'Replace',false);
conf(LABELS(i),predictions(i)) = conf(LABELS(i),predictions(i)) + 1;
end
correct = sum(predictions==LABELS)/S;
self(K,MaxIter) = correct;
% Test on train set EM GAUSSIAN
S = size(SET2,1);
conf = zeros(2);
predictions = zeros(S,1);
for i=1:S
probability = [max(Left.getLikelihood(SET2(i,:))),max(Right.getLikelihood(SET2(i,:)))];
predictions(i) = datasample(find(probability == max(probability)),1,'Replace',false);
conf(LABELS2(i),predictions(i)) = conf(LABELS2(i),predictions(i)) + 1;
end
conf
correct = sum(predictions==LABELS2)/S
table(MaxIter,K) = correct;
end
end
%% Create clustering classifiers and train with KMEANS
IND = randperm(size(train_set,1)); %randomize training data
train_set = train_set(IND,:);
labelstr = labelstr(IND);
K = 10;
Left = KMeans(K,size(train_set,2));
Right = KMeans(K,size(train_set,2));
for i=1:size(train_set,1)
i
switch labelstr(i)
case 1, Left.addDataPoint(train_set(i,:));
case 2, Right.addDataPoint(train_set(i,:));
end
end
%% Test on train set KMEANS
SET = test_same_set;
LABELS = labelstes;
S = size(SET,1);
conf = zeros(2);
predictions = zeros(S,1);
five = @(x) x(1,1);
for i=1:S
distance = [min(Left.getEuclideanDistance(SET(i,:))),min(Right.getEuclideanDistance(SET(i,:)))];
predictions(i) = find(distance == min(distance));
conf(LABELS(i),predictions(i)) = conf(LABELS(i),predictions(i)) + 1;
end
conf
correct = sum(predictions==LABELS)/S