-
Notifications
You must be signed in to change notification settings - Fork 0
/
Concensus.java
135 lines (113 loc) · 4.07 KB
/
Concensus.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
/*
@author Victor Lúcio
Federal University of São Paulo - ICT/UNIFESP
"A Classifiers Fusion System Applied to Fenology"
Multiple Classifier System
from: Faria, Fabio "A Framework for Pattern Classifier Selection and Fusion", 2014
Advisors: Jurandy Gomes de Almeida Junior <http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=K4736755E0>
Fabio Augusto Faria <http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=K4266712J6>
*/
import weka.classifiers.*;
import weka.core.*;
import java.util.*;
import java.io.*;
public class Concensus extends AbstractInstanceBasedSelection{
private ArrayList<ArrayList<Boolean>> matrixV, ranking;
private ArrayList<ArrayList<ClassifierPairStruct>> matrixDM;
private ArrayList<Boolean> selected;
private ArrayList<Integer> histogram;
private ArrayList<Double> secondPriority;
private int firstFilter, c;
private Threshold<Integer> threshold;
private AbstractSelectionMetrics metrics;
public Concensus(ArrayList<AbstractDiversityMeasure> dms, int firstFilter, int c,
AbstractSelectionMetrics metrics){
super(dms);
this.firstFilter = firstFilter;
this.c = c;
this.metrics = metrics;
}
public ArrayList<Boolean> select() throws Exception{
makeMatrix();
metrics.initialize(train, classifiers, instancesArray);
diversityCombination();
rank();
makeHistogram();
return selected;
}
private void makeMatrix() throws Exception{
matrixV = new ArrayList<ArrayList<Boolean>>(); //inicialização da matriz
int i, contC = 0, j = 0, k;
double sum = 0.0;
baseClassifiersSize = classifiers.size()/instancesArray.size();
//System.out.println(baseClassifiersSize);
for(i=0;i<classifiers.size();i++)
matrixV.add(new ArrayList<Boolean>());
for(i=0;i<classifiers.size();i++){
for(k=0;k<instancesArray.get(j).size();k++){
if((classifiers.get(i).classifyInstance(instancesArray.get(j).instance(k))) == instancesArray.get(j).instance(k).classValue()){
matrixV.get(i).add(true);
}else{
matrixV.get(i).add(false);
}
}
if((i+1) % baseClassifiersSize == 0)
j++;
}
}
private void diversityCombination(){
int i, j, d;
matrixDM = new ArrayList<ArrayList<ClassifierPairStruct>>();
for(d=0;d<dms.size();d++){
matrixDM.add(new ArrayList<ClassifierPairStruct>());
for(i=0;i<matrixV.size();i++){
for(j=i;j<matrixV.size();j++){
matrixDM.get(d).add(new ClassifierPairStruct(dms.get(d), dms.get(d).measure(matrixV.get(i), matrixV.get(j)), i, j));
}
}
}
}
private void rank(){
int d, i;
ranking = new ArrayList<ArrayList<Boolean>>();
RankingSelection<ClassifierPairStruct> rs = new RankingSelection<ClassifierPairStruct>(firstFilter);
rs.setInverted(true);
for(d=0;d<dms.size();d++){
rs.setArray(matrixDM.get(d));
ranking.add(rs.select());
}
}
private void makeHistogram() throws Exception{
RankingSelection<Double> rv = new RankingSelection<Double>(c);
ArrayList<Double> score = new ArrayList<Double>();
ArrayList<Boolean> measureApplied = metrics.select();
secondPriority = metrics.getMeasures();
histogram = new ArrayList<Integer>(Collections.nCopies(classifiers.size(), 0));
int i,j,m;
for(i=0;i<dms.size();i++){
for(j=0;j<ranking.get(i).size();j++){
if(ranking.get(i).get(j) == true){
histogram.set(matrixDM.get(i).get(j).c1, histogram.get(matrixDM.get(i).get(j).c1)+1);
histogram.set(matrixDM.get(i).get(j).c2, histogram.get(matrixDM.get(i).get(j).c2)+1);
}
}
}
//System.out.println("Histogram " + histogram);
//System.out.println("AverageAcuracyMean " + secondPriority);
for(i=0;i<classifiers.size();i++){
if(measureApplied.get(i)){
m = 1;
//System.out.println(">=");
}else{
m = 0;
//System.out.println("<");
}
score.add((double) ((100*histogram.get(i)*m) + secondPriority.get(i)));
}
//System.out.println(score);
rv.setInverted(true);
rv.setArray(score);
selected = rv.select();
//System.out.println(selected);
}
}