-
Notifications
You must be signed in to change notification settings - Fork 0
/
klassificatiescript2.asv
179 lines (135 loc) · 4.3 KB
/
klassificatiescript2.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
clear all;
fprintf('Support Vector Classification\n')
fprintf('_____________________________\n')
fig=1;
% Making X and Y
% %*******************
if 1
N=100;
load dataset.mat
%sol=classification_dataset(N,'gaussian');
X=sol{1,1};
Y=sol{1,2};
Xtest=sol{1,3};
Ytest=sol{1,4};
gam=sol{1,5};
%sigma=sol{1,6};
sigma=50;%If the sigma very small this lead to overfitting
end
% Dimension of the invoer:
%************************
[d N]=size(X);%rows and Columns
%visialisation of the input data:
%*******************************
figure(1)
idx=find(Y==1);
idx1=find(Y==-1);
plot(X(1,idx),X(2,idx),'ro',X(1,idx1),X(2,idx1),'b+'); %Inserted to solve the absence of gplotmatrix
% gplotmatrix(X',X',Y)
title('Scatter plot of the input')
% Parameters
%************
% Type of kernels: linear, polynomial, , gaussian_rbf,
%type='linear';
type='gaussian_rbf';
%type='exponential_rbf';
fprintf('Type of kernel %s \n',type)
epsilon=1e-20;%precision numerica de la maquina (aqui n o puede)
% Penalty factor for missclassification:
C=20;
% Penalty factor for missclassification:
C=20;
% Cminus Penalization over Test examples:
Cminus=0;
% Cminus Penalization over Test examples
Cplus=0;
%Number of Positive examples
numPlus=10;
% For the polynomial kernel :
dp=2;%Grado del polinomio (ver pagina 97)
%gaussianse of exponentiele RBF kernel:
%sigma=1;
% for the MLP kernel:
scale=1;
offset=1;
% for the B-spline kernel
ds=7;
% parameters:
%************
switch type
case 'linear'
par=[];%parametros del Kernel
type_nummer=0;
case 'polynomial'
% the dimension of the polynomial is given in the variable par
fprintf('the order is: %d \n',dp)
par=[dp];%parametros del Kernel(Aqui es el grado del polimonio)
type_nummer=1;
case 'gaussian_rbf'
% the sigma value of the rbf is given in par
% By means of crossvalidation
fprintf('sigma is: %d \n',sigma)
type_nummer=2;
par=[sigma];%parametros del Kernel(Aqui es la desviacion)
case 'exponential_rbf'
% the sigma value of the rbf is given in par
fprintf('sigma is: %d \n',sigma)
par=[sigma];
type_nummer=3;
case 'mlp'
% the w and bias value of the tanh is given in par
par=[scale offset];
type_nummer=4;%numero del Kernel para la funcion.
case 'Bspline'
% the order of the B-spline is given in par
par=[ds];
type_nummer=6;
case 'Local RBF' %'locale gaussian_rbf'
% the sigma value of the rbf is given in par
% By means of crossvalidation
fprintf('sigma is: %d \n',sigma)
type_nummer=7;
par=[sigma];
end
% training the QP-svm:
% *********************
t=cputime;
solution=solve_svm_qp(X,Y,Xtest,Ytest,C,Cminus,Cplus,epsilon,type_nummer,par);
solTsvm=tsvm(X,Y,Xtest,Ytest,C,Cminus,Cplus,numPlus,epsilon,type_nummer,par);
time=cputime-t;
fprintf('The training needed %d',time),fprintf(' seconds.\n');
alpha1=solution{1,1};
b1=solution{1,2};
% Plotting training set.
%****************************
if length(X(:,1))==2
figure(2),subplot(1,2,1)
SVMplot(X,Y,X,Y,alpha1,b1,40,type_nummer,par,epsilon);
title('training set');
end
% Classification of the training set:
%************************************
Yclass=sign(fsvmclass(X,Y,type_nummer,epsilon,alpha1,b1,X,par));
misclass=length(find((Yclass+Y)==0));
fprintf('number of misclassified points of the training set: %d (%3.1f%%) \n',misclass,100*misclass/length(Y'));
% Classification of the test set:
%**********************************
if ~(isempty(Xtest))
if length(X(:,1))==2
figure(2),subplot(1,2,2),SVMplot(Xtest,Ytest,X,Y,alpha1,b1,40,type_nummer,par,epsilon),
title('test set');
end
latent=fsvmclass(X,Y,type_nummer,epsilon,alpha1,b1,Xtest,par);
% ROC curve:
%************
p=find(Ytest==-1);
Yroc=Ytest;
for i=1:length(p)
Yroc(p(i))=0;
end
figure(3),ROC2(latent,Yroc,1);
Yclass=sign(latent);
misclass=length(find((Yclass+Ytest)==0));
fprintf('misclassified points of the test set: %d (%3.1f%%) \n',misclass,100*misclass/length(Ytest'));
title('test set');
end