-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeGenerator.java
More file actions
90 lines (75 loc) · 2.96 KB
/
TreeGenerator.java
File metadata and controls
90 lines (75 loc) · 2.96 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
package recommender.sol;
import recommender.src.IAttributeDataset;
import recommender.src.IAttributeDatum;
import recommender.src.IGenerator;
import recommender.src.INode;
import java.util.LinkedList;
import java.util.Random;
/**
* Class that generates decision trees using datasets
*
* @param <T>
*/
public class TreeGenerator<T extends IAttributeDatum> implements IGenerator {
IAttributeDataset<T> data;
INode classifier;
/**
* constructor takes in training data
*
* @param initTrainingData
*/
public TreeGenerator(IAttributeDataset<T> initTrainingData) {
this.data = initTrainingData;
}
/**
* the recursive part of buildClassifier - makes each layer of the tree one by one
*
* @param remAtt - list of attributes that still haven't appeared in the tree yet
* @param targetAttr - the attribute to be predicted
* @return - a decision tree
*/
public INode treeLayer(LinkedList<String> remAtt, String targetAttr) {
if (this.data.allSameValue(targetAttr)) { // if all data have the same value for the target attribute
return new Leaf(this.data.getSharedValue(targetAttr));
}
Random rng = new Random();
int index;
if (remAtt.size() == 0) { // no more attributes left to partition data with
return new Leaf(this.data.mostCommonValue(targetAttr));
} else if (remAtt.size() == 1) { // last attribute left
index = 0;
} else { // randomly choose attribute
index = rng.nextInt((remAtt.size() - 1));
}
String att = remAtt.get(index);
Node<T> parent = new Node<T>(att);
parent.mostCommon = this.data.mostCommonValue(targetAttr);
remAtt.remove(att); // remove current attribute from the list so that it won't be used to partition data again
LinkedList<IAttributeDataset<T>> dataList = this.data.partition(att);
for (IAttributeDataset<T> set : dataList) {
TreeGenerator<T> gen = new TreeGenerator<T>(set);
INode child = gen.treeLayer(remAtt, targetAttr); // recursive call
parent.children.addLast(child);
parent.edges.addLast(set.getSharedValue(att));
}
return parent;
}
@Override
public INode buildClassifier(String targetAttr) {
LinkedList<String> remAtt = new LinkedList<String>();
for (String att : this.data.getAttributes()) {
remAtt.addLast(att);
}
remAtt.remove(targetAttr); // don't want to partition based on target attribute
this.classifier = this.treeLayer(remAtt, targetAttr);
return this.classifier;
}
@Override
public Object lookupRecommendation(IAttributeDatum forVals) {
return this.classifier.lookupDecision(forVals);
}
@Override
public void printTree() {
this.classifier.printNode("");
}
}