-
Notifications
You must be signed in to change notification settings - Fork 0
/
Experiment.cs
240 lines (206 loc) · 9.02 KB
/
Experiment.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Spartacus {
class Experiment {
private FrmConsole f;
private DateTime startTime;
private string dataset;
private string target;
private int trainingSampleSize;
public static int SampleSeed = 0; // if = 0 will be used a random or '1' if Def.SampleUsingTheSameSeed = true;
private static List<string> datasetAvLst = new List<string>();
public Experiment(FrmConsole f) {
this.f = f;
startTime = DateTime.Now;
f.ad("Started at: " + startTime);
try {
init();
} catch (Exception e) {
FE.Show(e);
}
SampleSeed = 1;
Def.TreeMinNumberOfCasesPerNode = 30;
trainingSampleSize = 70;
Def.Multivariate = true;
Def.Tree.Algorithm = Tree.AlgorithmEnum.Gini;
Def.SampleUsingTheSameSeed = true;
// dataset = "materntur";
//target = "spontdel";
//dataset = "paul_icu";
//target = "outcome";
//dataset = "diabetes";
//target = "retinopa";
//dataset = "wpbc";
//target = "outcome_";
dataset = "icu";
target = "outcome";
Def.PresentCoefficientValue = 1;
//try {
execute();
// } catch (Exception e) {
// FE.Show(e);
// }
}
private void init() {
Def.ExperimentRunning = true;
Def.Schema = new Schema();
Def.Tree = new Tree();
Def.Schema.Tree = Def.Tree;
Def.Tree.Schema = Def.Schema;
Database db = new Database(Database.DriverEnum.PostgreSQL);
Def.Db = db;
Def.Db.ConStr = "DRIVER={" + Def.DbDriver + "};UID=" + Def.DbUser + ";SERVER=" + Def.DbServer + ";Port=" + Def.DbPort + ";Database=" + Def.DbDatabase + ";Password=" + Def.DbPassword;
Def.Db.Connect();
}
private bool dataSetSet(string t) {
List<string> tablesLst = Def.Db.BaseTableList();
foreach (string table in tablesLst) {
if (t == table.Substring(0, table.IndexOf(Def.DbBaseTableSufix))) {
Def.DbTableInUse = t;
return true;
}
}
return false;
}
private bool targetSet(string t) {
bool ret = false;
for (int y = 0; y < Def.Schema.VariableLst.Count; ++y) {
if (t == Def.Schema.VariableLst[y].Name) {
Def.Schema.Target = Def.Schema.VariableLst[y];
Def.Schema.VariableLst[y].VariableRole = SchemaVariable.VariableRoleEnum.Target;
ret=true;
} else {
Def.Schema.VariableLst[y].VariableRole = SchemaVariable.VariableRoleEnum.Predictor;
Def.Schema.PredictorLst.Add(Def.Schema.VariableLst[y]);
}
}
return ret;
}
//private bool predictorAllSet() {
// bool r = false;
// for (int y = 0; y < Def.Schema.VariableLst.Count; ++y) {
// if (Def.Schema.VariableLst[y].VariableRole != SchemaVariable.VariableRoleEnum.Target) {
// Def.Schema.VariableLst[y].VariableRole = SchemaVariable.VariableRoleEnum.Predictor;
// r = true;
// }
// }
// if (r == false) {
// f.ad("You must define a target variable before using predictorAllSet()");
// }
// return r;
//}
private void execute() {
string dimensionality = "_UniVar";
string SampleSeedStr = "";
if(Def.Multivariate)
dimensionality = "_MultVar";
if (SampleSeed!=0)
SampleSeedStr = "_SampleSeed(" + SampleSeed + ")";
string reportFilename = dataset + "_tg(" + target + ")" + dimensionality + "_trSS(" + trainingSampleSize + ")_presCoef(" + Def.PresentCoefficientValue + ")_alg(" + Def.Tree.Algorithm + ")" + SampleSeedStr + "_MinCasePerNode(" + Def.TreeMinNumberOfCasesPerNode + ")" + startTime.Day + "-" + startTime.Month + "-" + startTime.Year + " " + startTime.Hour + " " + startTime.Minute + " " + startTime.Second + " " + startTime.Millisecond;
if (!dataSetSet(dataset)) {
f.ad("Couldn't find data set " + dataset);
f.ad("Available datasets are:");
f.ad("");
List<string> tablesLst = Def.Db.BaseTableList();
foreach (string tb in tablesLst) {
f.ad(tb.Substring(0, tb.IndexOf(Def.DbBaseTableSufix)));
}
return;
} else {
f.ad("Using data set: " + dataset);
}
Def.Schema.PredictorLst.Clear();
try {
Def.Schema.Load();
} catch (Exception e) {
FE.Show(e);
return;
}
if (!targetSet(target)) {
f.ad("Couldn't find target " + target);
f.ad("Available variables are:");
f.ad("");
for (int y = 0; y < Def.Schema.VariableLst.Count; ++y) {
f.ad(Def.Schema.VariableLst[y].Name);
}
return;
} else {
f.ad("Using target: " + target);
}
Application.DoEvents();
try {
sample(trainingSampleSize);
} catch (Exception e) {
FE.Show(e);
return;
}
f.ad("Training set created using " + trainingSampleSize + "% of data," + " validation set created using " + (100-trainingSampleSize) + "% of data");
modelTypeSet();
//try {
treeInitialise();
//} catch (Exception e) {
// FE.Show(e);
// return;
//}
Def.Tree.FullAutogrow(Def.Tree.Root);
Def.Tree.ReductionInImpCalc();
validate(reportFilename);
}
private void sample(int sampleSize) {
string sqlDeleteNulls;
Def.DatasetWithNullRowCount = (int)Def.Db.GetNumber("select count(*) from " + Def.DbBsTb);
Def.DatasetNotNullRowCount = (int)Def.Db.GetNumber("select count(*) from " + Def.DbBsTb + " WHERE " + Def.Schema.Target.Name + " IS NOT NULL");
Def.TrainingSetPercent = sampleSize;
Def.TestingSetPercent = 100 - Def.TrainingSetPercent;
Def.TrainingSetRowCount = Def.TrainingSetPercent * Def.DatasetNotNullRowCount / 100;
Def.TestingSetRowCount = Def.DatasetNotNullRowCount - Def.TrainingSetRowCount;
Def.Db.RefereceTableCreate(0);
Def.Db.ExecuteNonQuery("DROP table " + Def.DbTsTb + "0 ", true);
Def.Db.ExecuteNonQuery("Create table " + Def.DbTsTb + "0 (" + Def.DbTableIdName + " integer PRIMARY KEY)");
Def.Db.Sample();
sqlDeleteNulls =
@"DELETE FROM " +
Def.DbTrTb + "0 " +
"WHERE " +
Def.DbTableIdName + " in " +
"(select " + Def.DbTableIdName + " " +
"FROM " + Def.DbBsTb + " WHERE " + Def.Schema.Target.Name + " IS NULL)";
Def.Db.ExecuteNonQuery(sqlDeleteNulls);
}
private void modelTypeSet() {
if (Def.Schema.Target.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Categorical) {
Def.Tree.ModelType = Tree.ModelTypeEnum.Classification;
} else {
Def.Tree.ModelType = Tree.ModelTypeEnum.Regression;
}
f.ad("Model type: " + Def.Tree.ModelType.ToString());
}
private void treeInitialise() {
if (Def.Schema.Target.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Categorical) {
NodeTargetCategorical root = new NodeTargetCategorical();
Def.Tree.Root = root;
} else {
NodeTargetContinuous root = new NodeTargetContinuous();
Def.Tree.Root = root;
}
Def.Tree.GrowthState = Tree.GrowthStateEnum.Root;
Def.TreeCanBeDisplayed = false;
//Def.FrmMain.TreeBuild();
}
private void validate(string reportFilename) {
if (Def.Multivariate) {
TreeMvValidation t = new TreeMvValidation();
t.NodeFill(Def.Tree.Root);
} else {
TreeValidation t = new TreeValidation();
t.NodeFill(Def.Tree.Root);
}
if (Def.Tree.ModelType == Tree.ModelTypeEnum.Classification) {
TreeClsValReport r = new TreeClsValReport(reportFilename);
r.Dump();
}
}
}
}