c45决策树java_c45算法建立决策树java练习.doc

41528d3028836879cd698677c3999917.gifc45算法建立决策树java练习.doc

【决策树】—C4.5算法建立决策树JAVA练习以下程序是我练习写的,不一定正确也没做存储优化。有问题请留言交流。转载请挂连接。当前的属性为:ageincomestudentcredit_rating当前的数据集为(最后一列是TARGET_VALUE):---------------------------------youthhighnofairnoyouthhighnoexcellentnomiddle_agedhighnofairyesseniorlowyesfairyesseniorlowyesexcellentnomiddle_agedlowyesexcellentyesyouthmediumnofairnoyouthlowyesfairyesseniormediumyesfairyesyouthmediumyesexcellentyesmiddle_agedhighyesfairyesseniormediumnoexcellentno---------------------------------C4.5建立树类packageC45Test;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;publicclassDecisionTree{publicTreeNodecreateDT(List>data,ListattributeList){System.out.println(“当前的DATA为“);for(inti=0;itemp=data.get(i);for(intj=0;j>resultData=null;MapattrvalueMap=gain.getAttributue(attrIndex);for(Map.Entryentry: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.setAttributue(entry.getKey());}else{for(intj=0;jresultAttr=newArrayList(attributeList);resultAttr.remove(attrIndex);leafNode=createDT(resultData,resultAttr);}node.getChildTreeNode().add(leafNode);node.getPathName().add(entry.getKey());}}returnnode;}classTreeNode{privateStringattributue;privateListchildTreeNode;privateListpathName;privateStringtargetFunValue;privateStringnodeName;publicTreeNode(StringnodeName){this.nodeName=nodeName;this.childTreeNode=newArrayList();this.pathName=newArrayList();}publicTreeNode(){this.childTreeNode=newArrayList();this.pathName=newArrayList();}publicStringgetAttributue(){returnattributue;}publicvoidsetAttributue(Stringattributue){this.attributue=attributue;}publicListgetChildTreeNode(){returnchildTreeNode;}publicvoidsetChildTreeNode(ListchildTreeNode){this.childTreeNode=childTreeNode;}publicStringgetTargetFunValue(){returntargetFunValue;}publicvoidsetTargetFunValue(StringtargetFunValue){this.targetFunValue=targetFunValue;}publicStringgetNodeName(){returnnodeName;}publicvoidsetNodeName(StringnodeName){this.nodeName=nodeName;}publicListgetPathName(){returnpathName;}publicvoidsetPathName(ListpathName){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>data;privateListattribute;publicInfoGain(List>data,Listattribute){this.data=newArrayList>();for(inti=0;itemp=data.get(i);ArrayListt=newArrayList();for(intj=0;j();for(intk=0;ktargetValueMap=getTargetValue();Settargetkey=targetValueMap.keySet();doubleentropy=0.0;for(Stringkey:targetkey){doublep=MathUtils.div((double)targetValueMap.get(key),(double)data.size());entropy+=(-1)*p*Math.log(p);}returnentropy;}//获得InfoApublicdoubl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值