-
Notifications
You must be signed in to change notification settings - Fork 3
/
function.h
462 lines (389 loc) · 14.6 KB
/
function.h
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// Created by tianle on 11/16/17.
//
#ifndef QUESTDECISIONTREE_FUNCTION_H
#define QUESTDECISIONTREE_FUNCTION_H
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_vector_double.h>
using namespace std;
// some type definitions
typedef vector<string> StringRecord;
typedef vector<StringRecord> StringTable;
typedef map<string,bool> MapStrBool;
typedef map<string,int> MapStrInt;
typedef vector<int> Record;
typedef vector<double> RecordDouble;
typedef vector<RecordDouble> Table;
class Variable{
/* Variable class indicates the variables used to make decision tree and
* some attributes of one variable.
*
* */
public:
string name; //variable name
int type; // Variable type which consist of Ordered variable and categorical variable.
int id; // 1 and 2 denote Ordered variable and categorical variable respectively.
bool isUsed; // indicate if the variable has been used for the tree.
};
typedef vector<Variable> VarVector;
class Node{
/*
* Node class
* */
public:
Variable attribute; //selected variable of the node
VarVector var_sel_sta; //all the variables and it's status--used or not used.
Table data_sta; //Remaining training data of the node
double splitPoint; // split point of the node.
bool isLeaf; // leaf node or not.
double classified; // classified results for leaf node, otherwise,empty value.
Node* leftChild; // left child
Node* rightChild; // right child
Node();
Node(Table data_sta,VarVector var_sta);
};
class Statistic{
/*
* Some statistic method, which will be used.
*
* */
public:
//static RecordDouble FvalueWithAnovaTest(Table data,VarVector variables); // Analysis of variance, with F values returned. using square deviation
//static RecordDouble FvalueAnovaTestWithAbs(Table data,VarVector variables);// Analysis of variance, with F values returned. using absolute deviation
//static double ChiSquareTest(Table data,int column1,int column2); // Chi-square analysis.
//static double calcu_variance(RecordDouble sets); // Calculate variance
//static double determain_class(Table data); // Determaine the class label according to the most frequent class in the dataset.
static RecordDouble FvalueWithAnovaTest(Table data,VarVector variables) {
/*
* Analysis of variance. with square deviation
* data --data
* variables ---Containing all the variables, may with different statues, used or not.
* return -- the collection of F value for each variable
* */
RecordDouble Fvalue;
for(int i=0;i<variables.size();i++){
if (variables[i].type==1 && !(variables[i].isUsed)) {
Table anovaTable;
//统计该变量下Level的数目
for(int m=0;m<data.size();m++){
//levels
RecordDouble tr;
tr.push_back(data[m][i]);
if(find(anovaTable.begin(),anovaTable.end(),tr)==anovaTable.end()){
anovaTable.push_back(tr);
}
}
sort(anovaTable.begin(),anovaTable.end());
//
// 将每一个level的观测值加入
for(int p=0;p<anovaTable.size();p++){
for(int q=0;q<data.size();q++){
if(data[q][i]==anovaTable[p][0]){
anovaTable[p].push_back(data[q][data[q].size()-1]);
}
}
}
//计算每一个level的观测值的数量
for(int j=0;j<anovaTable.size();j++){
anovaTable[j].push_back(accumulate(anovaTable[j].begin()+1,anovaTable[j].end(),0));
anovaTable[j].push_back(anovaTable[j].size()-2);
}
//利用anovatable 计算 F value
double Sa=0;
double interval=0;
double T=0;
double N=0;
for(int k=0;k<anovaTable.size();k++){
interval+=pow(anovaTable[k][anovaTable[k].size()-2],2)/double (anovaTable[k][anovaTable[k].size()-1]);
T+=anovaTable[k][anovaTable[k].size()-2];
N+=anovaTable[k][anovaTable[k].size()-1];
}
Sa=interval-pow(T,2)/N;
double MSa= Sa /double (anovaTable.size()-1);
double interval2=0;
for(int s=0;s<anovaTable.size();s++){
for(int t=1;t<anovaTable[s].size()-2;t++){
interval2+=pow(anovaTable[s][t],2);
}
}
double Se=interval2-interval;
double MSe= Se / double (N-(anovaTable.size()));
double F=MSa/MSe;
if(!(F==F)){
F=-1;
}
Fvalue.push_back(F);
}else {
Fvalue.push_back(-1);
}
}
return Fvalue;
}
static RecordDouble FvalueAnovaTestWithAbs(Table data,VarVector variables){
/*
*
* Analysis of variance. with abslolute deviation
* data --data
* variables ---Containing all the variables, may with different statues, used or not.
* return -- the collection of F value for each variable
*
* */
RecordDouble Fvalue;
for(int i=0;i<variables.size();i++){
if (variables[i].type==1 && !(variables[i].isUsed)) {
Table anovaTable;
//统计该变量下Level的数目
for(int m=0;m<data.size();m++){
//levels
RecordDouble tr;
tr.push_back(data[m][i]);
if(find(anovaTable.begin(),anovaTable.end(),tr)==anovaTable.end()){
anovaTable.push_back(tr);
}
}
sort(anovaTable.begin(),anovaTable.end());
for(int p=0;p<anovaTable.size();p++){
for(int q=0;q<data.size();q++){
if(data[q][i]==anovaTable[p][0]){
anovaTable[p].push_back(data[q][data[q].size()-1]);
}
}
}
//计算每一个level的观测值的数量
for(int j=0;j<anovaTable.size();j++){
anovaTable[j].push_back(accumulate(anovaTable[j].begin()+1,anovaTable[j].end(),0));
anovaTable[j].push_back(anovaTable[j].size()-2);
}
//利用anovatable 计算 F value
double Sa=0;
double interval=0;
double T=0;
double N=0;
for(int k=0;k<anovaTable.size();k++){
interval+=abs(anovaTable[k][anovaTable[k].size()-2])/double (anovaTable[k][anovaTable[k].size()-1]);
T+=anovaTable[k][anovaTable[k].size()-2];
N+=anovaTable[k][anovaTable[k].size()-1];
}
Sa=interval-abs(T)/N;
double MSa= Sa /double (anovaTable.size()-1);
double interval2=0;
for(int s=0;s<anovaTable.size();s++){
for(int t=1;t<anovaTable[s].size()-2;t++){
interval2+=abs(anovaTable[s][t]);
}
}
double Se=interval2-interval;
double MSe= Se / double (N-(anovaTable.size()));
double F=MSa/MSe;
Fvalue.push_back(F);
}else {
Fvalue.push_back(-1);
}
}
return Fvalue;
}
static double ChiSquareTest(Table data,int column1,int column2) {
/*
* Chi-square test using two column vector
* data --data
* column1 --the index of first column
* column2 --the index of second column
* return --the P value of chi-square test.
*
* */
Table contigencytable;
RecordDouble columnvector1;
RecordDouble columnvector2;
for(int i=0;i<data.size();i++){
if(find(columnvector1.begin(),columnvector1.end(),data[i][column1])==columnvector1.end()){
columnvector1.push_back(data[i][column1]);
}
}
sort(columnvector1.begin(),columnvector1.end());
RecordDouble lastline;
for(int t=0;t<columnvector1.size()+1;t++){
lastline.push_back(0);
}
for(int m=1;m<=3;m++) {
RecordDouble line;
for (int n = 0; n < columnvector1.size(); n++) {
int counter=0; // the number cases that column1=n && column2=m
for (int k = 0; k < data.size(); k++) {
if (data[k][column1] == columnvector1[n] && data[k][column2] == m) {
counter++;
}
}
line.push_back((double)counter);
}
double sum=accumulate(line.begin(),line.end(),0);
line.push_back(sum);
contigencytable.push_back(line);
for(int s=0;s<line.size();s++){
lastline[s]+=line[s];
}
}
contigencytable.push_back(lastline);
double chisquare=0;
int s=contigencytable.size();
int r=contigencytable[0].size();
for(int i=0;i<s;i++){
for(int j=0;j<r;j++){
chisquare+=pow(contigencytable[s-1][r-1]*contigencytable[i][j]-contigencytable[i][r-1]*contigencytable[s-1][j],2)/double (contigencytable[s-1][r-1]*contigencytable[i][r-1]*contigencytable[s-1][j]);
}
}
double mu=(s-1)*(r-1);
double pValue=gsl_cdf_chisq_P(chisquare,mu);
return pValue;
}
static double calcu_variance(RecordDouble resultSet){
/*
* Calculate variance of one list
* */
double sum = std::accumulate(std::begin(resultSet), std::end(resultSet), 0.0);
double mean = sum / resultSet.size(); //均值
double accum = 0.0;
std::for_each (std::begin(resultSet), std::end(resultSet), [&](const double d) {
accum += (d-mean)*(d-mean);
});
double var = accum/(double)(resultSet.size()); //方差
return var;
}
static double determain_class(Table data) {
/*
* Determaine the class label according to the most frequest class of the dataset.
*
* */
double class1=0;
double class2=0;
double class3=0;
for(int i=0;i<data.size();i++){
if(data[i][5]==1){
class1++;
}else if(data[i][5]==2){
class2++;
}else{
class3++;
}
}
double maxclass=1;
if(class2>class1 && class2>class3){
maxclass=2;
}
if(class3>class1 && class3>class2){
maxclass=3;
}
if(class1>class2 && class1>class3){
maxclass=1;
}
return maxclass;
}
};
class MatrixOp{
/*
* some matrix operations.
* */
public:
MatrixOp();
// static void gsl_matrix_mul(gsl_matrix *a,gsl_matrix *b,gsl_matrix *c);
// static void gsl_matrix_inverse(gsl_matrix *A, gsl_matrix *inverse);
// static gsl_matrix* gsl_matrix_transpose(gsl_matrix *A);
float getSquareError(Table clusters[],RecordDouble means[]);
RecordDouble getMeans(Table cluster);
static void gsl_matrix_mul(gsl_matrix *a,gsl_matrix *b,gsl_matrix *c)
{
/*
* Matrix multiplication,the data structure of matrix must be gsl_matrix.
* a -- Matrix A
* b -- matrix B
* c -- matrix of AB
* */
for (size_t i=0;i<a->size1;i++)
{
for (size_t j=0;j<b->size2;j++)
{
double sum=0.0;
for (size_t k=0;k<b->size1;k++)
{
sum+=gsl_matrix_get(a,i,k)*gsl_matrix_get(b,k,j);
}
gsl_matrix_set(c,i,j,sum);
}
}
}
static void gsl_matrix_inverse(gsl_matrix *A, gsl_matrix *inverse)
{
/*
* Matrix inverse operation, the data structure of matrix must be gsl_matrix.
* A --matrix
* inverse --inverse matrix of A
* */
int n = A->size1;
gsl_matrix *tmpA = gsl_matrix_alloc(n, n);
gsl_matrix_memcpy(tmpA, A);
gsl_permutation *p = gsl_permutation_alloc(n);
int sign = 0;
gsl_linalg_LU_decomp(tmpA, p, &sign);
inverse = gsl_matrix_alloc(n, n);
gsl_linalg_LU_invert(tmpA, p, inverse);
gsl_permutation_free(p);
gsl_matrix_free(tmpA);
}
static gsl_matrix* gsl_matrix_transpose(gsl_matrix *A){
/*
* Matrix transpose operation, the data structure of matrix must be gsl_matrix.
* A --Matrix A
* return --the transpose of A
* */
gsl_matrix *B=gsl_matrix_alloc(A->size2,A->size1);
for(int i=0;i<A->size2;i++){
for(int j=0;j<A->size1;j++){
gsl_matrix_set(B,i,j,gsl_matrix_get(A,j,i));
}
}
return B;
}
};
class Kmeans {
public:
/* Kmeans clustering impletation, which will be used later.
*
* */
Table clustereddata;
Kmeans();
Kmeans(int k, Table data);
double getDistance(RecordDouble tuple1,RecordDouble tuple2);
int clusterOfTuple(RecordDouble means[],RecordDouble tuple);
float getSquareError(Table clusters[],RecordDouble means[]);
RecordDouble getMeans(Table cluster);
};
class QuestDecisionTree{
/*
* The construction of decision tree.
* */
private:
Node* Tree; //Decision tree
int VariableNum; // the number of all the variables
Table TrainingData; //Training data
VarVector variables;// variable set.
public:
QuestDecisionTree();
QuestDecisionTree(Table data,VarVector variables);
Node* trainingTree(Table TrainingData,VarVector variables);
Variable variableSelection(Table data,VarVector variables);
double splitSelection(Table& data,Variable splitVar);
double SplitSelection_Ordered(Table data,Variable splitVar);
Node* get_tree();
};
#endif //QUESTDECISIONTREE_FUNCTION_H