以下程序是我练习写的,不一定正确也没做存储优化。有问题请留言交流。转载请挂连接。
当前的属性为:age income student credit_rating
当前的数据集为(最后一列是TARGET_VALUE):
---------------------------------
youth high no fair no
youth high no excellent no
middle_aged high no fair yes
senior low yes fair yes
senior low yes excellent no
middle_aged low yes excellent yes
youth medium no fair no
youth low yes fair yes
senior medium yes fair yes
youth medium yes excellent yes
middle_aged high yes fair yes
senior medium no excellent no
---------------------------------
C4.5建立树类
packageC45Test;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;public classDecisionTree {public TreeNode createDT(List> data,ListattributeList){
System.out.println("当前的DATA为");for(int i=0;i
ArrayList temp =data.get(i);for(int j=0;j
System.out.print(temp.get(j)+ " ");
}
System.out.println();
}
System.out.println("---------------------------------");
System.out.println("当前的ATTR为");for(int i=0;i
System.out.print(attributeList.get(i)+ " ");
}
System.out.println();
System.out.println("---------------------------------");
TreeNode node= newTreeNode();
String result=InfoGain.IsPure(InfoGain.getTarget(data));if(result != null){
node.setNodeName("leafNode");
node.setTargetFunValue(result);returnnode;
}if(attributeList.size() == 0){
node.setTargetFunValue(result);returnnode;
}else{
InfoGain gain= newInfoGain(data,attributeList);double maxGain = 0.0;int attrIndex = -1;for(int i=0;i
maxGain=tempGain;
attrIndex=i;
}
}
System.out.println("选择出的最大增益率属性为: " +attributeList.get(attrIndex));
node.setAttributeValue(attributeList.get(attrIndex));
List> resultData = null;
Map attrvalueMap =ga