-
Notifications
You must be signed in to change notification settings - Fork 52
/
CNN_TomekLinks.java
412 lines (352 loc) · 12.9 KB
/
CNN_TomekLinks.java
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/***********************************************************************
This file is part of KEEL-software, the Data Mining tool for regression,
classification, clustering, pattern mining and so on.
Copyright (C) 2004-2010
F. Herrera (herrera@decsai.ugr.es)
L. Sánchez (luciano@uniovi.es)
J. Alcalá-Fdez (jalcala@decsai.ugr.es)
S. García (sglopez@ujaen.es)
A. Fernández (alberto.fernandez@ujaen.es)
J. Luengo (julianlm@decsai.ugr.es)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/
**********************************************************************/
package keel.Algorithms.ImbalancedClassification.Resampling.CNN_TomekLinks;
import keel.Algorithms.Preprocess.Basic.*;
import keel.Dataset.*;
import org.core.*;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Arrays;
/**
* <p>
* File: CNN_TomekLinks.java
* </p>
*
* The CNN + TomekLinks algorithm is an undersampling method that can be used to deal with
* the imbalanced problem, a chain procedure of the CNN and Tomek Links methods.
*
* @author Written by Salvador Garcia Lopez (University of Granada) 30/03/2006
* @author Modified by Victoria Lopez Morales (University of Granada) 23/07/2010
* @author Modified by Victoria Lopez Morales (University of Granada) 21/09/2010
* @version 0.1
* @since JDK1.5
*
*/
public class CNN_TomekLinks extends Metodo {
/**
* <p>
* The CNN + TomekLinks algorithm is an undersampling method that can be used to deal with
* the imbalanced problem, a chain procedure of the CNN and Tomek Links methods.
* </p>
*/
/*Own parameters of the algorithm*/
private long semilla;
private int k;
/**
* <p>
* Constructor of the class. It configures the execution of the algorithm by
* reading the configuration script that indicates the parameters that are
* going to be used.
* </p>
*
* @param ficheroScript Name of the configuration script that indicates the
* parameters that are going to be used during the execution of the algorithm
*/
public CNN_TomekLinks (String ficheroScript) {
super (ficheroScript);
}
/**
* <p>
* The main method of the class that includes the operations of the algorithm.
* It includes all the operations that the algorithm has and finishes when it
* writes the output information into files.
* </p>
*/
public void run () {
int S[];
int i, j, l, m;
int nPos = 0, nNeg = 0;
int posID;
int nClases;
int pos;
int baraje[];
int tmp;
double conjS[][];
int clasesS[];
int tamS=0;
int claseObt;
int cont;
int busq;
boolean marcas[];
int nSel;
double conjS2[][];
int clasesS2[];
double minDist, dist;
long tiempo = System.currentTimeMillis();
/*CNN PART*/
/*Count of number of positive and negative examples*/
for (i=0; i<clasesTrain.length; i++) {
if (clasesTrain[i] == 0)
nPos++;
else
nNeg++;
}
if (nPos > nNeg) {
tmp = nPos;
nPos = nNeg;
nNeg = tmp;
posID = 1;
} else {
posID = 0;
}
/*Inicialization of the candidates set*/
S = new int[datosTrain.length];
for (i=0; i<S.length; i++)
S[i] = Integer.MAX_VALUE;
/*Inserting an element of mayority class*/
Randomize.setSeed (semilla);
pos = Randomize.Randint (0, clasesTrain.length-1);
while (clasesTrain[pos] == posID)
pos = (pos + 1) % clasesTrain.length;
S[tamS] = pos;
tamS++;
/*Insert all subset of minority class*/
for (i=0; i<clasesTrain.length; i++) {
if (clasesTrain[i] == posID) {
S[tamS] = i;
tamS++;
}
}
/*Algorithm body. We resort randomly the instances of T and compare with the rest of S.
If an instance doesn´t classified correctly, it is inserted in S*/
baraje = new int[datosTrain.length];
for (i=0; i<datosTrain.length; i++)
baraje[i] = i;
for (i=0; i<datosTrain.length; i++) {
pos = Randomize.Randint (i, clasesTrain.length-1);
tmp = baraje[i];
baraje[i] = baraje[pos];
baraje[pos] = tmp;
}
for (i=0; i<datosTrain.length; i++) {
if (clasesTrain[i] != posID) { //only for mayority class instances
/*Construction of the S set from the previous vector S*/
conjS = new double[tamS][datosTrain[0].length];
clasesS = new int[tamS];
for (j=0; j<tamS; j++) {
for (l=0; l<datosTrain[0].length; l++)
conjS[j][l] = datosTrain[S[j]][l];
clasesS[j] = clasesTrain[S[j]];
}
/*Do KNN to the instance*/
claseObt = KNN.evaluacionKNN (k, conjS, clasesS, datosTrain[baraje[i]], 2);
if (claseObt != clasesTrain[baraje[i]]) {//fail in the class, it is included in S
Arrays.sort(S);
busq = Arrays.binarySearch(S, baraje[i]);
if (busq < 0) {
S[tamS] = baraje[i];
tamS++;
}
}
}
}
/*Construction of the S set from the previous vector S*/
conjS = new double[tamS][datosTrain[0].length];
clasesS = new int[tamS];
for (j=0; j<tamS; j++) {
for (l=0; l<datosTrain[0].length; l++)
conjS[j][l] = datosTrain[S[j]][l];
clasesS[j] = clasesTrain[S[j]];
}
/*TOMEK LINKS PART*/
/*Inicialization of the instance flagged vector of the S set*/
marcas = new boolean[conjS.length];
for (i=0; i<conjS.length; i++) {
marcas[i] = true;
}
nSel = conjS.length;
for (i=0;i<conjS.length; i++) {
minDist = Double.POSITIVE_INFINITY;
pos = 0;
for (j=0; j<conjS.length; j++) {
if (i != j) {
dist = KNN.distancia (conjS[i],conjS[j]);
if (dist < minDist) {
minDist = dist;
pos = j;
}
}
}
if (clasesS[i] != clasesS[pos]) {
if (clasesS[i] != posID) {
if (marcas[i]==true)
{
marcas[i] = false;
nSel--;
}
} else {
if (marcas[pos]==true)
{
marcas[pos] = false;
nSel--;
}
}
}
}
/*Construction of the S set from the flags*/
conjS2 = new double[nSel][conjS[0].length];
clasesS2 = new int[nSel];
for (m=0, l=0; m<conjS.length; m++) {
if (marcas[m]) { //the instance will evaluate
for (j=0; j<conjS[0].length; j++) {
conjS2[l][j] = conjS[m][j];
}
clasesS2[l] = clasesS[m];
l++;
}
}
System.out.println("CNN_TomekLinks "+ relation + " " + (double)(System.currentTimeMillis()-tiempo)/1000.0 + "s");
OutputIS.escribeSalida(ficheroSalida[0], conjS2, clasesS2, entradas, salida, nEntradas, relation);
OutputIS.escribeSalida(ficheroSalida[1], test, entradas, salida, nEntradas, relation);
}
/**
* <p>
* Obtains the parameters used in the execution of the algorithm and stores
* them in the private variables of the class
* </p>
*
* @param ficheroScript Name of the configuration script that indicates the
* parameters that are going to be used during the execution of the algorithm
*/
public void leerConfiguracion (String ficheroScript) {
String fichero, linea, token;
StringTokenizer lineasFichero, tokens;
byte line[];
int i, j;
ficheroSalida = new String[2];
fichero = Fichero.leeFichero (ficheroScript);
lineasFichero = new StringTokenizer (fichero,"\n\r");
lineasFichero.nextToken();
linea = lineasFichero.nextToken();
tokens = new StringTokenizer (linea, "=");
tokens.nextToken();
token = tokens.nextToken();
/*Getting the names of the training and test files*/
line = token.getBytes();
for (i=0; line[i]!='\"'; i++);
i++;
for (j=i; line[j]!='\"'; j++);
ficheroTraining = new String (line,i,j-i);
for (i=j+1; line[i]!='\"'; i++);
i++;
for (j=i; line[j]!='\"'; j++);
ficheroTest = new String (line,i,j-i);
/*Getting the path and base name of the results files*/
linea = lineasFichero.nextToken();
tokens = new StringTokenizer (linea, "=");
tokens.nextToken();
token = tokens.nextToken();
/*Getting the names of output files*/
line = token.getBytes();
for (i=0; line[i]!='\"'; i++);
i++;
for (j=i; line[j]!='\"'; j++);
ficheroSalida[0] = new String (line,i,j-i);
for (i=j+1; line[i]!='\"'; i++);
i++;
for (j=i; line[j]!='\"'; j++);
ficheroSalida[1] = new String (line,i,j-i);
/*Getting the seed*/
linea = lineasFichero.nextToken();
tokens = new StringTokenizer (linea, "=");
tokens.nextToken();
semilla = Long.parseLong(tokens.nextToken().substring(1));
/*Getting the number of neighbors*/
linea = lineasFichero.nextToken();
tokens = new StringTokenizer (linea, "=");
tokens.nextToken();
k = Integer.parseInt(tokens.nextToken().substring(1));
}
/**
* This function builds the data matrix for reference data and normalizes inputs values
* @throws keel.Algorithms.Preprocess.Basic.CheckException Can not be normalized.
*/
protected void normalizar () throws CheckException {
int i, j, k;
Instance temp;
double caja[];
StringTokenizer tokens;
boolean nulls[];
/*Check if dataset corresponding with a classification problem*/
if (Attributes.getOutputNumAttributes() < 1) {
throw new CheckException ("This dataset haven?t outputs, so it not corresponding to a classification problem.");
} else if (Attributes.getOutputNumAttributes() > 1) {
throw new CheckException ("This dataset have more of one output.");
}
if (Attributes.getOutputAttribute(0).getType() == Attribute.REAL) {
throw new CheckException ("This dataset have an input attribute with floating values, so it not corresponding to a classification problem.");
}
entradas = Attributes.getInputAttributes();
salida = Attributes.getOutputAttribute(0);
nEntradas = Attributes.getInputNumAttributes();
tokens = new StringTokenizer (training.getHeader()," \n\r");
tokens.nextToken();
relation = tokens.nextToken();
datosTrain = new double[training.getNumInstances()][Attributes.getInputNumAttributes()];
clasesTrain = new int[training.getNumInstances()];
caja = new double[1];
nulosTrain = new boolean[training.getNumInstances()][Attributes.getInputNumAttributes()];
nominalTrain = new int[training.getNumInstances()][Attributes.getInputNumAttributes()];
realTrain = new double[training.getNumInstances()][Attributes.getInputNumAttributes()];
for (i=0; i<training.getNumInstances(); i++) {
temp = training.getInstance(i);
nulls = temp.getInputMissingValues();
datosTrain[i] = training.getInstance(i).getAllInputValues();
for (j=0; j<nulls.length; j++)
if (nulls[j]) {
datosTrain[i][j]=0.0;
nulosTrain[i][j] = true;
}
caja = training.getInstance(i).getAllOutputValues();
clasesTrain[i] = (int) caja[0];
for (k = 0; k < datosTrain[i].length; k++) {
if (Attributes.getInputAttribute(k).getType() == Attribute.NOMINAL) {
nominalTrain[i][k] = (int)datosTrain[i][k];
datosTrain[i][k] /= Attributes.getInputAttribute(k).
getNominalValuesList().size() - 1;
} else {
realTrain[i][k] = datosTrain[i][k];
datosTrain[i][k] -= Attributes.getInputAttribute(k).getMinAttribute();
datosTrain[i][k] /= Attributes.getInputAttribute(k).getMaxAttribute() -
Attributes.getInputAttribute(k).getMinAttribute();
if (Double.isNaN(datosTrain[i][k])){
datosTrain[i][k] = realTrain[i][k];
}
}
}
}
datosTest = new double[test.getNumInstances()][Attributes.getInputNumAttributes()];
clasesTest = new int[test.getNumInstances()];
caja = new double[1];
for (i=0; i<test.getNumInstances(); i++) {
temp = test.getInstance(i);
nulls = temp.getInputMissingValues();
datosTest[i] = test.getInstance(i).getAllInputValues();
for (j=0; j<nulls.length; j++)
if (nulls[j]) {
datosTest[i][j]=0.0;
}
caja = test.getInstance(i).getAllOutputValues();
clasesTest[i] = (int) caja[0];
}
} //end-method
}