Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void buildTree(DecTreeNode parent,
- List<Instance> instances,
- List<Integer> remainingAttributes,
- String attributeName,
- DataSet train){
- this.labels=train.labels;
- this.attributes=train.attributes;
- this.attributeValues=train.attributeValues;
- if (attributes.isEmpty()){
- String majorLabel=pluralityValue(instances, train);
- int majorLabelIndex=labels.indexOf(majorLabel);
- parent.addChild( new DecTreeNode(majorLabelIndex, null, parent.attribute, true));
- }
- if (instances.isEmpty()){
- parent.addChild(new DecTreeNode(parent.label, null, parent.attribute, true));
- }
- else if(isPureNode(instances)){
- String pureLabel = pluralityValue(instances, train);
- int pureLabelIndex=labels.indexOf(pureLabel);
- parent.addChild(new DecTreeNode(pureLabelIndex, null, parent.attribute, true));
- }
- double entropy = calcEntropy(instances, labels.size());
- System.out.println("entropy is: "+entropy);
- int largestEntropyIndex = Integer.MIN_VALUE;
- double largestMutualInfomation = Double.MIN_VALUE;
- for (int i = 0; i < remainingAttributes.size(); i++) {
- double conditionalEntropy=calcConditionalEntropy(remainingAttributes.get(i), train);
- System.out.println("conditional entropy is: "+conditionalEntropy);
- double mutualInformation = entropy - conditionalEntropy;
- if (mutualInformation > largestMutualInfomation){
- largestMutualInfomation = mutualInformation;
- largestEntropyIndex = remainingAttributes.get(i);
- }
- }
- System.out.println("largest entropy index is: "+largestEntropyIndex);
- DecTreeNode currentNode;
- if(parent == null){
- String majorLabel = pluralityValue(instances, train);
- int majorLabelIndex=labels.indexOf(majorLabel);
- DTNode = new DecTreeNode(majorLabelIndex, largestEntropyIndex, -1 , isPureNode(instances) || remainingAttributes.size() == 1);
- currentNode = DTNode;
- parent=DTNode;
- } else {
- String majorLabel = pluralityValue(instances, train);
- int majorLabelIndex=labels.indexOf(majorLabel);
- currentNode = new DecTreeNode(majorLabelIndex, largestEntropyIndex, parent.attribute, isPureNode(instances) || remainingAttributes.size() == 0);
- parent.addChild(currentNode);
- }
- String largestEntropyAttr=attributes.get(largestEntropyIndex);
- int numBranches = attributeValues.get(largestEntropyAttr).size();
- ArrayList<ArrayList<Instance>> labelInstances = new ArrayList<ArrayList<Instance>>();
- for (int i = 0; i < numBranches; i++) {
- labelInstances.add(new ArrayList<Instance>());
- }
- for (Instance instance : instances) {
- labelInstances.get(instance.attributes.get(largestEntropyIndex)).add(instance);
- }
- for (int i = 0; i < numBranches; i++) {
- remainingAttributes.remove(remainingAttributes.indexOf(largestEntropyIndex));
- String largestEntropyAttrName=attributeValues.get(attributes.get(largestEntropyIndex)).get(i);
- buildTree(currentNode,labelInstances.get(i) , remainingAttributes, largestEntropyAttrName, train);
- remainingAttributes.add(largestEntropyIndex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment