C45算法建立决策树JAVA练习DOC.docx
《C45算法建立决策树JAVA练习DOC.docx》由会员分享,可在线阅读,更多相关《C45算法建立决策树JAVA练习DOC.docx(15页珍藏版)》请在冰豆网上搜索。
![C45算法建立决策树JAVA练习DOC.docx](https://file1.bdocx.com/fileroot1/2022-9/30/72e013ca-623b-43a6-a03e-e4b05ac7e2fe/72e013ca-623b-43a6-a03e-e4b05ac7e2fe1.gif)
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>data,ListattributeList){
System.out.println("当前的DATA为");
for(inti=0;iArrayListtemp=data.get(i);
for(intj=0;jSystem.out.print(temp.get(j)+"");
}
System.out.println();
}
System.out.println("---------------------------------");
System.out.println("当前的ATTR为");
for(inti=0;iSystem.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;idoubletempGain=gain.getGainRatio(i);
if(maxGainmaxGain=tempGain;
attrIndex=i;
}
}
System.out.println("选择出的最大增益率属性为:
"+attributeList.get(attrIndex));
node.setAttributeValue(attributeList.get(attrIndex));
List>resultData=null;
MapattrvalueMap=gain.getAttributeValue(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.setAttributeValue(entry.getKey());
}else{
for(intj=0;jresultData.get(j).remove(attrIndex);
}
ArrayListresultAttr=newArrayList(attributeList);
resultAttr.remove(attrIndex);
leafNode=createDT(resultData,resultAttr);
}
node.getChildTreeNode().add(leafNode);
node.getPathName().add(entry.getKey());
}
}
returnnode;
}
classTreeNode{
privateStringattributeValue;
privateListchildTreeNode;
privateListpathName;
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;
}
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;iListtemp=data.get(i);
ArrayListt=newArrayList();
for(intj=0;jt.add(temp.get(j));
}
this.data.add(t);
}
this.attribute=newArrayList