-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeclare.h
121 lines (103 loc) · 3.71 KB
/
declare.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
/*
* declare.h
*
* Created on: Mar 31, 2017
* Author: aa
*/
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
#include <iterator>
#include <iostream>
using namespace std;
struct interval{
double left;
double right;
double loss;
};
struct predict{
double treatment;
double left;
double right;
double outcome;
double loss;
};
struct splitFeature{
string name;//The name of split attribute
int type=1;//The type of split attribute
double split_value;//the split value of split attribute if it is numerical. zero if it is nominal
int vindex;//The index of split attribute in the attr_table
double before,after; // the loss before and after
vector<int> leftindex,rightindex;
};
struct Node{
vector<int> index;
unsigned depth=0;
bool isLeaf=false;
splitFeature spf;
interval iv;
vector<Node*> Children;
};
struct pruneBox{
double reduction=0;
unsigned size=0;
double score;
Node* node;
};
struct Model{
Node* node;
double cvLoss;
double testLoss;
double complexity;
};
struct attribute
{
string name;
int vindex;
int type=1;// 1 for numeric and 0 for categorical
vector<double> value;
vector<double> breaks;
double upper;
double lower;
bool discrete=false;
vector<double> levels;
};
struct splitData{
double value;
int index;
};
extern unsigned sided;
extern double S;
extern double alpha;
extern unsigned nfolds;
extern double complexity;
extern unsigned maxDepth;
extern unsigned minSize;
extern double S;
extern double alpha;
extern vector<attribute> features;
//extern attribute outcome;
//extern attribute treatment;
extern vector<double> grid;
Model* cvTree(vector<vector<double> >&data,vector<vector<double> >&test,vector<vector<double> >&IndvLoss,vector<double> &grid, vector<attribute> &Features,vector<double> Complexity,int flag);
double calError(vector<predict> &Prediction);
vector<predict> predictData(vector<vector<double> > &data,vector<int> index,Node* node,vector<attribute> &Features,int flag);
void predictTree(vector<double> &obs,predict &prediction,Node* node,vector<attribute> &Features,int flag);
void pruneTree(Node* root,double complexity);
pruneBox* pruneUpdate(Node* node,double complexity,vector<pruneBox> &PruneSet);
void printTree(Node* node);
Node* duplicateTree(Node* node);
Node* buildTree(vector<vector<double> >&data,vector<vector<double> >&IndvLoss,vector<double> &grid,vector<int> index, vector<attribute> &Features, Node* node,unsigned depth);
splitFeature fastSplit(vector<vector<double> >&data,vector<vector<double> >&IndvLoss,vector<double> &grid,vector<int> index, vector<attribute> &Features);
splitFeature bestSplit(vector<vector<double> >&data,vector<vector<double> >&IndvLoss,vector<double> &grid,vector<int> index, vector<attribute> &Features);
interval updateInterval(vector<vector<double> >&data,vector<vector<double> >&IndvLoss,vector<double> &grid,vector<int> index, vector<vector<double> >&mosaic, int flag);
interval bestInterval(vector<vector<double> >&data,vector<vector<double> >&IndvLoss,vector<double> &grid,vector<int> index, vector<vector<double> > &mosaic);
double calcLoss(vector<vector<double> >&data,vector<vector<double> >&IndvLoss,vector<int> index,double left,double right);
double sumLoss(vector<vector<double> >&IndvLoss,vector<int> indexIn, vector<int> indexOut);
void updateIndvLoss(vector<vector<double> > *data,vector<vector<double> >&IndvLoss,double &S,double &alpha,int flag);
void printData(vector< vector<double> > table);
void read_csv(const string &inFile,vector<vector<double> >&data,vector<attribute> &Features,int &p,int &n);
void varType(vector<attribute> &Features);