lamiastella

buildTree

Oct 10th, 2014
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. private void buildTree(DecTreeNode parent,
  2.                            List<Instance> instances,
  3.                            List<Integer> remainingAttributes,
  4.                            String attributeName,
  5.                            DataSet train){
  6.  
  7.         this.labels=train.labels;
  8.         this.attributes=train.attributes;
  9.         this.attributeValues=train.attributeValues;
  10.         if (attributes.isEmpty()){
  11.             String majorLabel=pluralityValue(instances, train);
  12.             int majorLabelIndex=labels.indexOf(majorLabel);
  13.             parent.addChild( new DecTreeNode(majorLabelIndex, null, parent.attribute, true));
  14.         }
  15.         if (instances.isEmpty()){
  16.             parent.addChild(new DecTreeNode(parent.label, null, parent.attribute, true));
  17.         }
  18.         else if(isPureNode(instances)){
  19.             String pureLabel = pluralityValue(instances, train);
  20.             int pureLabelIndex=labels.indexOf(pureLabel);
  21.             parent.addChild(new DecTreeNode(pureLabelIndex, null, parent.attribute, true));
  22.         }
  23.  
  24.         double entropy = calcEntropy(instances, labels.size());
  25.         System.out.println("entropy is: "+entropy);
  26.         int largestEntropyIndex = Integer.MIN_VALUE;
  27.         double largestMutualInfomation = Double.MIN_VALUE;
  28.         for (int i = 0; i < remainingAttributes.size(); i++) {
  29.             double conditionalEntropy=calcConditionalEntropy(remainingAttributes.get(i), train);
  30.             System.out.println("conditional entropy is: "+conditionalEntropy);
  31.             double mutualInformation = entropy - conditionalEntropy;
  32.             if (mutualInformation > largestMutualInfomation){
  33.                 largestMutualInfomation = mutualInformation;
  34.                 largestEntropyIndex = remainingAttributes.get(i);
  35.             }
  36.         }
  37.         System.out.println("largest entropy index is: "+largestEntropyIndex);
  38.         DecTreeNode currentNode;
  39.         if(parent == null){
  40.             String majorLabel = pluralityValue(instances, train);
  41.             int majorLabelIndex=labels.indexOf(majorLabel);
  42.             DTNode = new DecTreeNode(majorLabelIndex, largestEntropyIndex, -1 , isPureNode(instances) || remainingAttributes.size() == 1);
  43.             currentNode = DTNode;
  44.             parent=DTNode;
  45.         } else {
  46.             String majorLabel = pluralityValue(instances, train);
  47.             int majorLabelIndex=labels.indexOf(majorLabel);
  48.             currentNode = new DecTreeNode(majorLabelIndex, largestEntropyIndex, parent.attribute, isPureNode(instances) || remainingAttributes.size() == 0);
  49.             parent.addChild(currentNode);
  50.         }
  51.         String largestEntropyAttr=attributes.get(largestEntropyIndex);
  52.         int numBranches = attributeValues.get(largestEntropyAttr).size();
  53.         ArrayList<ArrayList<Instance>> labelInstances = new ArrayList<ArrayList<Instance>>();
  54.         for (int i = 0; i < numBranches; i++) {
  55.             labelInstances.add(new ArrayList<Instance>());
  56.         }
  57.         for (Instance instance : instances) {
  58.             labelInstances.get(instance.attributes.get(largestEntropyIndex)).add(instance);
  59.         }
  60.         for (int i = 0; i < numBranches; i++) {
  61.             remainingAttributes.remove(remainingAttributes.indexOf(largestEntropyIndex));
  62.             String largestEntropyAttrName=attributeValues.get(attributes.get(largestEntropyIndex)).get(i);
  63.             buildTree(currentNode,labelInstances.get(i) , remainingAttributes, largestEntropyAttrName, train);
  64.             remainingAttributes.add(largestEntropyIndex);
  65.         }
  66.        
  67.  
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment