-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
200 lines (150 loc) · 5.71 KB
/
main.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
196
197
198
199
200
#include <QCoreApplication>
#include <iostream>
#include <sstream>
#include <bits/stdc++.h>
#include "author_classification/Preprocessing/text.h"
#include <author_classification/Preprocessing/buildtxt.cpp>
#include "author_classification_fs/pca.h"
#include "ml_on_author_prediction/RS/multiclass_rs.h"
#include "ml_on_author_prediction/KNN/knn.h"
#include "ml_on_author_prediction/ml_on_author_prediction/Hugo_KNN.h"
#include "ml_on_author_prediction/KNN/myknn_algo.h"
//#include <QtWidgets/QApplication>
//#include <QtWidgets/QMainWindow>
//#include <QtCharts/QChartView>
//#include <QtCharts/QPieSeries>
//#include <QtCharts/QPieSlice>
#include <time.h>
using namespace std;
//QT_CHARTS_USE_NAMESPACE
//void Ensemble(double ** pred,int nombreAlgo,int NombreAuteur,int k,int* AuteurK,double* ProbaK){
// double res[NombreAuteur];
// for(int i=0;i<NombreAuteur;i++)
// res[i]=0;
// for(int i=0;i<NombreAuteur;i++){
// for(int j=0;j<nombreAlgo;j++){
// res[i]=res[i]+pred[j][i];
// }
// }
// for(int i=0;i<NombreAuteur;i++)
// res[i]=res[i]/nombreAlgo;
// vector <int> a;
// for(int i=0;i<NombreAuteur;i++){
// a.push_back(res[i]);
// }
// std::sort(a.begin(), a.end());
// int b[NombreAuteur];
// for (int i = 0; i < k; ++i){
// ProbaK[i]=a.at(k-1-i);
// for(int j=0;j<NombreAuteur;j++){
// if(res[j]==ProbaK[i]){
// b[i]=j;
// res[j]=-1;
// }
// }
// AuteurK[i]=b[i];
// }
// }
int main()
{
time_t timer;
double new_time;
new_time = time(&timer);
int no_authors = 10;
int lineCount = 0; //nbre de ligne du fichier csv qui résultera du texte test
float parameter = 0.98;
std::string c = "../apprentissage_10.txt";
std::string test_text = "../testResult.txt";
int numberOfTrees = 1;
string train = "../finalMatrix.csv";
string test = "../finalTest.csv";
int NumbersofNeighbors=3;
std::cout << "Fetching features..." << std::endl;
lineCount = preprocessingTest("../1013.txt","txt", "testResult");
new_time = difftime(time(&timer),new_time);
std::cout << new_time << " seconds since previous stage" << endl;
std::cout << "Starting PCA..." << std::endl;
//principalComponentAnalysis(c,test_text, parameter);
new_time = difftime(time(&timer),new_time);
std::cout << new_time << " seconds since previous stage" << endl;
std::cout << "Learning task..." << endl;
//machine learning
std::cout << "Random Forest:" << std::endl;
multiclass_rs * classifier1 = new multiclass_rs(numberOfTrees, no_authors);
int * predictions_random_forest = classifier1->run_random_forest(train, test);
new_time = difftime(time(&timer),new_time);
std::cout << new_time << " seconds since previous stage" << endl;
// std::cout << "First KNN:" << endl;
int * predictions_first_knn = KNN_(NumbersofNeighbors, test, train);
new_time = difftime(time(&timer),new_time);
std::cout << new_time << " secondes since previous stage" << endl;
std::cout << "Turning predictions into probability..." << std::endl;
double proba_rs [no_authors];
double proba_first_knn [no_authors];
for (int i = 0; i < no_authors; i++){
proba_rs[i] = 0;
proba_first_knn[i] = 0;
}
for (int i = 0; i < no_authors; i++) {
for (int j = 0; j < lineCount; j++){
if (predictions_random_forest[j] == i)
proba_rs[i] += 1;
if (predictions_first_knn[j] == i)
proba_first_knn[i] += 1;
}
}
for (int i = 0; i < no_authors; i++){
proba_rs[i] /= lineCount;
proba_first_knn[i] /= lineCount;
}
new_time = difftime(time(&timer),new_time);
std::cout << new_time << " seconds since previous stage" << endl;
for (int i=0; i < no_authors; i++){
std::cout << IdToAuthor(i) << " : " << proba_rs[i] << endl;
}
//hugo
// std::cout << "Second KNN:" << endl;
// int no_main_authors = 3;
// double res[no_main_authors];
// ExecutionML(train, test, no_main_authors, res);
// std::cout << "resultat hugo" << res[0] << std::endl;
// new_time = difftime(time(&timer),new_time);
// new_time /= 60;
// std::cout << new_time << " minutes since previous stage" << endl;
// std::cout << "Combining results of different learners..." << std::endl;
// int no_algos = 2;
// double * res2 [no_algos];
// res2[0] = proba_rs;
// res2[1] = proba_first_knn;
// int no_main_authors = 3;
// double ProbaK[no_main_authors];
// int AuthorK[no_main_authors];
// Ensemble(res2, no_algos, no_authors, no_main_authors, AuthorK, ProbaK);
// for (int i = 0; i < no_main_authors;i++) {
// cout << "Author " << IdToAuthor(AuthorK[i]) << ":" << ProbaK[i] << endl;
// }
// std::cout<< "Preparing pie chart" << std::endl;
// QApplication a();
// QPieSeries *series = new QPieSeries();
// for (int i = 0; i < no_main_authors ; i++) {
// series->append((char*)AuthorK[i], (int) 100*ProbaK[i]);
// }
// QPieSlice *slice = series->slices().at(1);
// slice->setExploded();
// slice->setLabelVisible();
// slice->setPen(QPen(Qt::darkGreen, 2));
// slice->setBrush(Qt::green);
// QChart *chart = new QChart();
// chart->addSeries(series);
// chart->setTitle("Author?");
// //chart->legend()->hide();
// QChartView *chartView = new QChartView(chart);
// chartView->setRenderHint(QPainter::Antialiasing);
// QMainWindow window;
// window.setCentralWidget(chartView);
// window.resize(400, 300);
// window.show();
// new_time = difftime(time(&timer),new_time);
// std::cout << new_time << " seconds since previous stage" << endl;
return 0;
}