2016-12-30 175 views
0

我正在嘗試編寫生成決策樹的ID3算法,但運行我的代碼時出現StackOverflowError錯誤。 當調試時,我注意到循環開始時,屬性下降到4(從最初9)。 樹生成的代碼如下。我打電話的所有功能都正常工作,它們已經過測試。 但是,錯誤代碼指出問題出在另一個使用流的函數上,但它已經單獨測試了 ,我知道它正常工作。請記住,我正在處理隨機數據,因此該函數有時會拋出錯誤,有時不會。我在其下面發佈了錯誤代碼,但熵函數和信息增益工作。StackOverflowError決策樹生成JAVA

這是樹節點結構:

public class TreeNode { 
    List<Patient> samples; 
    List<TreeNode> children; 
    TreeNode parent; 
    Integer attribute; 
    String attributeValue; 
    String className; 

    public TreeNode(List<Patient> samples, List<TreeNode> children, TreeNode parent, Integer attribute, 
      String attributeValue, String className) { 
     this.samples = samples; 
     this.children = children; 
     this.parent = parent; 
     this.attribute = attribute; 
     this.attributeValue = attributeValue; 
     this.className = className; 
    } 
} 

這就是拋出錯誤代碼:

public TreeNode id3(List<Patient> patients, List<Integer> attributes, TreeNode root) { 
     boolean isLeaf = patients.stream().collect(Collectors.groupingBy(i -> i.className)).keySet().size() == 1; 
     if (isLeaf) { 
      root.setClassName(patients.get(0).className); 
      return root; 
     } 
     if (attributes.size() == 0) { 
      root.setClassName(mostCommonClass(patients)); 
      return root; 
     } 
     int bestAttribute = maxInformationGainAttribute(patients, attributes); 
     Set<String> attributeValues = attributeValues(patients, bestAttribute); 
     for (String value : attributeValues) { 
      List<Patient> branch = patients.stream().filter(i -> i.patientData[bestAttribute].equals(value)) 
        .collect(Collectors.toList()); 

      TreeNode child = new TreeNode(branch, new ArrayList<>(), root, bestAttribute, value, null); 

      if (branch.isEmpty()) { 
       child.setClassName(mostCommonClass(patients)); 
       root.addChild(new TreeNode(child)); 
      } else { 
       List<Integer> newAttributes = new ArrayList<>(); 
       newAttributes.addAll(attributes); 
       newAttributes.remove(new Integer(bestAttribute)); 
       root.addChild(new TreeNode(id3(branch, newAttributes, child))); 
      } 
     } 
     return root; 
    } 

這些都是其他功能:

public static double entropy(List<Patient> patients) { 
     double entropy = 0.0; 
     double recurP = (double) patients.stream().filter(i -> i.className.equals("recurrence-events")).count() 
       /(double) patients.size(); 
     double noRecurP = (double) patients.stream().filter(i -> i.className.equals("no-recurrence-events")).count() 
       /(double) patients.size(); 
     entropy -= (recurP * (recurP > 0 ? Math.log(recurP) : 0/Math.log(2)) 
       + noRecurP * (noRecurP > 0 ? Math.log(noRecurP) : 0/Math.log(2))); 
     return entropy; 
    } 



public static double informationGain(List<Patient> patients, int attribute) { 
     double informationGain = entropy(patients); 
     Map<String, List<Patient>> patientsGroupedByAttribute = patients.stream() 
       .collect(Collectors.groupingBy(i -> i.patientData[attribute])); 
     List<List<Patient>> subsets = new ArrayList<>(); 
     for (String i : patientsGroupedByAttribute.keySet()) { 
      subsets.add(patientsGroupedByAttribute.get(i)); 
     } 

     for (List<Patient> lp : subsets) { 
      informationGain -= proportion(lp, patients) * entropy(lp); 
     } 
     return informationGain; 
    } 


private static int maxInformationGainAttribute(List<Patient> patients, List<Integer> attributes) { 
     int maxAttribute = 0; 
     double maxInformationGain = 0; 
     for (int i : attributes) { 
      if (informationGain(patients, i) > maxInformationGain) { 
       maxAttribute = i; 
       maxInformationGain = informationGain(patients, i); 
      } 
     } 
     return maxAttribute; 
    } 

例外:

Exception in thread "main" java.lang.StackOverflowError 
    at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source) 
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source) 
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source) 
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) 
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source) 
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source) 
    at java.util.stream.LongPipeline.reduce(Unknown Source) 
    at java.util.stream.LongPipeline.sum(Unknown Source) 
    at java.util.stream.ReferencePipeline.count(Unknown Source) 
    at Patient.entropy(Patient.java:39) 
    at Patient.informationGain(Patient.java:67) 
    at Patient.maxInformationGainAttribute(Patient.java:85) 
    at Patient.id3(Patient.java:109) 

回答

0

行:

root.addChild(new TreeNode(id3(branch, newAttributes, child)));

被調用每一個方法遞歸時間,從而導致堆棧溢出。這告訴我你的邏輯中有什麼錯誤,沒有任何結束遞歸的「基本情況」,即返回根目錄。我對預期的行爲或開始的數據知之甚少,無法確定發生了什麼問題,但我會先用調試器逐步完成代碼,並確保該方法中的邏輯表現出您期望的行爲。我知道這不是一個很好的答案,但它是一個起點,希望幫助或其他人會用更具體的解決方案加以注意。

+0

我一直在一遍又一遍的調試它,它的工作原理直到屬性降到4,這是奇怪的部分。當屬性下降到4時,它開始回退一步,並再次向前走。但它直到那時才生成適當的樹。 :( – vixenn

+0

我會看看兩種方法, maxInformationGainAttribute(患者,屬性); 和 attributeValues(patients,bestAttribute); ,並確保它們返回您所期望的值,以防止它卡住。 –

+0

確保maxInformationGainAttribute(patients,attributes);正在做它應該做的事情,因爲如果它不修改屬性列表,那麼您將在此行傳遞相同的值: newAttributes.addAll(attributes); –