-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSet.java
More file actions
303 lines (285 loc) · 8.77 KB
/
Copy pathDataSet.java
File metadata and controls
303 lines (285 loc) · 8.77 KB
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
import java.io.File;
import java.io.FileInputStream;
import java.util.Random;
import java.util.Scanner;
public class DataSet implements OptionHandler{
protected Attributes attributes;
protected Examples examples;
protected String name;
protected Random random;
protected long seed;
protected boolean isPartitioned = false;
protected int[] partitionflag;
Double [] p;
boolean pIsCalculated = false;
int index;
public DataSet(Attributes attributes, String a) {
this.attributes = attributes;
this.name = a;
}
public DataSet(Attributes attributes) {
this.attributes = attributes;
this.examples = new Examples(attributes);
index = this.attributes.getClassIndex();
}
public DataSet() {
// TODO Auto-generated constructor stub
}
void add(DataSet dataset) {
this.attributes = dataset.attributes;
this.examples = dataset.examples;
index = this.attributes.getClassIndex();
}
void add(Example example) {
this.examples.add(example);
}
public Attributes getAttributes() {
return this.attributes;
}
public Examples getExamples() {
return this.examples;
}
public boolean getHasNominalAttributes() {
for (int i=0;i<this.attributes.getSize();i++) {
if(attributes.get(i).isNominal())
return true;
}
return false;
}
public boolean getHasNumericAttributes() {
for (int i=0;i<this.attributes.getSize();i++) {
if(!attributes.get(i).isNominal())
return true;
}
return false;
}
public void load(java.lang.String filename) throws java.lang.Exception {
System.out.println(filename);
File file = new File(filename);
Scanner scan = new Scanner(new FileInputStream(file));
String datasetname = "";
String s = "";
scan.useDelimiter("\n");
while (scan.hasNext()) {
s=scan.next();
if(s.contains("@dataset")) {
String[] q =s.split(" ");
datasetname = q[1];
}
if(s.contains("@attribute")) {
String parsing = "";
while(s.contains("@attribute")) {
parsing+=s+"\n";
s=scan.next();
}
// System.out.println(parsing);
Scanner temp = new Scanner(parsing);
this.attributes = new Attributes(temp);
}
else if(s.contains("@examples")) {
String parsing="";
while(scan.hasNext()) {
parsing+=scan.next()+"\n";
}
Scanner temp = new Scanner(parsing);
this.examples = new Examples(this.attributes,temp);
}
//System.out.println(s);
}
//дdataset
this.name = datasetname;
index = this.attributes.getClassIndex();
}
private void parse(java.util.Scanner scanner) throws java.lang.Exception {
}
public void addExamples(Examples examples) {
this.examples = examples;
}
public java.lang.String toString() {
String output = "@dataset "+name+"\n\n";
output+=this.attributes.toString()+"\n";
output+=this.examples.toString()+"\n\n\n";
return output;
}
public void setRandom(java.util.Random random) {
this.random = random;
}
public void setSeed(long seed) {
this.seed = seed;
}
public long getSeed() {
return this.seed;
}
public void Partition(int k) {
random = new Random();
//do not use seed in this project because the seed is a constant, will generate pseudo-random number
//when implementing input a seed from command line, can use seed for generating.
partitionflag = new int[this.examples.size()];
for (int i=0;i<partitionflag.length;i++) {
partitionflag[i] = random.nextInt(k);
//System.out.println(partitionflag[i]);
}
isPartitioned = true;
}
public TrainTestSets getCVSets(int p, int k) {
//System.out.println(p+"===");
TrainTestSets output = new TrainTestSets();
DataSet newTrain = new DataSet(this.attributes);
DataSet newTest = new DataSet(this.attributes);
//int count1=0, count2=0;
if (!isPartitioned)
Partition(k);
//build training set
for (int i=0;i<this.examples.size();i++) {
if (partitionflag[i]==p) {
newTest.add(this.examples.get(i));
//count1++;
}
else {
Example temp = this.examples.get(i);
newTrain.add(temp);
//count2++;
}
}
//System.out.println(count1+" "+ count2);
output.setTestingSet(newTest);
output.setTrainingSet(newTrain);
if (output.test.examples.size()<2||output.train.examples.size()<2) {
System.out.println("There is not enough examples for each partition,"
+ "please choose a new -x value and try again.");
System.exit(0);
}
return output;
}
@Override
public void setOptions(String[] args) {
// TODO Auto-generated method stub
}
int getBestAttribute() throws Exception {
index = this.attributes.getClassIndex();
Double maxGainRatio=0.0; //the value must >0,if=0,means do not need to split
int bestIndex=-1;
for (int i=0;i<this.attributes.getSize();i++) {
if (i==index) continue;
if (getGainRatio(i)>maxGainRatio) {
maxGainRatio = getGainRatio(i);
//System.out.println(maxGainRatio+"DataSet::getBestAttribute");
bestIndex = i;
}
}
if (maxGainRatio==0.0) return -1;
return bestIndex;
}
DataSet[] partitiononAttribute(int attributeIndex)//return DataSet[] {
index = this.attributes.getClassIndex();
DataSet[] DataSets = new DataSet[this.attributes.get(attributeIndex).domain.size()];
for(int i=0;i<DataSets.length;i++) {
DataSets[i] = new DataSet(this.attributes);
}
int temp =0;
for (int i=0;i<this.examples.size();i++) {
temp = this.examples.get(i).get(attributeIndex).intValue();
DataSets[temp].add(this.examples.get(i));
}
return DataSets;
}
Double getGainRatio(int attributeIndex) throws Exception {
if (getSplitInformation(attributeIndex)==0)
return 0.0;
//System.out.println(attributeIndex);
//System.out.println(getGain(attributeIndex)/getSplitInformation(attributeIndex));
return getGain(attributeIndex)/getSplitInformation(attributeIndex);
}
Double getGain(int attributeIndex) throws Exception {
index = this.attributes.getClassIndex();
Double entropy = getEntropy();
Double gain = entropy;
//System.out.println(entropy);
//get sv/s
Double [] count = new Double[this.attributes.get(attributeIndex).domain.size()];
Double[] s = new Double[count.length];
int temp = 0;
for (int k=0;k<count.length;k++) count[k] =0.0;
for (int i =0;i<this.examples.size();i++) {
temp = examples.get(i).get(attributeIndex).intValue();
count[temp] += 1;
}
for(int i=0;i<s.length;i++) {
s[i] = (double) (count[i]/this.examples.size());
}
for(int i=0;i<this.attributes.get(attributeIndex).domain.size();i++) //ith attribute {
Double all = 0.0;//define Double for the divide computation
Double sEntropy = 0.0;
Double[] counts = new Double[this.attributes.getClassAttribute().domain.size()];
for (int k=0;k<counts.length;k++) counts[k] =0.0;
for (int k =0;k<this.examples.size();k++) {
if (examples.get(k).get(attributeIndex).intValue()==i) {
temp = examples.get(k).get(index).intValue();
counts[temp]+=1;
all+=1;
}
}
for(int j =0;j<this.attributes.getClassAttribute().domain.size();j++)//ith class label {
if(all == 0||counts[j]==0)
continue;
sEntropy += -(counts[j]/all)*((Math.log((counts[j]/all))/Math.log(2)));
//System.out.println("counts[j]: "+counts[j]+" all: "+all+ " /: "+ counts[j]/all);
}
sEntropy *= s[i];
//System.out.println(sEntropy);
gain -= sEntropy;
//System.out.println(gain);
}
return gain;
}
Double[] getP() throws Exception {
index = this.attributes.getClassIndex();
// class p
if(pIsCalculated)
return p;
Double [] count = new Double[this.attributes.getClassAttribute().domain.size()];
for(int k=0;k<count.length;k++) count[k] =0.0;
p = new Double[count.length];
int temp = 0;
for (int i =0;i<this.examples.size();i++) {
temp = examples.get(i).get(index).intValue();
count[temp] += 1;
}
for (int i=0;i<p.length;i++) {
p[i] = (double) (count[i]/this.examples.size());
}
pIsCalculated = true;
return p;
}
Double getEntropy() throws Exception {
p = getP();
Double entropy = 0.0;
for(int i=0;i<p.length;i++) {
if(p[i]==0) continue;
entropy += -p[i]*(Math.log(p[i])/Math.log(2));
//System.out.println(entropy);
}
// System.out.println(entropy);
return entropy;
}
Double getSplitInformation(int attributeIndex) {
index = this.attributes.getClassIndex();
Double splitInformation = 0.0;
Double [] count = new Double[this.attributes.get(attributeIndex).domain.size()];
for(int k=0;k<count.length;k++) count[k] =0.0;
Double[] s = new Double[count.length];
int temp = 0;
for(int i =0;i<this.examples.size();i++) {
temp = examples.get(i).get(attributeIndex).intValue();
count[temp] += 1;
}
for(int i=0;i<s.length;i++) {
s[i] = (double) (count[i]/this.examples.size());
if(s[i]==0.0) continue;
splitInformation += -s[i]*(Math.log(s[i])/Math.log(2));
//System.out.println(Math.log(s[i])/Math.log(2));
}
//System.out.println("splitInformation "+splitInformation);
return splitInformation;
}
}