-
Notifications
You must be signed in to change notification settings - Fork 0
/
dimensionReduction.cpp
executable file
·196 lines (168 loc) · 6.22 KB
/
dimensionReduction.cpp
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
/**
* Functions for Dimensionality Reduction:
* PCA
* Entropy
*
* Classifier:
* Normal Bayes
*
* Author: Gabriela Thumé
* Universidade de São Paulo / ICMC / 2014
**/
#include <opencv2/highgui/highgui.hpp>
#include <fstream>
#include <sstream>
#include <iostream>
#include <dirent.h>
#include <map>
#include <cmath>
#include <vector>
#include "funcoesArquivo.h"
#include "classifier.h"
using namespace cv;
using namespace std;
double log2(double number){
return log(number)/log(2);
}
Mat entropyReduction(Mat data, int tam_janela, string name_my_file){
map<float, int> frequencias;
map<float, int>::const_iterator iterator;
double entropy, prob;
int i, j, height, width, janela, fim_janela, indice_janela;
string arq_saida;
stringstream tam;
tam << tam_janela;
height = data.size().height;
width = data.size().width;
Mat vectorEntropy(height, ceil((float)width/tam_janela), CV_32FC1);
arq_saida = "entropy/ENTROPIA_" + tam.str() + "_" + name_my_file;
ofstream arq(arq_saida.c_str());
for (i = 0; i < height; i++){
janela = 0;
indice_janela = 0;
while (janela < width){
entropy = 0;
fim_janela = janela+tam_janela;
if (fim_janela > width)
fim_janela = width;
frequencias.clear();
for (j = janela; j < fim_janela; j++){
float valor = trunc(10000*data.at<float>(i, j))/10000;
frequencias[valor]++;
}
for (iterator = frequencias.begin(); iterator != frequencias.end(); ++iterator) {
prob = static_cast<double>(iterator->second) / (fim_janela -janela) ;
entropy += prob * log2( prob ) ;
}
if (entropy != 0)
entropy *= -1;
vectorEntropy.at<float>(i, indice_janela) = (float)entropy;
indice_janela++;
janela += tam_janela;
}
}
arq << vectorEntropy;
arq.close();
return vectorEntropy;
}
Mat pcaReduction(Mat data, int nComponents, string name_my_file){
Mat projection, eigenvectors;
stringstream n;
n << nComponents;
string arq_saida = "pca/PCA_" + n.str() + "_" + name_my_file;
ofstream arq(arq_saida.c_str());
PCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, nComponents);
eigenvectors = pca.eigenvectors.clone();
projection = pca.project(data);
arq << projection;
arq.close();
return projection;
}
void inputError(){
cout << "This programs waits: <directory> <technique> <attributes for PCA | window size for Entropy>\n";
cout << "\tTechnique: 0-None, 1-PCA, 2-Entropy ou 3-All\n";
exit(0);
}
int main(int argc, const char *argv[]){
Classifier c;
Mat vectorEntropy, projection, data, classes;
DIR *directory;
struct dirent *arq;
ifstream my_file;
string name_arq, name_dir, name;
int nClasses, metodo, atributos, janela;
pair <int, int> minority(-1,-1);
float prob = 0.5;
if (argc < 3)
inputError();
name_dir = argv[1];
metodo = atoi(argv[2]);
switch(metodo){
case 0: /* Just classification */
break;
case 1: /* PCA */
if (argc < 4)
inputError();
atributos = atoi(argv[3]);
break;
case 2: /* Entropy */
if (argc < 4)
inputError();
janela = atoi(argv[3]);
break;
case 3: /* PCA + Entropy */
if (argc < 5)
inputError();
atributos = atoi(argv[3]);
janela = atoi(argv[4]);
break;
default:
break;
}
/* For each file on input directory, do the following operations */
directory = opendir(name_dir.c_str());
if (directory != NULL){
while ((arq = readdir(directory))){
string out = "";
name_arq = arq->d_name;
name = name_dir + arq->d_name;
my_file.open(name.c_str());
if(my_file.good()){
/* Read the feature vectors */
data = readFeatures(name.c_str(), classes, nClasses);
if (data.size().height != 0){
switch(metodo){
case 0:
cout << endl << "Classification for "<< name.c_str() << endl;
c.bayes(prob, 10, data, classes, nClasses, minority, out.c_str());
break;
case 1:
cout << endl << "PCA for "<< name.c_str() << " with " << atributos << " attributes" << endl;
projection = pcaReduction(data, atributos, name_arq);
c.bayes(prob, 10, projection, classes, nClasses, minority, out.c_str());
break;
case 2:
cout << endl << "Entropy for "<< name.c_str() << " with window = " << janela << endl;
vectorEntropy = entropyReduction(data, janela, name_arq);
c.bayes(prob, 10, vectorEntropy, classes, nClasses, minority, out.c_str());
break;
case 3:
cout << endl << "Classification for "<< name.c_str() << endl;
c.bayes(prob, 10, data, classes, nClasses, minority, out.c_str());
cout << endl << "PCA for "<< name.c_str() << " with " << atributos << " attributes" << endl;
projection = pcaReduction(data, atributos, name_arq);
c.bayes(prob, 10, projection, classes, nClasses, minority, out.c_str());
cout << endl << "Entropy for "<< name.c_str() << " with window = " << janela << endl;
vectorEntropy = entropyReduction(data, janela, name_arq);
c.bayes(prob, 10, vectorEntropy, classes, nClasses, minority, out.c_str());
break;
default:
break;
}
}
}
my_file.close();
}
}
return 0;
}