-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxd.m
127 lines (83 loc) · 2.32 KB
/
xd.m
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
#!/usr/bin/octave -q
###1) extraer los datos de las clases 0 a 3;
load("data/mnist/mnisttr.mat.gz");
load("data/mnist/mnisttrlabels.mat.gz");
disp("Datos cargados")
datos = X(xl < 4, :);
labels = xl(xl < 4, :);
###2) separar un conjunto de entrenamiento y otro de test;
rand("seed",23);
[N, columnas] = size(datos);
perm = randperm(N);
datos = datos(perm, :);
labels = labels(perm, :);
Ntrain = round(0.3*N);
tr = datos(1:Ntrain, :);
trlabels = labels(1:Ntrain, :);
te = datos(Ntrain+1:N, :);
telabels = labels(Ntrain+1:N,:);
###3) implementar (en octave) un clasificador de 4 clases mediante
###el método por votación (Transparencias 4.32 a 4.39) utilizando
###clasificadores de dos clases (libsvm);
for i = 0:3
for j = i+1:3
#i vs j
indicesi = find(trlabels == i);
indicesj = find(trlabels == j);
datos = tr([indicesi; indicesj], :);
etiquetas = trlabels([indicesi; indicesj], :);
res = svmtrain(etiquetas, datos,"-q -t 1 -c 1000");
if i == 0
svm(i+j)= res;
else
svm(i+j+1) = res;
endif
endfor
endfor
disp("Svm entrenados");
for i = 1:6
[pred, precision, _] = svmpredict(telabels, te, svm(i),"-q");
prediccionesVOT(:,i) = pred;
endfor
disp("Test clasificado")
for i = 1:rows(prediccionesVOT)
for j = 1:4
votos(i,j) = sum(prediccionesVOT(i,:) == j - 1);
endfor
#ganador(i,1) = max(votos(i,:));
endfor
[numvotos, ganador] = max(votos');
ganador = ganador -1;
disp("Votacion realizada")
disp("VOTACION: ")
aciertosVOT = sum(ganador' == telabels);
porcentajeAciertosVOT = aciertosVOT/rows(telabels)
###4) implementar (en octave) un clasificador de 4 clases mediante
###el método DAG (Transparencia 4.40 y 4.41 y Ejercicio 10 del tema
###4 en el boletín de ejercicios);
for i = 1:rows(telabels)
minimo = 0;
maximo = 3;
while (minimo +1 != maximo)
if minimo == 0
currSVM = maximo;
else
currSVM = minimo + maximo + 1;
endif
pred = svmpredict(telabels(i,:), te(i,:), svm(currSVM),"-q");
if pred != maximo
maximo--;
else
minimo++;
endif
endwhile
if minimo == 0
currSVM = maximo;
else
currSVM = minimo + maximo + 1;
endif
prediccionesDAGS(i) = svmpredict(telabels(i,:), te(i,:), svm(currSVM),"-q");
endfor
disp("DAGS: ")
aciertosDAGS = sum(prediccionesDAGS' == telabels);
porcentajeAciertosDAGS = aciertosDAGS/rows(telabels)