C45算法建立决策树JAVA练习DOC.docx
- 文档编号:7734
- 上传时间:2022-09-30
- 格式:DOCX
- 页数:15
- 大小:15.31KB
C45算法建立决策树JAVA练习DOC.docx
《C45算法建立决策树JAVA练习DOC.docx》由会员分享,可在线阅读,更多相关《C45算法建立决策树JAVA练习DOC.docx(15页珍藏版)》请在冰豆网上搜索。
C45算法建立决策树JAVA练习DOC
【决策树】—C4.5算法建立决策树JAVA练习
以下程序是我练习写的,不一定正确也没做存储优化。
有问题请留言交流。
转载请挂连接。
当前的属性为:
ageincomestudentcredit_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;
publicclassDecisionTree{
publicTreeNodecreateDT(List
System.out.println("当前的DATA为");
for(inti=0;i ArrayList for(intj=0;j System.out.print(temp.get(j)+""); } System.out.println(); } System.out.println("---------------------------------"); System.out.println("当前的ATTR为"); for(inti=0;i System.out.print(attributeList.get(i)+""); } System.out.println(); System.out.println("---------------------------------"); TreeNodenode=newTreeNode(); Stringresult=InfoGain.IsPure(InfoGain.getTarget(data)); if(result! =null){ node.setNodeName("leafNode"); node.setTargetFunValue(result); returnnode; } if(attributeList.size()==0){ node.setTargetFunValue(result); returnnode; }else{ InfoGaingain=newInfoGain(data,attributeList); doublemaxGain=0.0; intattrIndex=-1; for(inti=0;i doubletempGain=gain.getGainRatio(i); if(maxGain maxGain=tempGain; attrIndex=i; } } System.out.println("选择出的最大增益率属性为: "+attributeList.get(attrIndex)); node.setAttributeValue(attributeList.get(attrIndex)); List Map for(Map.Entry attrvalueMap.entrySet()){ resultData=gain.getData4Value(entry.getKey(),attrIndex); TreeNodeleafNode=null; System.out.println("当前为"+attributeList.get(attrIndex)+"的"+entry.getKey()+"分支。 "); if(resultData.size()==0){ leafNode=newTreeNode(); leafNode.setNodeName(attributeList.get(attrIndex)); leafNode.setTargetFunValue(result); leafNode.setAttributeValue(entry.getKey()); }else{ for(intj=0;j resultData.get(j).remove(attrIndex); } ArrayList resultAttr.remove(attrIndex); leafNode=createDT(resultData,resultAttr); } node.getChildTreeNode().add(leafNode); node.getPathName().add(entry.getKey()); } } returnnode; } classTreeNode{ privateStringattributeValue; privateList privateList privateStringtargetFunValue; privateStringnodeName; publicTreeNode(StringnodeName){ this.nodeName=nodeName; this.childTreeNode=newArrayList this.pathName=newArrayList } publicTreeNode(){ this.childTreeNode=newArrayList this.pathName=newArrayList } publicStringgetAttributeValue(){ returnattributeValue; } publicvoidsetAttributeValue(StringattributeValue){ this.attributeValue=attributeValue; } publicList returnchildTreeNode; } publicvoidsetChildTreeNode(List this.childTreeNode=childTreeNode; } publicStringgetTargetFunValue(){ returntargetFunValue; } publicvoidsetTargetFunValue(StringtargetFunValue){ this.targetFunValue=targetFunValue; } publicStringgetNodeName(){ returnnodeName; } publicvoidsetNodeName(StringnodeName){ this.nodeName=nodeName; } publicList returnpathName; } publicvoidsetPathName(List this.pathName=pathName; } } } 增益率计算类(取log的时候底用的是e,没用2) packageC45Test; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.HashSet; importjava.util.Iterator; importjava.util.List; importjava.util.Map; importjava.util.Set; //C4.5实现 publicclassInfoGain{ privateList privateList publicInfoGain(List this.data=newArrayList for(inti=0;i List ArrayList for(intj=0;j t.add(temp.get(j)); } this.data.add(t); } this.attribute=newArrayList
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C45 算法 建立 决策树 JAVA 练习 DOC